Class: MethodPattern::PatternMatchedFunction::Pattern
- Inherits:
-
Object
- Object
- MethodPattern::PatternMatchedFunction::Pattern
- Defined in:
- lib/method_pattern.rb
Instance Attribute Summary collapse
-
#accepted ⇒ Object
readonly
Returns the value of attribute accepted.
-
#block ⇒ Object
readonly
Returns the value of attribute block.
Instance Method Summary collapse
-
#initialize(*accepted, block) ⇒ Pattern
constructor
A new instance of Pattern.
- #match?(*args) ⇒ Boolean
- #match_arg?(pattern, arg) ⇒ Boolean
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
#accepted ⇒ Object (readonly)
Returns the value of attribute accepted.
38 39 40 |
# File 'lib/method_pattern.rb', line 38 def accepted @accepted end |
#block ⇒ Object (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
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
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 |