Class: Type::Definition::Nilable
- Inherits:
-
Object
- Object
- Type::Definition::Nilable
- Includes:
- Type::Definition
- Defined in:
- lib/type/definition/nilable.rb
Overview
Nilable Type::Definitions are the same as their non-nilable counterparts with the following exceptions:
-
a ‘nil` value is considered valid
-
a ‘nil` value is returned without casting
Instance Attribute Summary
Attributes included from Type::Definition
Instance Method Summary collapse
-
#cast!(input) ⇒ Object
Casts the input unless it is nil.
-
#initialize(parent) ⇒ Nilable
constructor
A new instance of Nilable.
- #nilable ⇒ self
- #nilable? ⇒ True
- #to_s ⇒ String
-
#valid?(input, *args) ⇒ Boolean
Returns true if input is nil or the input is valid.
Methods included from Type::Definition
Constructor Details
#initialize(parent) ⇒ Nilable
Returns a new instance of Nilable.
23 24 25 |
# File 'lib/type/definition/nilable.rb', line 23 def initialize(parent) super(nil, parent) end |
Instance Method Details
#cast!(input) ⇒ Object
Casts the input unless it is nil
33 34 35 36 |
# File 'lib/type/definition/nilable.rb', line 33 def cast!(input) return nil if input.nil? super end |
#nilable ⇒ self
44 45 46 |
# File 'lib/type/definition/nilable.rb', line 44 def nilable self end |
#nilable? ⇒ True
39 40 41 |
# File 'lib/type/definition/nilable.rb', line 39 def nilable? true end |
#to_s ⇒ String
49 50 51 52 |
# File 'lib/type/definition/nilable.rb', line 49 def to_s parent_name = @parent && @parent.name parent_name ? "Type::#{parent_name}(nilable)" : super end |
#valid?(input, *args) ⇒ Boolean
Returns true if input is nil or the input is valid
28 29 30 |
# File 'lib/type/definition/nilable.rb', line 28 def valid?(input, *args) input.nil? || super end |