Skip to content

Commit

Permalink
creating shared branches for our trees
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Allen <ryan@yeahnah.org>
  • Loading branch information
joho authored and ryan-allen committed Jan 21, 2009
1 parent 3323cc7 commit 057726d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lumberjack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ def self.construct(initial_scope = [], &block)
builder.__process(block)
end

@@methods_to_keep = /^__/, /class/, /instance_eval/, /method_missing/
@@methods_to_keep = /^__/, /class/, /instance_eval/, /method_missing/,
/instance_variable_(g|s)et/

instance_methods.each do |m|
undef_method m unless @@methods_to_keep.find { |r| r.match m }
Expand Down Expand Up @@ -34,6 +35,16 @@ def load_tree_file(filename)
end
end

def shared_branch(branch_name, &block)
instance_variable_set "@#{branch_name}", lambda(&block)
end

def graft_branch(branch_name)
branch = instance_variable_get("@#{branch_name}")
raise "Attemption to graft branch #{branch_name} which is undefined" unless branch
instance_eval(&branch)
end

def method_missing(*args, &block)
# if we only have one arg, and no block, then we're trying to build a
# module scope, i.e. a/b/c/d would resolve to A::B::C::D, so let's start
Expand Down
48 changes: 48 additions & 0 deletions lumberjack_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,52 @@ def test_we_can_load_in_other_files
assert_equal 10, family.first.members.last.age

end

def test_we_can_share_branches_that_are_defined
families = Lumberjack.construct do

shared_branch :kids do
person 'Jack', 11
person 'Jill', 10
end

family "Dad's new family" do
members do
person 'Dad', 45
graft_branch :kids
end
end

family "Mum's new family" do
members do
person 'Mum', 40
person 'Red-headed step-child', 8
graft_branch :kids
end
end
end

assert 2, families.length
assert_kind_of Family, families[0]
assert_kind_of Family, families[1]

assert 3, families[0].members.size
assert families[0].members.any? {|m| m.given_name == 'Jack'}
assert families[0].members.any? {|m| m.given_name == 'Jill'}

assert 4, families[1].members.size
assert families[1].members.any? {|m| m.given_name == 'Jack'}
assert families[1].members.any? {|m| m.given_name == 'Jill'}
end

def test_we_cant_share_branches_that_are_undefined
# TODO: why does this output funny stuff
assert_raise RuntimeError do
Lumberjack.construct do
family 'wont work' do
graft_branch :non_existant
end
end
end
end
end

0 comments on commit 057726d

Please sign in to comment.