class Noaidi::Matcher

Public Class Methods

new(pattern) click to toggle source
# File lib/noaidi/matcher.rb, line 3
def initialize(pattern)
  @pattern = pattern
end

Public Instance Methods

match?(value) click to toggle source
# File lib/noaidi/matcher.rb, line 7
def match?(value)
  case @pattern
  when Proc
    match_with_lambda(value)
  when Hash
    match_with_hash(value)
  else
    @pattern === value
  end
end
pattern_class() click to toggle source
# File lib/noaidi/matcher.rb, line 18
def pattern_class
  @pattern.class
end

Private Instance Methods

match_with_hash(value) click to toggle source

TODO: compile it at construction time

# File lib/noaidi/matcher.rb, line 25
def match_with_hash(value)
  return false unless value.is_a?(Hash)

  @pattern.map do |key, val|
    Matcher.new(val).match?(value[key])
  end.all?{ |x| x == true }
end
match_with_lambda(value) click to toggle source
# File lib/noaidi/matcher.rb, line 33
def match_with_lambda(value)
  @pattern.call(value)
end