dyoder-functor 0.4.1 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/functor.rb +11 -14
- metadata +1 -1
data/lib/functor.rb
CHANGED
@@ -11,7 +11,7 @@ class Functor
|
|
11
11
|
klass = self.name ; module_eval <<-CODE
|
12
12
|
def #{name}( *args, &block )
|
13
13
|
begin
|
14
|
-
|
14
|
+
#{klass}.functors[ :#{name} ].apply( self, *args, &block )
|
15
15
|
rescue ArgumentError => e
|
16
16
|
begin
|
17
17
|
super
|
@@ -19,11 +19,6 @@ class Functor
|
|
19
19
|
raise e
|
20
20
|
end
|
21
21
|
end
|
22
|
-
if rval.is_a? Continuation
|
23
|
-
super ; rval.call
|
24
|
-
else
|
25
|
-
rval
|
26
|
-
end
|
27
22
|
end
|
28
23
|
CODE
|
29
24
|
end
|
@@ -42,23 +37,21 @@ class Functor
|
|
42
37
|
end
|
43
38
|
|
44
39
|
def apply( object, *args, &block )
|
45
|
-
object.instance_exec( *args, &match(
|
40
|
+
object.instance_exec( *args, &match( args, &block ) )
|
46
41
|
end
|
47
42
|
|
48
43
|
def call( *args, &block )
|
49
|
-
|
50
|
-
match( *args, &block ).call( *args )
|
44
|
+
match( args, &block ).call( *args )
|
51
45
|
end
|
52
46
|
|
53
47
|
def to_proc ; lambda { |*args| self.call( *args ) } ; end
|
54
48
|
|
55
49
|
private
|
56
50
|
|
57
|
-
def match(
|
58
|
-
args
|
59
|
-
pattern, action = @rules.find { |
|
60
|
-
|
61
|
-
return action
|
51
|
+
def match( args, &block )
|
52
|
+
args << block if block_given?
|
53
|
+
pattern, action = @rules.find { | p, a | match?( args, p ) }
|
54
|
+
action or raise argument_error( args )
|
62
55
|
end
|
63
56
|
|
64
57
|
def match?( args, pattern )
|
@@ -69,4 +62,8 @@ class Functor
|
|
69
62
|
( rule.is_a?( Proc ) and rule.call( arg ) ) or rule === arg or rule == arg
|
70
63
|
end
|
71
64
|
|
65
|
+
def argument_error( args )
|
66
|
+
ArgumentError.new( "argument mismatch for argument(s): #{ args.map{ |arg| arg.inspect }.join(', ') }." )
|
67
|
+
end
|
68
|
+
|
72
69
|
end
|