class MightyJSON::Type::Base

Attributes

type[R]

Public Class Methods

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

Public Instance Methods

compile(var:, path:) click to toggle source
# File lib/mighty_json/type.rb, line 24
      def compile(var:, path:)
        v = var.cur
        err = "(raise Error.new(value: #{v}, type: #{self.to_s.inspect}, path: #{path.inspect}))"
        case @type
        when :ignored
          if path == []
            'raise IllegalTypeError.new(type: :ignored)'
          else
            'none'
          end
        when :any
          v
        when :number
          <<~END
            #{v}.is_a?(Numeric) ?
              #{v} :
              #{err}
          END
        when :string
          <<~END
            #{v}.is_a?(String) ?
              #{v} :
              #{err}
          END
        when :boolean
          <<~END
            #{v} == true || #{v} == false ?
              #{v} :
              #{err}
          END
        when :numeric
          <<~END
            #{v}.is_a?(Numeric) || (#{v}.is_a?(String) && /\\A[-+]?[\\d.]+\\Z/ =~ #{v}) ?
              #{v} :
              #{err}
          END
        when :symbol
          <<~END
            #{v}.is_a?(Symbol) ?
              #{v} :
              #{err}
          END
        else
          err
        end
      end
to_s() click to toggle source
# File lib/mighty_json/type.rb, line 71
def to_s
  @type.to_s
end