diff options
-rw-r--r-- | Changelog | 1 | ||||
-rw-r--r-- | example/layout/css.haml | 2 | ||||
-rw-r--r-- | example/sample.ru | 4 | ||||
-rw-r--r-- | lib/zorglub/node.rb | 55 | ||||
-rw-r--r-- | spec/node_spec.rb | 10 | ||||
-rw-r--r-- | spec/spec_helper.rb | 19 |
6 files changed, 50 insertions, 41 deletions
@@ -3,6 +3,7 @@ * add sass engine * use Bundler * enable travis + * fix and rename inherited_vars in cli_vals 2012-01-17 Jérémy Zurcher <jeremy@asynk.ch> * release 0.5 diff --git a/example/layout/css.haml b/example/layout/css.haml index fbe0a47..a48c120 100644 --- a/example/layout/css.haml +++ b/example/layout/css.haml @@ -1,5 +1,5 @@ %h1=@title %p="xx <b>css</b> layout xx" -%p="Css #{inherited_var(:css).inspect}" +%p="Css #{cli_val(:css).inspect}" %p=@content %p="xx -- xx" diff --git a/example/sample.ru b/example/sample.ru index 9dc4d13..c7fd639 100644 --- a/example/sample.ru +++ b/example/sample.ru @@ -76,7 +76,7 @@ class Node2 < Zorglub::Node # class level engine engine! 'tmp-engine' # class level css - inherited_var :css, 'class_level.css' + cli_val :css, 'class_level.css' # def index *args "<title>Node2:index</title><b>START</b>#{html}<a href=#{Node2.r(:meth0)}>next</a><br/><b>END</b>" @@ -84,7 +84,7 @@ class Node2 < Zorglub::Node # def meth0 *args # instance level css - inherited_var :css, 'instance_level.css' + cli_val :css, 'instance_level.css' "<title>Node2:meth0</title><b>START</b>#{html}<a href=#{Node2.r(:meth1,1,2)}>next</a><br/><b>END</b>" end # diff --git a/lib/zorglub/node.rb b/lib/zorglub/node.rb index 5421046..8fe1e4e 100644 --- a/lib/zorglub/node.rb +++ b/lib/zorglub/node.rb @@ -154,59 +154,60 @@ module Zorglub "You are being redirected, please follow this link to: <a href='#{target}'>#{target}</a>!" end # - # inherited vars, they can be modified at class level only + # class level inherited values are key=>array, copied at inheritance + # so they can be extanded at class level + # values are copied from class into instance at object creation + # so that can be extanded without modifying class level values + # typical usage are css or js inclusions # - @inherited_vars = { } + @cli_vals = { } # class << self # - attr_reader :inherited_vars + attr_reader :cli_vals # - def inherited_var sym, *args - var = @inherited_vars[sym] ||=[] + def cli_val sym, *args + vals = @cli_vals[sym] ||=[] unless args.empty? - var.concat args - var.uniq! + vals.concat args + vals.uniq! end - var + vals end # end # - def inherited_var sym, *args - @instance_inherited_vars ||={} - var= @instance_inherited_vars[sym] ||= self.class.inherited_vars[sym].clone || [] + def cli_val sym, *args + vals = @cli_vals[sym] ||=[] unless args.empty? - var.concat args - var.uniq! + vals.concat args + vals.uniq! end - var + vals end # # before_all and after_all hooks # - @inherited_vars[:before_all] = [] - @inherited_vars[:after_all] = [] + @cli_vals[:before_all] = [] + @cli_vals[:after_all] = [] class << self # - attr_reader :hooks - # def call_before_hooks obj - @inherited_vars[:before_all].each do |blk| blk.call obj end + @cli_vals[:before_all].each do |blk| blk.call obj end end # def before_all &blk - @inherited_vars[:before_all]<< blk - @inherited_vars[:before_all].uniq! + @cli_vals[:before_all]<< blk + @cli_vals[:before_all].uniq! end # def call_after_hooks obj - @inherited_vars[:after_all].each do |blk| blk.call obj end + @cli_vals[:after_all].each do |blk| blk.call obj end end # def after_all &blk - @inherited_vars[:after_all]<< blk - @inherited_vars[:after_all].uniq! + @cli_vals[:after_all]<< blk + @cli_vals[:after_all].uniq! end # end @@ -218,8 +219,8 @@ module Zorglub def inherited sub sub.engine! engine||(self==Zorglub::Node ? UNDEFINED : nil ) sub.layout! layout||(self==Zorglub::Node ? UNDEFINED : nil ) - sub.instance_variable_set :@inherited_vars, {} - @inherited_vars.each do |s,v| sub.inherited_var s, *v end + sub.instance_variable_set :@cli_vals, {} + @cli_vals.each do |s,v| sub.cli_val s, *v end end # def call env @@ -256,6 +257,8 @@ module Zorglub @options = options @request = Rack::Request.new env @response = Rack::Response.new + @cli_vals ={} + self.class.cli_vals.each do |s,v| cli_val s, *v end end # def state state=nil diff --git a/spec/node_spec.rb b/spec/node_spec.rb index 5a0dcf1..a125d56 100644 --- a/spec/node_spec.rb +++ b/spec/node_spec.rb @@ -199,25 +199,29 @@ describe Zorglub do Node5.layout.should be_nil end # - it "inherited_vars should be inherited and extended" do + it "cli_vals should be inherited and extended" do r = Node5.my_call '/index' vars = YAML.load r.body[0] vars.should == ['js0','js1','js3','jsx','css0','css1','css2'] vars[7].should be_nil end # - it "inherited_vars should be extended at method level" do + it "cli_vals should be extended at method level" do r = Node4.my_call '/more' vars = YAML.load r.body[0] vars.should == ['js0','js1','js2'] vars[3].should be_nil end # - it "inherited_vars should be untouched" do + it "cli_vals should be untouched" do r = Node4.my_call '/index' vars = YAML.load r.body[0] vars.should == ['js0','js1'] vars[2].should be_nil + r = Node5.my_call '/index' + vars = YAML.load r.body[0] + vars.should == ['js0','js1','js3','jsx','css0','css1','css2'] + vars[7].should be_nil end # it "ext definition and file engine should work" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e710967..9369519 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -122,24 +122,25 @@ end # class Node4 < Zorglub::Node no_layout! - inherited_var :js,'js0' - inherited_var :js,'js1' + cli_val :js,'js0' + cli_val :js,'js1' def index - inherited_var(:js).to_yaml + cli_val(:js).to_yaml end def more - inherited_var(:js,'js2').to_yaml + cli_val :js,'js2' + cli_val(:js).to_yaml end end # class Node5 < Node4 - inherited_var :js, 'js3' - inherited_var :css, 'css0', 'css1' + cli_val :js, 'js3' + cli_val :css, 'css0', 'css1' # no_layout! inherited from Node4 def index - js = inherited_var(:js,'jsx') - inherited_var(:css, 'css0', 'css1','css2') - css = inherited_var :css + js = cli_val(:js,'jsx') + cli_val(:css, 'css0', 'css1','css2') + css = cli_val :css js.concat(css).to_yaml end end |