Class: StrongJSON::Type::Literal

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(value) ⇒ Literal

Returns a new instance of Literal.



131
132
133
# File 'lib/strong_json/type.rb', line 131

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



129
130
131
# File 'lib/strong_json/type.rb', line 129

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object



144
145
146
147
148
149
# File 'lib/strong_json/type.rb', line 144

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

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

Raises:



139
140
141
142
# File 'lib/strong_json/type.rb', line 139

def coerce(value, path: ErrorPath.root(self))
  raise TypeError.new(path: path, value: value) unless (_ = self.value) == value
  value
end

#to_sObject



135
136
137
# File 'lib/strong_json/type.rb', line 135

def to_s
  self.alias&.to_s || (_ = @value).inspect
end