Class: NormalizedHash::Matchers::HashEnclosingArray

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

Instance Method Details

#descriptionObject



96
97
98
# File 'lib/normalized_hash/hash_enclosed_in_array.rb', line 96

def description
  @description
end

#failure_message_for_shouldObject



100
101
102
# File 'lib/normalized_hash/hash_enclosed_in_array.rb', line 100

def failure_message_for_should
  [@target.inspect, description, @failure].join ' '
end

#failure_message_for_should_notObject



104
105
106
# File 'lib/normalized_hash/hash_enclosed_in_array.rb', line 104

def failure_message_for_should_not
  [@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.

Returns:

  • (Boolean)


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