class MightyJSON::Type::Enum

Attributes

types[R]

Public Class Methods

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

Public Instance Methods

compile(var:, path:) click to toggle source
# File lib/mighty_json/type.rb, line 200
      def compile(var:, path:)
        v = var.cur
        <<~END
          #{@types.map do |type|
            <<~END2.chomp
              begin
                #{type.compile(var: var, path: path)}
              rescue Error, UnexpectedFieldError, IllegalTypeError
              end
            END2
          end.join(' || ')} || (raise Error.new(path: #{path.inspect}, type: #{self.to_s.inspect}, value: #{v}))
        END
      end
to_s() click to toggle source
# File lib/mighty_json/type.rb, line 214
def to_s
  "enum(#{types.map(&:to_s).join(", ")})"
end