Class: NormalizedHash::Matchers::KeyNamesOfEnclosedHash
- Inherits:
-
KeyNameMatchers
- Object
- KeyNameMatchers
- NormalizedHash::Matchers::KeyNamesOfEnclosedHash
- Defined in:
- lib/normalized_hash/hash_enclosed_in_array.rb
Overview
RSpec::Matchers class for testing hashes enclosed in an array.
Array of Hashes: each Hash should have key :name or :<collection.singularize>
Hash here (subject) is an instance of Hash, which is an element of an Array, and Array itself is a value of a Hash:
{ :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
Test that all values of the tested Array are of the same class.
Methods inherited from KeyNameMatchers
Constructor Details
This class inherits a constructor from NormalizedHash::Matchers::KeyNameMatchers
Instance Method Details
#description ⇒ Object
50 51 52 |
# File 'lib/normalized_hash/hash_enclosed_in_array.rb', line 50 def description "have key :name or '#{@expectation.singularize}' in every enclosed Hash " end |
#failure_message_for_should ⇒ Object
72 73 74 |
# File 'lib/normalized_hash/hash_enclosed_in_array.rb', line 72 def "expected each of #{@target.map(&:keys)} include '#{@expectation.singularize}'" end |
#failure_message_for_should_not ⇒ Object
76 77 78 |
# File 'lib/normalized_hash/hash_enclosed_in_array.rb', line 76 def "expected #{@target.map(&:keys)} not include '#{@expectation.singularize}'" end |
#matches?(target) ⇒ Boolean
Test that all values of the tested Array are of the same class
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/normalized_hash/hash_enclosed_in_array.rb', line 59 def matches? target @target = target result = true target.each do |hash| next unless hash.is_a? Hash keys = hash.keys.map(&:to_s).map(&:downcase) result &&= false unless (keys.include? @expectation || keys.include?("name")) end result end |