summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2012-01-04 09:14:04 +0100
committerJérémy Zurcher <jeremy@asynk.ch>2012-01-04 09:14:04 +0100
commite782955f6f6b0814685c9377b6663a76d18c0b7b (patch)
treeaababd07598d44e4af2cc4d823afd8d1bf58d908
parent50b5749972b78a5a4e03e5c2075e884296f0269d (diff)
downloadzorglub-e782955f6f6b0814685c9377b6663a76d18c0b7b.zip
zorglub-e782955f6f6b0814685c9377b6663a76d18c0b7b.tar.gz
Node swallows defs as inherited_vars, update specs
-rw-r--r--lib/zorglub/defs.rb48
-rw-r--r--lib/zorglub/node.rb24
-rw-r--r--spec/node_spec.rb30
-rw-r--r--spec/spec_helper.rb14
4 files changed, 43 insertions, 73 deletions
diff --git a/lib/zorglub/defs.rb b/lib/zorglub/defs.rb
deleted file mode 100644
index accbc4d..0000000
--- a/lib/zorglub/defs.rb
+++ /dev/null
@@ -1,48 +0,0 @@
-# -*- coding: UTF-8 -*-
-#
-module Zorglub
- #
- class Node
- #
- @defs_h = {
- }
- #
- class << self
- #
- attr_reader :defs_h
- #
- # TODO override is maybe not the better way
- def inherited sub
- sub.layout layout
- sub.engine engine
- sub.instance_variable_set :@defs_h, {}
- @defs_h.each do |d,v|
- sub.defs d, *v
- end
- end
- #
- def defs sym, *args
- unless args.empty?
- @defs_h[sym] ||=[]
- @defs_h[sym].concat args
- @defs_h[sym].uniq!
- end
- @defs_h[sym]
- end
- #
- end
- #
- def defs sym, *args
- d = self.class.defs_h[sym].clone
- unless args.empty?
- d ||=[]
- d.concat args
- d.uniq!
- end
- d
- end
- end
- #
-end
-#
-# EOF
diff --git a/lib/zorglub/node.rb b/lib/zorglub/node.rb
index 669f2c4..78f46af 100644
--- a/lib/zorglub/node.rb
+++ b/lib/zorglub/node.rb
@@ -9,13 +9,17 @@ module Zorglub
:after_all => [],
}
#
+ @inherited_vars = { }
+ #
class << self
#
- attr_reader :hooks
+ attr_reader :hooks, :inherited_vars
#
def inherited sub
sub.layout layout
sub.engine engine
+ sub.instance_variable_set :@inherited_vars, {}
+ @inherited_vars.each do |s,v| sub.inherited_var s, *v end
end
#
def engine engine=nil
@@ -28,6 +32,15 @@ module Zorglub
@layout ||= Config.layout
end
#
+ def inherited_var sym, *args
+ var = @inherited_vars[sym] ||=[]
+ unless args.empty?
+ var.concat args
+ var.uniq!
+ end
+ var
+ end
+ #
attr_writer :app
def map app, location
@app = app
@@ -146,6 +159,15 @@ module Zorglub
File.join(Config.view_base_path, @action[:view])+Config.engine_ext(@action[:engine])
end
#
+ def inherited_var sym, *args
+ d = self.class.inherited_vars[sym].clone || []
+ unless args.empty?
+ d.concat args
+ d.uniq!
+ end
+ d
+ end
+ #
def args
@action[:args]
end
diff --git a/spec/node_spec.rb b/spec/node_spec.rb
index 4b4fbed..889ba7c 100644
--- a/spec/node_spec.rb
+++ b/spec/node_spec.rb
@@ -126,31 +126,25 @@ describe Zorglub do
r.header['location'].should == Node0.r(:do_partial,1,2,3)
end
#
- it "defs should be inherited" do
+ it "inherited_vars should be inherited and extended" do
r = Node5.my_call '/index'
- ar = YAML.load r.body[0]
- ar[0].should == 'js0'
- ar[1].should == 'js1'
- ar[2].should == 'js3'
- ar[3].should == 'jsx'
- ar[4].should be_nil
+ vars = YAML.load r.body[0]
+ vars.should == ['js0','js1','js3','jsx','css0','css1','css2']
+ vars[7].should be_nil
end
#
- it "defs should be method scope modified" do
+ it "inherited_vars should be extended at method level" do
r = Node4.my_call '/more'
- ar = YAML.load r.body[0]
- ar[0].should == 'js0'
- ar[1].should == 'js1'
- ar[2].should == 'js2'
- ar[3].should be_nil
+ vars = YAML.load r.body[0]
+ vars.should == ['js0','js1','js2']
+ vars[3].should be_nil
end
#
- it "defs should not have been modified by other method" do
+ it "inherited_vars should be untouched" do
r = Node4.my_call '/index'
- ar = YAML.load r.body[0]
- ar[0].should == 'js0'
- ar[1].should == 'js1'
- ar[2].should be_nil
+ vars = YAML.load r.body[0]
+ vars.should == ['js0','js1']
+ vars[2].should be_nil
end
end
#
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 84aebdd..96d224f 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -11,7 +11,6 @@ end
require 'yaml'
#
require 'zorglub'
-require 'zorglub/defs'
#
HASH_PROC = Proc.new { |path,obj| {:path=>path,:layout=>obj.layout,:view=>obj.view,:args=>obj.args}.to_yaml }
RENDER_PROC = Proc.new { |path,obj|
@@ -101,22 +100,25 @@ class Node3 < Zorglub::Node
end
#
class Node4 < Zorglub::Node
- defs :js,'js0','js1'
+ inherited_var :js,'js0','js1'
def index
no_layout
- defs(:js).to_yaml
+ inherited_var(:js).to_yaml
end
def more
no_layout
- defs(:js,'js2').to_yaml
+ inherited_var(:js,'js2').to_yaml
end
end
#
class Node5 < Node4
- defs :js, 'js3'
+ inherited_var :js, 'js3'
+ inherited_var :css, 'css0', 'css1'
def index
no_layout
- defs(:js,'jsx').to_yaml
+ js = inherited_var(:js,'jsx')
+ css = inherited_var(:css, 'css0', 'css1','css2')
+ js.concat(css).to_yaml
end
end
#