Class: StrongJSON::Type::Optional

Inherits:
Object
  • Object
show all
Includes:
Match, WithAlias
Defined in:
lib/strong_json/type.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from WithAlias

#alias, #with_alias

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

#typeObject (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.expand(type: @type))
  else
    nil
  end
end

#to_sObject



106
107
108
# File 'lib/strong_json/type.rb', line 106

def to_s
  self.alias&.to_s || "optional(#{@type})"
end