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.



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

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



127
128
129
# File 'lib/strong_json/type.rb', line 127

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object



142
143
144
145
146
# File 'lib/strong_json/type.rb', line 142

def ==(other)
  if other.is_a?(Literal)
    other.value == value
  end
end

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

Raises:



137
138
139
140
# File 'lib/strong_json/type.rb', line 137

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

#to_sObject



133
134
135
# File 'lib/strong_json/type.rb', line 133

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