Skip to content

Commit

Permalink
Merge pull request #47 from eudoxa/use_stack_control_instead_of_counting
Browse files Browse the repository at this point in the history
use stack control instead of counting
  • Loading branch information
eudoxa committed May 12, 2015
2 parents a600052 + f4d91b0 commit 96ca782
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
45 changes: 24 additions & 21 deletions lib/chanko/invoker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,6 @@ 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 @@ -61,7 +49,7 @@ def __fetch_unit_local(method_name)
end

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

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

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

def __has_default_block?
Expand All @@ -95,14 +83,15 @@ def __has_default_block?
def __invoke_default_block
current_default_block = __default_block
begin
__increment_run_default_depth
if view?
capture(&current_default_block)
else
instance_exec(&current_default_block)
__without_default_stack do
__without_locals_stack do
if view?
capture(&current_default_block)
else
instance_exec(&current_default_block)
end
end
end
ensure
__decrement_run_default_depth
end
end

Expand All @@ -119,5 +108,19 @@ def __with_unit_locals_stack(locals)
ensure
__unit_locals_stack.pop
end

def __without_default_stack
default = __defaults_stack.pop
yield
ensure
__defaults_stack << default
end

def __without_locals_stack
locals = __unit_locals_stack.pop
yield
ensure
__unit_locals_stack << locals
end
end
end
9 changes: 9 additions & 0 deletions spec/chanko/invoker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ module Chanko
"default"
end.should eq "outer_one.inner_one.outer_two.default.inner_two.outer_three"
end

context 'active_if is false' do
it "invokes given block as a fallback " do
Chanko::Loader.load("sensitive_inactive_unit")
controller.invoke(:sensitive_inactive_unit, :outer) do
invoke(:sensitive_inactive_unit, :inner)
end.should eq nil
end
end
end

context "when run_default is called but no block given" do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module SensitiveInactiveUnit
include Chanko::Unit
active_if { false }

scope(:controller) do
function(:outer) {}
function(:innner) {}
end
end

0 comments on commit 96ca782

Please sign in to comment.