Class: StrongJSON::Type::Optional
- Inherits:
-
Object
- Object
- StrongJSON::Type::Optional
- Defined in:
- lib/strong_json/type.rb
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #coerce(value, path: ErrorPath.root(self)) ⇒ Object
-
#initialize(type) ⇒ Optional
constructor
A new instance of Optional.
- #to_s ⇒ Object
Methods included from WithAlias
Methods included from Match
Constructor Details
#initialize(type) ⇒ Optional
Returns a new instance of Optional.
94 95 96 |
# File 'lib/strong_json/type.rb', line 94 def initialize(type) @type = type end |
Instance Attribute Details
#type ⇒ Object (readonly)
Returns the value of attribute type.
92 93 94 |
# File 'lib/strong_json/type.rb', line 92 def type @type end |
Instance Method Details
#==(other) ⇒ Object
110 111 112 113 114 115 |
# File 'lib/strong_json/type.rb', line 110 def ==(other) if other.is_a?(Optional) # @type var other: Optional<any> other.type == type end end |
#coerce(value, path: ErrorPath.root(self)) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/strong_json/type.rb', line 98 def coerce(value, path: ErrorPath.root(self)) unless value == nil @type.coerce(value, path: path.(type: @type)) else nil end end |
#to_s ⇒ Object
106 107 108 |
# File 'lib/strong_json/type.rb', line 106 def to_s self.alias&.to_s || "optional(#{@type})" end |