Class: Shaped::Shapes::Hash

Inherits:
Shaped::Shape show all
Defined in:
lib/shaped/shapes/hash.rb

Instance Method Summary collapse

Constructor Details

#initialize(shape_description) ⇒ Hash

Returns a new instance of Hash.



4
5
6
7
8
9
10
# File 'lib/shaped/shapes/hash.rb', line 4

def initialize(shape_description)
  unless shape_description.is_a?(Hash)
    raise(Shaped::InvalidShapeDescription, "A #{self.class} description must be a Hash.")
  end

  @hash_description = shape_description.transform_values { |value| Shaped::Shape(value) }
end

Instance Method Details

#matched_by?(hash) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
# File 'lib/shaped/shapes/hash.rb', line 12

def matched_by?(hash)
  return false if !hash.is_a?(Hash)

  @hash_description.all? do |key, expected_value_shape|
    expected_value_shape.matched_by?(hash[key])
  end
end

#to_sObject



20
21
22
23
24
25
26
27
# File 'lib/shaped/shapes/hash.rb', line 20

def to_s
  printable_shape_description =
    @hash_description.map do |key, value|
      "#{key.inspect} => #{value}"
    end.join(', ')

  "{ #{printable_shape_description} }"
end