lib/patternmatching/pattern.rb in patternmatching-0.2.4 vs lib/patternmatching/pattern.rb in patternmatching-0.2.5
- old
+ new
@@ -1,8 +1,8 @@
module PatternMatching
- # Domain Specific Language style methods for inside block
+ #Domain Specific Language style methods for inside block
module DSL_INSIDE
#A pattern matches description inside block
#=== Usage
# seems as {some pattern...} do ... end
def as(&block)
@@ -23,10 +23,11 @@
proc {_}
end
end
include DSL_INSIDE
+ #Domain Specific Language style methods for inside module
module DSL_MODULE
#Define method as partial style
#=== Usage
# func(NAME).seems as {PATTERN_1} do ACTION_1 end
# func(NAME).seems as {PATTERN_2} do ACTION_2 end
@@ -44,11 +45,16 @@
pattern_name = ("@_pattern_" + name.to_s ).to_sym
unless method_defined?(name)
patterns = []
instance_variable_set(pattern_name, patterns)
define_method(name) do |target|
- MatchExec.exec_as(target, patterns, self)
+ result = MatchExec.exec_as(target, patterns, self)
+ if result
+ result[0].instance_eval(&result[1])
+ else
+ nil
+ end
end
end
patterns = instance_variable_get(pattern_name)
fragments = PatternFragments.new(patterns)
if block
@@ -57,11 +63,11 @@
fragments
end
end
include DSL_MODULE
- # Domain Specific Language style methods for outside
+ #Domain Specific Language style methods for outside
module DSL_OUTSIDE
#Build structured data
#=== Usage
# build {[foo(bar, 100), foo(buzz, "abc")]}
def build(&block)
@@ -76,11 +82,16 @@
# seems something do ACTION_DEFAULT end
# end
def make(target, &block)
patterns = []
PatternFragments.new(patterns).instance_eval(&block)
- MatchExec.exec_as(target, patterns, self)
+ result = MatchExec.exec_as(target, patterns, self)
+ if result
+ result[0].instance_eval(&result[1])
+ else
+ nil
+ end
end
end
include DSL_OUTSIDE
extend DSL_OUTSIDE
@@ -204,15 +215,15 @@
rescue NotMatched
next
end
executer = ExecuteAs.new(args, receiver)
next if condition != nil and not executer.instance_eval(&condition)
- return executer.instance_eval(&action)
+ return [executer, action]
end
nil
end
- # Private class to access instance valiables of the receiver
+ #Private class to access instance valiables of the receiver
class InstanceVariableAccessor
def initialize(receiver)
@receiver = receiver
end
private