Class: Contracts::Builtin::Optional
- Inherits:
-
CallableClass
- Object
- CallableClass
- Contracts::Builtin::Optional
- Defined in:
- lib/contracts/builtin_contracts.rb
Overview
Use this for specifying optional keyword argument Example: Optional[Num]
Constant Summary collapse
- UNABLE_TO_USE_OUTSIDE_OF_OPT_HASH =
"Unable to use Optional contract outside of KeywordArgs contract"
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(contract) ⇒ Optional
constructor
A new instance of Optional.
- #inspect ⇒ Object
- #to_s ⇒ Object
- #valid?(value) ⇒ Boolean
- #within_opt_hash! ⇒ Object
Methods inherited from CallableClass
Constructor Details
#initialize(contract) ⇒ Optional
Returns a new instance of Optional.
509 510 511 512 513 |
# File 'lib/contracts/builtin_contracts.rb', line 509 def initialize(contract) super() @contract = contract @within_opt_hash = false end |
Class Method Details
._valid?(hash, key, contract) ⇒ Boolean
502 503 504 505 506 507 |
# 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 |
Instance Method Details
#inspect ⇒ Object
529 530 531 |
# File 'lib/contracts/builtin_contracts.rb', line 529 def inspect to_s end |
#to_s ⇒ Object
525 526 527 |
# File 'lib/contracts/builtin_contracts.rb', line 525 def to_s "Optional[#{formatted_contract}]" end |
#valid?(value) ⇒ Boolean
520 521 522 523 |
# File 'lib/contracts/builtin_contracts.rb', line 520 def valid?(value) ensure_within_opt_hash Contract.valid?(value, contract) end |
#within_opt_hash! ⇒ Object
515 516 517 518 |
# File 'lib/contracts/builtin_contracts.rb', line 515 def within_opt_hash! @within_opt_hash = true self end |