Class: DeepClassCompare::HashMatcher

Inherits:
Matcher show all
Includes:
ContainerComparable
Defined in:
lib/deep_class_compare/hash_matcher.rb

Instance Method Summary collapse

Methods inherited from Matcher

build

Constructor Details

#initialize(klass = Hash) ⇒ HashMatcher

Returns a new instance of HashMatcher.



4
5
6
# File 'lib/deep_class_compare/hash_matcher.rb', line 4

def initialize(klass = Hash)
  super(klass)
end

Instance Method Details

#build_chain(key_comparable, value_comparable = nil) ⇒ Object



19
20
21
22
23
# File 'lib/deep_class_compare/hash_matcher.rb', line 19

def build_chain(key_comparable, value_comparable = nil)
  key_comparable, value_comparable = Object, key_comparable if value_comparable.nil?
  @key_chain = parse_comparable_to_chain(key_comparable)
  @value_chain = parse_comparable_to_chain(value_comparable)
end

#match?(hash) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/deep_class_compare/hash_matcher.rb', line 8

def match?(hash)
  super && compare_hash_with_chain(hash)
end

#of(key_comparable, value_comparable = nil) ⇒ Object



12
13
14
15
16
17
# File 'lib/deep_class_compare/hash_matcher.rb', line 12

def of(key_comparable, value_comparable = nil)
  raise_pattern_error! unless @key_chain.nil? && @value_chain.nil?
  dup.tap do |matcher|
    matcher.build_chain(key_comparable, value_comparable)
  end
end