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.



42
43
44
45
# File 'lib/method_pattern.rb', line 42

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

Instance Attribute Details

#acceptedObject (readonly)

Returns the value of attribute accepted.



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

def accepted
  @accepted
end

#blockObject (readonly)

Returns the value of attribute block.



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

def block
  @block
end

Instance Method Details

#match?(args) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
# File 'lib/method_pattern.rb', line 47

def match? args
  @accepted.each_with_index do |pattern, index|
    return false unless match_arg?(pattern, args[index])
  end

  true
end

#match_arg?(pattern, arg) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/method_pattern.rb', line 55

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

    pattern.each do |key, value|
      return false unless arg.key?(key) && match_arg?(value, arg[key])
    end

    true
  else
    pattern === arg
  end
end