Skip to content

Commit

Permalink
allow reopening
Browse files Browse the repository at this point in the history
  • Loading branch information
lawrencepit committed Jun 14, 2008
1 parent 512a2e6 commit 0d5c9c1
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 14 deletions.
2 changes: 1 addition & 1 deletion doc/README
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ You can also define Functor objects directly:
fib = Functor.new do
given( 0 ) { 0 }
given( 1 ) { 1 }
given( Integer ) { |n| self.call( n - 1 ) + self.call( n - 2 ) }
given( 2..10000 ) { |n| self.call( n - 1 ) + self.call( n - 2 ) }
end

You can use functors directly with functions taking a block like this:
Expand Down
7 changes: 3 additions & 4 deletions lib/functor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
class Functor

module Method

def self.included( k )
def k.functor( name, *args, &block )
functors = module_eval { @__functors ||= {} }
Expand All @@ -24,14 +23,14 @@ def #{name}( *args, &block )
end
CODE
end
functors[ name ].given( *args, &block )
functors[ name ].given( *args, &block )
end
end
end

def initialize( &block ) ; @patterns = []; instance_eval(&block) if block_given? ; end
def initialize( &block ) ; @patterns = {}; instance_eval(&block) if block_given? ; end

def given( *pattern, &block ) ; @patterns.push [ pattern, block ] ; self ; end
def given( *pattern, &block ) ; @patterns[ pattern ] = block ; self ; end

def bind( object ) ; @object = object ; self ; end

Expand Down
2 changes: 1 addition & 1 deletion test/fib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
fib ||= Functor.new do
given( 0 ) { 0 }
given( 1 ) { 1 }
given( Integer ) { |n| self.call( n - 1 ) + self.call( n - 2 ) }
given( Class.new { def ===(v) ; v > 1 ; end }.new ) { |n| self.call( n - 1 ) + self.call( n - 2 ) }
end

describe "Dispatch on a functor object should" do
Expand Down
13 changes: 5 additions & 8 deletions test/inheritance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class A
include Functor::Method
functor( :smurf, Integer ) { |x| "A: Integer" }
functor( :smurf, String ) { |s| "A: String" }
functor( :smurf, Symbol ) { |s| smurf( "boo" ) }
end

class B < A
Expand All @@ -12,17 +13,13 @@ class B < A

describe "Functor methods should support inheritance" do

before do
@b = B.new
end

specify "by inheriting base class implementations" do
@b.smurf( 5 ).should == "A: Integer"
B.new.smurf( 5 ).should == "A: Integer"
end

specify "by allowing derived classes to override an implementation" do
@b.smurf( "foo" ).should == "B: String"
B.new.smurf( "foo" ).should == "B: String"
A.new.smurf( :foo ).should == "A: String"
B.new.smurf( :foo ).should == "B: String"
end

end

0 comments on commit 0d5c9c1

Please sign in to comment.