module Deterministic::PatternMatching::Match
Constants
- Matcher
Public Class Methods
new(container, context)
click to toggle source
# File lib/deterministic/match.rb, line 15 def initialize(container, context) @container = container @context = context @collection = [] end
Public Instance Methods
any(value=nil, &result_block)
click to toggle source
catch-all
# File lib/deterministic/match.rb, line 29 def any(value=nil, &result_block) push(Object, value, result_block) end
call()
click to toggle source
# File lib/deterministic/match.rb, line 21 def call value = @container.respond_to?(:value) ? @container.value : nil matcher = @collection.detect { |m| m.matches?(value) } raise NoMatchError, "No match could be made for #{@container.inspect}" if matcher.nil? @context.instance_exec(value, &matcher.block) end
Private Instance Methods
compose_predicates(f, g)
click to toggle source
# File lib/deterministic/match.rb, line 52 def compose_predicates(f, g) ->(*args) { f[*args] && g[*args] } end
push(type, condition, result_block)
click to toggle source
# File lib/deterministic/match.rb, line 40 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 === @container.value } else ->(v) { @container.value == 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/deterministic/match.rb, line 57 def type_pred (->(type, x) { @container.is_a? type }).curry end