Class: MightyJSON::Builder
- Inherits:
-
Object
- Object
- MightyJSON::Builder
- Includes:
- Types
- Defined in:
- lib/mighty_json/builder.rb
Constant Summary collapse
Instance Method Summary collapse
- #compile💪💪💪 ⇒ Object
-
#initialize(&block) ⇒ Builder
constructor
A new instance of Builder.
- #let(name, type) ⇒ Object
Methods included from Types
#any, #array, #array?, #boolean, #boolean?, #enum, #ignored, #literal, #literal?, #number, #number?, #numeric, #numeric?, #object, #object?, #optional, #prohibited, #string, #string?, #symbol, #symbol?
Constructor Details
#initialize(&block) ⇒ Builder
Returns a new instance of Builder.
8 9 10 11 |
# File 'lib/mighty_json/builder.rb', line 8 def initialize(&block) @lets = {} instance_eval(&block) end |
Instance Method Details
#compile💪💪💪 ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# 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) ⇒ Object
44 45 46 47 |
# File 'lib/mighty_json/builder.rb', line 44 def let(name, type) @lets[name] = type define_singleton_method(name) { type } end |