Skip to content

Commit

Permalink
support chaining within trigger groups, refs #22
Browse files Browse the repository at this point in the history
  • Loading branch information
jenseng committed Apr 1, 2014
1 parent a4d74e6 commit 7418320
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/hair_trigger/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def all_names

def self.chainable_methods(*methods)
methods.each do |method|
class_eval <<-METHOD
class_eval <<-METHOD, __FILE__, __LINE__ + 1
alias #{method}_orig #{method}
def #{method}(*args)
@chained_calls << :#{method}
Expand All @@ -128,7 +128,7 @@ def set_#{method}(*args)
@chained_calls.pop # the subtrigger will get this, we don't need it
@chained_calls = @chained_calls.uniq
@triggers << trigger = clone
trigger.#{method}(*args, &Proc.new)
trigger.#{method}(*args, &(block_given? ? Proc.new : nil))
else
#{method}_orig(*args)
maybe_execute(&Proc.new) if block_given?
Expand Down
13 changes: 13 additions & 0 deletions spec/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ def builder(name = nil)
end
end

describe "groups" do
it "should allow chained methods" do
triggers = builder.on(:foos){ |t|
t.where('bar=1').name('bar'){ 'BAR;' }
t.where('baz=1').name('baz'){ 'BAZ;' }
}.triggers
triggers.map(&:prepare!)
triggers.map(&:prepared_name).should == ['bar', 'baz']
triggers.map(&:prepared_where).should == ['bar=1', 'baz=1']
triggers.map(&:prepared_actions).should == ['BAR;', 'BAZ;']
end
end

context "adapter-specific actions" do
before(:each) do
@adapter = MockAdapter.new("mysql")
Expand Down

0 comments on commit 7418320

Please sign in to comment.