class StrongJSON::Type::Base

Attributes

type[R]

@dynamic type

Public Class Methods

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

Public Instance Methods

==(other) click to toggle source
# File lib/strong_json/type.rb, line 77
def ==(other)
  if other.is_a?(Base)
    other.type == type
  end
end
Also aliased as: eql?
coerce(value, path: ErrorPath.root(self)) click to toggle source
# File lib/strong_json/type.rb, line 62
def coerce(value, path: ErrorPath.root(self))
  raise TypeError.new(value: value, path: path) unless test(value)

  case type
  when :symbol
    value.to_sym
  else
    value
  end
end
eql?(other)
Alias for: ==
test(value) click to toggle source
# File lib/strong_json/type.rb, line 41
def test(value)
  case @type
  when :any
    true
  when :number
    value.is_a?(Numeric)
  when :integer
    value.is_a?(Integer)
  when :string
    value.is_a?(String)
  when :boolean
    value == true || value == false
  when :numeric
    value.is_a?(Numeric) || value.is_a?(String) && /\A[\-\+]?[\d.]+\Z/ =~ value
  when :symbol
    value.is_a?(String) || value.is_a?(Symbol)
  else
    false
  end
end
to_s() click to toggle source
# File lib/strong_json/type.rb, line 73
def to_s
  self.alias&.to_s || @type.to_s
end