Class: Contracts::ImmutableHashOf

Inherits:
Callable
  • Object
show all
Defined in:
middleman-core/lib/middleman-core/contracts.rb,
middleman-core/lib/middleman-core/contracts.rb

Constant Summary collapse

INVALID_KEY_VALUE_PAIR =
'You should provide only one key-value pair to HashOf contract'.freeze

Instance Method Summary collapse

Methods inherited from Callable

[]

Constructor Details

#initialize(key, value) ⇒ ImmutableHashOf

Returns a new instance of ImmutableHashOf



26
27
28
29
# File 'middleman-core/lib/middleman-core/contracts.rb', line 26

def initialize(key, value)
  @key   = key
  @value = value
end

Instance Method Details

#to_sObject



40
41
42
# File 'middleman-core/lib/middleman-core/contracts.rb', line 40

def to_s
  "ImmutableHash<#{@key}, #{@value}>"
end

#valid?(hash) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
# File 'middleman-core/lib/middleman-core/contracts.rb', line 31

def valid?(hash)
  return false unless hash.is_a?(::Hamster::Hash)

  keys_match = hash.keys.map { |k| Contract.valid?(k, @key) }.all?
  vals_match = hash.values.map { |v| Contract.valid?(v, @value) }.all?

  [keys_match, vals_match].all?
end