julien51-babylon 0.1.9 → 0.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,18 +13,22 @@ module Babylon
13
13
  # The caller needs to pass the context in which the partial will be rendered
14
14
  # Render must be called with :partial as well (other options will be supported later). The partial vale should be a relative path
15
15
  # to another file view, from the calling view.
16
+ # You can also use :locals => {:name => value} to use defined locals in your embedded views.
16
17
  def render(xml, options = {})
17
18
  # First, we need to identify the partial file path, based on the @view_template path.
18
19
  partial_path = (@view_template.split("/")[0..-2] + options[:partial].split("/")).join("/").gsub(".xml.builder", "") + ".xml.builder"
19
20
  raise ViewFileNotFound, "No such file #{partial_path}" unless Babylon.views[partial_path]
21
+ saved_locals = @locals
22
+ @locals = options[:locals]
20
23
  eval(Babylon.views[partial_path], binding, partial_path, 1)
24
+ @locals = saved_locals # Re-assign the previous locals to be 'clean'
21
25
  end
22
26
 
23
27
  ##
24
28
  # Instantiate a new view with the various varibales passed in assigns and the path of the template to render.
25
29
  def initialize(path = "", assigns = {})
26
30
  @view_template = path
27
-
31
+ @locals = {}
28
32
  assigns.each do |key, value|
29
33
  instance_variable_set(:"@#{key}", value)
30
34
  end
@@ -41,6 +45,14 @@ module Babylon
41
45
  end
42
46
  builder.doc.root.children # we output the document built
43
47
  end
48
+
49
+ ##
50
+ # Used to macth locals variables
51
+ def method_missing(sym, *args, &block)
52
+ raise NameError, "undefined local variable or method `#{sym}' for #{self}" unless @locals[sym]
53
+ @locals[sym]
54
+ end
55
+
44
56
  end
45
57
  end
46
58
  end
@@ -68,11 +68,12 @@ describe Babylon::Base::View do
68
68
  @xml_string = <<-eoxml
69
69
  xml.message(:to => "you", :from => "me", :type => :chat) do |message|
70
70
  message.body("salut")
71
- render(message, {:partial => "partial"})
71
+ render(message, {:partial => "partial", :locals => {:subtitle => "bonjour monde"}})
72
72
  end
73
73
  eoxml
74
74
  @partial_string = <<-eoxml
75
75
  xml.title("hello word")
76
+ xml.subtitle(subtitle)
76
77
  eoxml
77
78
  Babylon.views.stub!(:[]).with(@view_template).and_return(@xml_string)
78
79
  Babylon.views.stub!(:[]).with("/a/path/to/a/view/partial.xml.builder").and_return(@partial_string)
@@ -81,6 +82,11 @@ describe Babylon::Base::View do
81
82
  it "should render the partial in the right context" do
82
83
  @view.evaluate.xpath("//message/title").text.should == "hello word"
83
84
  end
85
+
86
+ it "should allocate the locals variables" do
87
+ @view.evaluate.xpath("//message/subtitle").text.should == "bonjour monde"
88
+ end
89
+
84
90
  end
85
91
 
86
92
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: julien51-babylon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - julien Genestoux
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-06 00:00:00 -07:00
12
+ date: 2009-08-11 00:00:00 -07:00
13
13
  default_executable: babylon
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency