Created
February 19, 2013 03:20
-
-
Save ajsharp/4982804 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/lib/bldr/node.rb b/lib/bldr/node.rb | |
| index 2d3fa5f..9e19f87 100644 | |
| --- a/lib/bldr/node.rb | |
| +++ b/lib/bldr/node.rb | |
| @@ -23,11 +23,22 @@ module Bldr | |
| @parent = opts[:parent] | |
| @views = opts[:views] | |
| @locals = opts[:locals] | |
| + | |
| + copy_ivars_from_parent(opts[:parent]) if opts[:parent] | |
| # Storage hash for all descendant nodes | |
| @result = {} | |
| instance_eval(&block) if block_given? | |
| end | |
| + | |
| + PROTECTED_IVARS = [:@parent, :@opts, :@current_object, :@views, :@locals, :@result] | |
| + | |
| + def copy_ivars_from_parent(parent) | |
| + (parent.instance_variables - PROTECTED_IVARS).map(&:to_s).each do |var| | |
| + instance_variable_set(var, parent.instance_variable_get(var)) | |
| + end | |
| + end | |
| + private :copy_ivars_from_parent | |
| # Create and render a node. | |
| # | |
| diff --git a/lib/sinatra/bldr.rb b/lib/sinatra/bldr.rb | |
| index 1b13295..04d0c2d 100644 | |
| --- a/lib/sinatra/bldr.rb | |
| +++ b/lib/sinatra/bldr.rb | |
| @@ -23,12 +23,16 @@ module Sinatra | |
| # @param [Hash] opts a hash of options | |
| # @option opts [Hash] :locals a hash of local variables to be used in the template | |
| def bldr(template, opts = {}, &block) | |
| - opts[:scope] = ::Bldr::Node.new(nil, opts.merge(:views => (settings.views || "./views"))) | |
| - | |
| locals = opts.delete(:locals) || {} | |
| + opts[:scope] = ::Bldr::Node.new(nil, opts.merge(:views => (settings.views || "./views"))) | |
| + | |
| # copy local instance_variables to template | |
| - instance_variables.map(&:to_s).each { |var| locals[var] = instance_variable_get(var) } | |
| + instance_variables.map(&:to_s).each do |var| | |
| + val = instance_variable_get(var) | |
| + locals[var] = val | |
| + opts[:scope].instance_variable_set(var, val) | |
| + end | |
| MultiJson.encode render(:bldr, template, opts, locals, &block).result | |
| # @todo add support for alternate formats, like plist | |
| diff --git a/spec/integration/sinatra_spec.rb b/spec/integration/sinatra_spec.rb | |
| index ca01020..da43b02 100644 | |
| --- a/spec/integration/sinatra_spec.rb | |
| +++ b/spec/integration/sinatra_spec.rb | |
| @@ -60,12 +60,22 @@ describe "Using Bldr with a sinatra app" do | |
| @person = Person.new('bert', 99) | |
| bldr :'fixtures/ivar' | |
| end | |
| + | |
| + get '/nested_ivars' do | |
| + @person = Person.new('bert', 99) | |
| + bldr :'fixtures/nested_ivars' | |
| + end | |
| end | |
| it 'passes ivars through to the template' do | |
| response = Rack::MockRequest.new(TestApp).get('/ivar') | |
| decode(response.body).should == {'person' => {'name' => 'bert', 'age' => 99}} | |
| end | |
| + | |
| + it 'makes ivars available in nested objects' do | |
| + response = Rack::MockRequest.new(TestApp).get('/nested_ivars') | |
| + decode(response.body).should == {'person' => {'name' => 'bert', 'age' => 99}} | |
| + end | |
| it "properly renders a template that only contains a template call" do | |
| response = Rack::MockRequest.new(TestApp).get('/root_partial') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment