class Tzu::Match
with thanks to github.com/pzol/deterministic
Constants
- Matcher
Public Class Methods
new(outcome, context)
click to toggle source
# File lib/tzu/match.rb, line 5 def initialize(outcome, context) @outcome, @context, @collection = outcome, context, [] end
Public Instance Methods
invalid(&result_block)
click to toggle source
todo: hash and define_method if more overrides identified
# File lib/tzu/match.rb, line 22 def invalid(&result_block) push('failure', :validation, result_block) end
result()
click to toggle source
# File lib/tzu/match.rb, line 9 def result matcher = @collection.detect { |m| m.matches?(@outcome.type) } raise "No match could be made for #{@outcome}" if matcher.nil? @context.instance_exec(@outcome.result, &matcher.block) end
Private Instance Methods
compose_predicates(f, g)
click to toggle source
# File lib/tzu/match.rb, line 46 def compose_predicates(f, g) ->(*args) { f[*args] && g[*args] } end
push(type, condition, result_block)
click to toggle source
# File lib/tzu/match.rb, line 34 def push(type, condition, result_block) condition_pred = case when condition.nil?; ->(v) { true } when condition.is_a?(Proc); condition when condition.is_a?(Class); ->(v) { condition === @outcome.type } else ->(v) { @outcome.type == condition } end matcher_pred = compose_predicates(type_pred[type], condition_pred) @collection << Matcher.new(matcher_pred, result_block) end
type_pred()
click to toggle source
return a partial function for matching a matcher's type
# File lib/tzu/match.rb, line 51 def type_pred (->(type, x) { @outcome.send("#{type.to_s}?") }).curry end