Skip to content

Commit

Permalink
Rename PatternCollector -> FunctionDefinition
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshCheek committed Jan 20, 2015
1 parent e3281e6 commit f670159
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions lib/overloaded_methods.rb
@@ -1,10 +1,10 @@
module OverloadedMethods

def overload_method name
pattern_collector = PatternCollector.new
yield pattern_collector
collector = FunctionDefinition.new
yield collector
define_method name do |*params|
pattern_collector.execute params
collector.execute params
end
end

Expand All @@ -23,28 +23,29 @@ def capture &block
@block = block
end
end
class PatternCollector

class FunctionDefinition
def initialize
@patterns = []
@clauses = []
end
def pattern &block
capture &block
def pattern &predicate
capture &predicate
end
def when &block
capture &block
def when &predicate
capture &predicate
end
def execute params
matching_pattern = @patterns.find{|predicate, block| predicate.call *params}
matching_pattern = @clauses.find{|predicate, _| predicate.call *params}
return matching_pattern[1].execute(params) unless matching_pattern.nil?
return @default.call(*params) unless @default.nil?
end
def default &block
@default = block
end
private
def capture &block
def capture &predicate
b = BlockCollector.new
@patterns << [block, b]
@clauses << [predicate, b]
b
end
end
Expand Down

0 comments on commit f670159

Please sign in to comment.