Skip to content

Commit

Permalink
Added this_context
Browse files Browse the repository at this point in the history
  • Loading branch information
auser committed Mar 19, 2009
1 parent df11571 commit 1692ccb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/parenting/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ def run_in_context(&block)
@parent = parent

context_stack.push self
instance_eval &block if block
this_context.instance_eval &block if block
context_stack.pop
head
end

def head
context_stack.first
end
def context
context_stack.last
def this_context
@this_context ||= context_stack.last
end
def parent
@parent ||= current_context[-1] == self ? current_context[-2] : current_context[-1]
Expand Down
6 changes: 6 additions & 0 deletions test/test_parenting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,11 @@ class QuickieTest < Test::Unit::TestCase
$d.parent.should == @a.b.c
$d.parent.parent.parent.should == @a
end
it "should have the latest context set as the last item in the stack" do
@a.b.c.this_context.nil?.should == false
@a.b.this_context.should == @a.b
@a.b.c.this_context.should == @a.b.c
@a.b.c.d.this_context.should == @a.b.c.d
end
end
end

0 comments on commit 1692ccb

Please sign in to comment.