class MightyJSON::Builder

Constants

NONE

Public Class Methods

new(&block) click to toggle source
# File lib/mighty_json/builder.rb, line 8
def initialize(&block)
  @lets = {}
  instance_eval(&block)
end

Public Instance Methods

compile💪💪💪() click to toggle source
# File lib/mighty_json/builder.rb, line 13
    def compile💪💪💪
      Struct.new(*@lets.keys).new(
        *@lets.values.map do |type|
          Object.new.tap do |this|
            var = Variable.new
            v = var.cur
            eval <<~END
              def this.coerce(#{v})
                none = NONE
                #{type.compile(var: var, path: [])}
              end

              def this.=~(value)
                coerce(value)
                true
              rescue Error, IllegalTypeError, UnexpectedFieldError
                false
              end

              def this.===(value)
                coerce(value)
                true
              rescue Error, IllegalTypeError, UnexpectedFieldError
                false
              end
            END
          end
        end
      )
    end
let(name, type) click to toggle source
# File lib/mighty_json/builder.rb, line 44
def let(name, type)
  @lets[name] = type
  define_singleton_method(name) { type }
end