Class: MethodPattern::PatternMatchedFunction::Pattern

Inherits:
Object
  • Object
show all
Defined in:
lib/method_pattern.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*accepted, block) ⇒ Pattern

Returns a new instance of Pattern.



40
41
42
43
# File 'lib/method_pattern.rb', line 40

def initialize *accepted, block
  @accepted = accepted
  @block = block
end

Instance Attribute Details

#acceptedObject (readonly)

Returns the value of attribute accepted.



38
39
40
# File 'lib/method_pattern.rb', line 38

def accepted
  @accepted
end

#blockObject (readonly)

Returns the value of attribute block.



38
39
40
# File 'lib/method_pattern.rb', line 38

def block
  @block
end

Instance Method Details

#match?(*args) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/method_pattern.rb', line 45

def match? *args
  @accepted.each_with_index.reduce(true) do |result, (pattern, index)|
    result && match_arg?(pattern, args[index])
  end
end

#match_arg?(pattern, arg) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/method_pattern.rb', line 51

def match_arg? pattern, arg
  case pattern
  when Hash
    return false unless arg.is_a? Hash

    pattern.reduce(true) do |result, (key, value)|
      result && arg.key?(key) && match_arg?(value, arg[key])
    end
  else
    pattern === arg
  end
end