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
95 96 97 |
# File 'lib/strong_json/type.rb', line 95 def initialize(type) @type = type end |
Instance Attribute Details
#type ⇒ Object (readonly)
Returns the value of attribute type.
93 94 95 |
# File 'lib/strong_json/type.rb', line 93 def type @type end |
Instance Method Details
#==(other) ⇒ Object
111 112 113 114 115 |
# File 'lib/strong_json/type.rb', line 111 def ==(other) if other.is_a?(Optional) other.type == type end end |
#coerce(value, path: ErrorPath.root(self)) ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/strong_json/type.rb', line 99 def coerce(value, path: ErrorPath.root(self)) unless value == nil @type.coerce(value, path: path.(type: @type)) else nil end end |
#to_s ⇒ Object
107 108 109 |
# File 'lib/strong_json/type.rb', line 107 def to_s self.alias&.to_s || "optional(#{@type})" end |