class Noaidi::Matchmaker
Constants
- NoMatchError
Public Class Methods
new(block)
click to toggle source
# File lib/noaidi/matchmaker.rb, line 13 def initialize(block) @branches = process_block(block) end
Public Instance Methods
call(value)
click to toggle source
# File lib/noaidi/matchmaker.rb, line 17 def call(value) values = Array(value) @branches.each do |matchers, block| return block.call(*meaningful_values(matchers, values)) if matches?(matchers, values) end raise NoMatchError, "No match for #{@values.inspect}" end
Private Instance Methods
matches?(matchers, values)
click to toggle source
# File lib/noaidi/matchmaker.rb, line 38 def matches?(matchers, values) return false unless values.length == matchers.length values.each_with_index do |value, i| return false unless matchers[i].match?(value) end true end
meaningful_values(matchers, values)
click to toggle source
# File lib/noaidi/matchmaker.rb, line 30 def meaningful_values(matchers, values) values.zip(matchers).select do |i| value = i[0] matcher = i[1] !matcher.nil? && (matcher.pattern_class == Class || value.class != matcher.pattern_class) end.map{|i| i[0]} end
process_block(block)
click to toggle source
# File lib/noaidi/matchmaker.rb, line 26 def process_block(block) block.call(Parser.new) end