Class: NormalizedHash::Matchers::HashEnclosingArray
- Inherits:
-
Object
- Object
- NormalizedHash::Matchers::HashEnclosingArray
- Defined in:
- lib/normalized_hash/hash_enclosed_in_array.rb
Overview
Hash (subject) ancloses Array wit ha bunch of Hases inside. Each aHas is then tested by KeyNamesOfEnclosedHash
{ :foos => <--- subject
[
{ :foo (or :name) => ...,
:bar => ...,
},
{ :foo (or :name) => ...,
:bar => ...,
},
]
}
Instance Method Summary collapse
- #description ⇒ Object
- #failure_message_for_should ⇒ Object
- #failure_message_for_should_not ⇒ Object
-
#matches?(target) ⇒ Boolean
Run matcher only if target is Hash with Array embedded at the first depth level.
Instance Method Details
#description ⇒ Object
96 97 98 |
# File 'lib/normalized_hash/hash_enclosed_in_array.rb', line 96 def description @description end |
#failure_message_for_should ⇒ Object
100 101 102 |
# File 'lib/normalized_hash/hash_enclosed_in_array.rb', line 100 def [@target.inspect, description, @failure].join ' ' end |
#failure_message_for_should_not ⇒ Object
104 105 106 |
# File 'lib/normalized_hash/hash_enclosed_in_array.rb', line 104 def [@target.inspect, 'not', description, @failure].join ' ' end |
#matches?(target) ⇒ Boolean
Run matcher only if target is Hash with Array embedded at the first depth level. Not recursive, recursion should be implemented on level higher.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/normalized_hash/hash_enclosed_in_array.rb', line 112 def matches? target result = true @target = target if (target.is_a?(Hash) && target.values.map(&:class).include?(Array)) @description = "enclose Arrays of Hashes with key #{@target.keys}" @failure = ":\n" target.each do |key,array| if array.is_a? Array @checked = array res = KeyNamesOfEnclosedHash.new(key).matches?(array) result &&= res @failure << { key => array}.inspect unless res end end else @description = "*** spec did not run *** " result = false end result end |