module Match

Public Instance Methods

match?(lhs, rhs) click to toggle source
# File lib/match.rb, line 2
def match?(lhs, rhs)
  return true if lhs.is_a?(RuboClaus::CatchAll)
  return false if lhs.args.length != rhs.length
  any_match?(lhs.args, rhs)
end

Private Instance Methods

any_match?(lhs, rhs) click to toggle source
# File lib/match.rb, line 21
        def any_match?(lhs, rhs)
  return true if single_match?(lhs, rhs)
  deep_match?(lhs, rhs)
end
deep_match?(lhs, rhs) click to toggle source
# File lib/match.rb, line 8
        def deep_match?(lhs, rhs)
  return match_array(lhs, rhs) if [lhs, rhs].all? { |side| side.is_a? Array }
  return match_hash(lhs, rhs) if [lhs, rhs].all? { |side| side.is_a? Hash }
  false
end
handle_destructuring(lhs, rhs) click to toggle source
# File lib/match.rb, line 39
        def handle_destructuring(lhs, rhs)
  [lhs, rhs.select {|k, v| lhs.keys.include? k }]
end
head_tail_destructuring_match(lhs, rhs) click to toggle source
# File lib/match.rb, line 51
        def head_tail_destructuring_match(lhs, rhs)
  any_match?(lhs[0..-2], rhs[0..(lhs[0..-2].size - 1)])
end
keys_match?(lhs, rhs) click to toggle source
# File lib/match.rb, line 43
        def keys_match?(lhs, rhs)
  lhs.keys.sort == rhs.keys.sort
end
match_array(lhs, rhs) click to toggle source
# File lib/match.rb, line 26
        def match_array(lhs, rhs)
  return head_tail_destructuring_match(lhs, rhs) if lhs.include?(:tail)
  return false if lhs.length != rhs.length
  lhs.zip(rhs) { |array| return false unless any_match?(*array) } || true
end
match_hash(lhs, rhs) click to toggle source
# File lib/match.rb, line 32
        def match_hash(lhs, rhs)
  lhs, rhs = handle_destructuring(lhs, rhs) if lhs.keys.length < rhs.keys.length
  return false unless keys_match?(lhs, rhs)
  return false unless values_match?(lhs, rhs)
  true
end
single_match?(lhs, rhs) click to toggle source
# File lib/match.rb, line 14
        def single_match?(lhs, rhs)
  return true if lhs == :any
  return true if lhs == rhs
  return true if lhs == rhs.class
  false
end
values_match?(lhs, rhs) click to toggle source
# File lib/match.rb, line 47
        def values_match?(lhs, rhs)
  any_match?(lhs.values, rhs.values)
end