class StrongJSON::Type::Enum

Attributes

detector[R]
types[R]

@dynamic types, detector

Public Class Methods

new(types, detector = nil) click to toggle source
# File lib/strong_json/type.rb, line 300
def initialize(types, detector = nil)
  @types = types
  @detector = detector
end

Public Instance Methods

==(other) click to toggle source
# File lib/strong_json/type.rb, line 327
def ==(other)
  if other.is_a?(Enum)
    other.types == types &&
      other.detector == detector
  end
end
Also aliased as: eql?
coerce(value, path: ErrorPath.root(self)) click to toggle source
# File lib/strong_json/type.rb, line 309
def coerce(value, path: ErrorPath.root(self))
  if d = detector
    type = d[value]
    if type && types.include?(type)
      return type.coerce(value, path: path.expand(type: type))
    end
  end

  types.each do |ty|
    begin
      return ty.coerce(value, path: path.expand(type: ty))
    rescue UnexpectedAttributeError, TypeError # rubocop:disable Lint/HandleExceptions
    end
  end

  raise TypeError.new(path: path, value: value)
end
eql?(other)
Alias for: ==
to_s() click to toggle source
# File lib/strong_json/type.rb, line 305
def to_s
  self.alias&.to_s || "enum(#{types.map(&:to_s).join(", ")})"
end