Class: NormalizedHash::Matchers::KeyNamesOfEnclosedHash

Inherits:
KeyNameMatchers show all
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

Methods inherited from KeyNameMatchers

#initialize

Constructor Details

This class inherits a constructor from NormalizedHash::Matchers::KeyNameMatchers

Instance Method Details

#descriptionObject



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_shouldObject



72
73
74
# File 'lib/normalized_hash/hash_enclosed_in_array.rb', line 72

def failure_message_for_should
  "expected each of #{@target.map(&:keys)} include '#{@expectation.singularize}'"
end

#failure_message_for_should_notObject



76
77
78
# File 'lib/normalized_hash/hash_enclosed_in_array.rb', line 76

def failure_message_for_should_not
  "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

Parameters:

  • target (Array)

    tested array

Returns:

  • (Boolean)

    true if pass, false if fail



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