Class: Contracts::Builtin::KeywordArgs

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

Overview

Use this for specifying contracts for keyword arguments Example: KeywordArgs[ e: Range, f: Optional[Num] ]

Instance Method Summary collapse

Methods inherited from CallableClass

[]

Constructor Details

#initialize(options) ⇒ KeywordArgs

Returns a new instance of KeywordArgs.



444
445
446
447
# File 'lib/contracts/builtin_contracts.rb', line 444

def initialize(options)
  super()
  @options = options
end

Instance Method Details

#inspectObject



462
463
464
# File 'lib/contracts/builtin_contracts.rb', line 462

def inspect
  to_s
end

#to_sObject



458
459
460
# File 'lib/contracts/builtin_contracts.rb', line 458

def to_s
  "KeywordArgs[#{options}]"
end

#valid?(hash) ⇒ Boolean

Returns:

  • (Boolean)


449
450
451
452
453
454
455
456
# File 'lib/contracts/builtin_contracts.rb', line 449

def valid?(hash)
  return false unless hash.is_a?(Hash)
  return false unless hash.keys - options.keys == []

  options.all? do |key, contract|
    Optional._valid?(hash, key, contract)
  end
end