From 714388d225fe79c61b7776cd494b5ca700c27aea Mon Sep 17 00:00:00 2001 From: Joe Ferris Date: Tue, 2 Dec 2008 20:20:30 -0500 Subject: [PATCH] A consequence can now be converted to a Proc that will run the appropriate method with the given arguments --- lib/adherence/consequence.rb | 8 ++++++++ spec/adherence/consequence_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/adherence/consequence.rb b/lib/adherence/consequence.rb index bfbbb37..7cf72b8 100644 --- a/lib/adherence/consequence.rb +++ b/lib/adherence/consequence.rb @@ -6,6 +6,14 @@ def initialize(options) assert_valid_options!(options) end + def to_proc + method = self.method + args = self.args + lambda do + send(method, *args) + end + end + private def assert_valid_options!(options) #:nodoc: diff --git a/spec/adherence/consequence_spec.rb b/spec/adherence/consequence_spec.rb index 1302eb1..699e80f 100644 --- a/spec/adherence/consequence_spec.rb +++ b/spec/adherence/consequence_spec.rb @@ -42,4 +42,24 @@ def create_consequence end end end + + describe Consequence do + before do + @args = [:one, :two] + @consequence = Consequence.new(:method => :example, :args => @args) + end + + it "should call its method with the given args when run" do + klass = Class.new + klass.class_eval do + attr_accessor :args + def example(*args) + @args = args + end + end + object = klass.new + object.instance_eval(&@consequence) + object.args.should == @args + end + end end