Class: Squeeze::Matcher

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

Overview

Matchers are initialized with a pattern, to be used for retrieval in a HashTree. What makes Matcher special, really the only reason it exists, is that it overrides #eql? and #hash so that Hashes will treat two Matchers as the same object if their patterns are the same. This prevents redundancy when a HashTree creates its internal tree.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern) ⇒ Matcher

Returns a new instance of Matcher.



11
12
13
# File 'lib/squeeze/matcher.rb', line 11

def initialize(pattern)
  @pattern = pattern || true
end

Instance Attribute Details

#patternObject (readonly)

Returns the value of attribute pattern.



10
11
12
# File 'lib/squeeze/matcher.rb', line 10

def pattern
  @pattern
end

Instance Method Details

#call(val) ⇒ Object



15
16
17
# File 'lib/squeeze/matcher.rb', line 15

def call(val)
  @pattern == val || @pattern == true
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/squeeze/matcher.rb', line 19

def eql?(other)
  other.kind_of?(self.class) && other.pattern == @pattern
end

#hashObject



23
24
25
# File 'lib/squeeze/matcher.rb', line 23

def hash
  @pattern.hash
end