class Contracts::Builtin::Optional
Use this for specifying optional keyword argument Example: Optional[Num]
Constants
- UNABLE_TO_USE_OUTSIDE_OF_OPT_HASH
Attributes
Public Class Methods
Source
# File lib/contracts/builtin_contracts.rb, line 502 def self._valid?(hash, key, contract) return Contract.valid?(hash[key], contract) unless contract.is_a?(Optional) contract.within_opt_hash! !hash.key?(key) || Contract.valid?(hash[key], contract) end
Source
# File lib/contracts/builtin_contracts.rb, line 509 def initialize(contract) super() @contract = contract @within_opt_hash = false end
Calls superclass method
Public Instance Methods
Source
# File lib/contracts/builtin_contracts.rb, line 525 def to_s "Optional[#{formatted_contract}]" end
Source
# File lib/contracts/builtin_contracts.rb, line 520 def valid?(value) ensure_within_opt_hash Contract.valid?(value, contract) end
Source
# File lib/contracts/builtin_contracts.rb, line 515 def within_opt_hash! @within_opt_hash = true self end
Private Instance Methods
Source
# File lib/contracts/builtin_contracts.rb, line 537 def ensure_within_opt_hash return if within_opt_hash fail ArgumentError, UNABLE_TO_USE_OUTSIDE_OF_OPT_HASH end
Source
# File lib/contracts/builtin_contracts.rb, line 543 def formatted_contract Formatters::InspectWrapper.create(contract) end