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.



425
426
427
# File 'lib/contracts/builtin_contracts.rb', line 425

def initialize(options)
  @options = options
end

Instance Method Details

#inspectObject



441
442
443
# File 'lib/contracts/builtin_contracts.rb', line 441

def inspect
  to_s
end

#to_sObject



437
438
439
# File 'lib/contracts/builtin_contracts.rb', line 437

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

#valid?(hash) ⇒ Boolean

Returns:

  • (Boolean)


429
430
431
432
433
434
435
# File 'lib/contracts/builtin_contracts.rb', line 429

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