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



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

def initialize(type)
  @type = type
end

Instance Attribute Details

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

#to_sObject



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

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