Class: Contracts::Builtin::StrictHash

Inherits:
CallableClass show all
Defined in:
lib/contracts/builtin_contracts.rb

Overview

Use this to specify the Hash characteristics. This contracts fails if there are any extra keys that don’t have contracts on them. Example: StrictHash[{ a: String, b: Bool }]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from CallableClass

[]

Constructor Details

#initialize(contract_hash) ⇒ StrictHash

Returns a new instance of StrictHash.



409
410
411
# File 'lib/contracts/builtin_contracts.rb', line 409

def initialize(contract_hash)
  @contract_hash = contract_hash
end

Instance Attribute Details

#contract_hashObject (readonly)

Returns the value of attribute contract_hash.



407
408
409
# File 'lib/contracts/builtin_contracts.rb', line 407

def contract_hash
  @contract_hash
end

Instance Method Details

#valid?(arg) ⇒ Boolean

Returns:

  • (Boolean)


413
414
415
416
417
418
419
# File 'lib/contracts/builtin_contracts.rb', line 413

def valid?(arg)
  return false unless arg.is_a?(Hash)

  contract_hash.all? do |key, _v|
    contract_hash.key?(key) && Contract.valid?(arg[key], contract_hash[key])
  end
end