Skip to content

Commit

Permalink
Merge pull request #45 from eudoxa/fix_stacks_of_default_block_and_lo…
Browse files Browse the repository at this point in the history
…cals

fix stacks of default block and locals
  • Loading branch information
eudoxa committed May 11, 2015
2 parents 6ff352f + 53e6044 commit 7477696
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
30 changes: 24 additions & 6 deletions lib/chanko/invoker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ def method_missing(method_name, *args, &block)
end
end

def __current_run_default_depth
@__run_default_depth ||= 0
end

def __increment_run_default_depth
@__run_default_depth = __current_run_default_depth + 1
end

def __decrement_run_default_depth
@__run_default_depth = __current_run_default_depth - 1
end

def __find_unit_local(method_name)
__current_unit_locals.has_key?(method_name)
end
Expand All @@ -49,7 +61,7 @@ def __fetch_unit_local(method_name)
end

def __current_unit_locals
__unit_locals_stack.last || {}
__unit_locals_stack[-1 - __current_run_default_depth] || {}
end

def __unit_locals_stack
Expand All @@ -73,18 +85,24 @@ def __defaults_stack
end

def __default_block
__defaults_stack.last
__defaults_stack[-1 - __current_run_default_depth]
end

def __has_default_block?
!!__default_block
end

def __invoke_default_block
if view?
capture(&__default_block)
else
instance_exec(&__default_block)
current_default_block = __default_block
begin
__increment_run_default_depth
if view?
capture(&current_default_block)
else
instance_exec(&current_default_block)
end
ensure
__decrement_run_default_depth
end
end

Expand Down
17 changes: 17 additions & 0 deletions spec/chanko/invoker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,23 @@ module Chanko
end
end

context "when nested run_default is called in function" do
it "invokes given block as a fallback" do
Chanko::Loader.load("sensitive_unit")
expect(SensitiveUnit).to receive(:ping).once

controller.invoke(:sensitive_unit, :outer_default) do
"default"
end.should eq "default"
end

it 'use both locals' do
controller.invoke(:example_unit, :nesting_locals_outer, :locals => { :outer_one => "outer_one", :outer_two => "outer_two", :outer_three => "outer_three"}) do
"default"
end.should eq "outer_one.inner_one.outer_two.default.inner_two.outer_three"
end
end

context "when run_default is called but no block given" do
it "invokes given block as a fallback" do
controller.invoke(:example_unit, :default).should == nil
Expand Down
13 changes: 13 additions & 0 deletions spec/fixtures/units/example_unit/example_unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ module ExampleUnit
function(:render) do
render_to_string :partial => "/test", :locals => { :local => "test" }
end

function(:nesting_locals_outer) do
result = "#{outer_one}."
result += invoke(:example_unit, :nesting_locals_inner, :locals => { :inner_one => "inner_one", :inner_two => "inner_two" }) do
"#{outer_two}.#{run_default}"
end
result += ".#{outer_three}"
result
end

function(:nesting_locals_inner) do
"#{inner_one}.#{run_default}.#{inner_two}"
end
end

scope(:view) do
Expand Down
16 changes: 16 additions & 0 deletions spec/fixtures/units/sensitive_unit/sensitive_unit.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
module SensitiveUnit
include Chanko::Unit
raise_error

def self.ping
end

scope(:controller) do
function(:outer_default) do
invoke(:sensitive_unit, :inner_default) do
SensitiveUnit.ping
run_default
end
end

function(:inner_default) do
run_default
end
end
end

0 comments on commit 7477696

Please sign in to comment.