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.



96
97
98
# File 'lib/strong_json/type.rb', line 96

def initialize(type)
  @type = type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



94
95
96
# File 'lib/strong_json/type.rb', line 94

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



112
113
114
115
116
117
# File 'lib/strong_json/type.rb', line 112

def ==(other)
  if other.is_a?(Optional)
    # @type var other: Optional<any>
    other.type == type
  end
end

#coerce(value, path: ErrorPath.root(self)) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/strong_json/type.rb', line 100

def coerce(value, path: ErrorPath.root(self))
  unless value == nil
    @type.coerce(value, path: path.expand(type: @type))
  else
    nil
  end
end

#to_sObject



108
109
110
# File 'lib/strong_json/type.rb', line 108

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