Class: DynamicSchema::Compiler
- Inherits:
-
BasicObject
- Includes:
- PP::ObjectMixin
- Defined in:
- lib/dynamic_schema/compiler.rb
Instance Method Summary
collapse
Constructor Details
#initialize(compiled_schema = nil, compiled_blocks: nil) ⇒ Compiler
Returns a new instance of Compiler.
6
7
8
9
10
11
12
|
# File 'lib/dynamic_schema/compiler.rb', line 6
def initialize( compiled_schema = nil, compiled_blocks: nil )
@compiled_schema = compiled_schema || {}
@block = nil
@compiled = false
@compiled_blocks = compiled_blocks || []
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/dynamic_schema/compiler.rb', line 61
def method_missing( method, *args, &block )
first = args.first
options = nil
if args.empty?
options = {}
elsif first.is_a?( ::Hash )
options = first
elsif args.length == 1 &&
( first.is_a?( ::Class ) || first.is_a?( ::Module ) || first.is_a?( ::Array ) )
options = { type: first }
elsif args.length == 2 &&
( first.is_a?( ::Class ) || first.is_a?( ::Module ) || first.is_a?( ::Array ) ) &&
args[ 1 ].is_a?( ::Hash )
options = args[ 1 ]
options[ :type ] = args[ 0 ]
else
::Kernel.raise \
::ArgumentError,
"A schema definition may only include the type (Class or Module) followed by options (Hash). "
end
type = options[ :type ]
if type.is_a?( ::Array ) && block
_object( method, options, &block )
elsif type == ::Object || ( type.nil? && block )
_object( method, options, &block )
else
_value( method, options )
end
end
|
Instance Method Details
#_object(name, options = {}, &block) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/dynamic_schema/compiler.rb', line 46
def _object( name, options = {}, &block )
name = name.to_sym
receiver = ::DynamicSchema::Receiver::Object
::Kernel.raise ::NameError, "The name '#{name}' is reserved and cannot be used for parameters." \
if receiver.method_defined?( name ) || receiver.private_method_defined?( name )
type = options[ :type ]
options[ :type ] = ::Object unless type.is_a?( ::Array )
@compiled_schema[ name ] = options.merge( {
compiler: Compiler.new( compiled_blocks: @compiled_blocks ).compile( &block )
} )
self
end
|
#_value(name, options) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/dynamic_schema/compiler.rb', line 34
def _value( name, options )
name = name.to_sym
receiver = ::DynamicSchema::Receiver::Object
::Kernel.raise ::NameError, "The name '#{name}' is reserved and cannot be used for parameters." \
if receiver.method_defined?( name ) || receiver.private_method_defined?( name )
_validate_in!( name, options[ :type ], options[ :in ] ) if options[ :in ]
@compiled_schema[ name ] = options
self
end
|
#compile(&block) ⇒ Object
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/dynamic_schema/compiler.rb', line 14
def compile( &block )
@block = block
@compiled = false
unless @compiled_blocks.include?( @block )
@compiled_blocks << @block
self.instance_eval( &@block )
@compiled = true
end
self
end
|
#compiled ⇒ Object
25
26
27
28
29
30
31
32
|
# File 'lib/dynamic_schema/compiler.rb', line 25
def compiled
if !@compiled && @block
@compiled_blocks << @block unless @compiled_blocks.include?( @block )
self.instance_eval( &@block )
@compiled = true
end
@compiled_schema
end
|
#inspect ⇒ Object
98
99
100
|
# File 'lib/dynamic_schema/compiler.rb', line 98
def inspect
{ schema: @compiled_schema }.inspect
end
|
#is_a?(klass) ⇒ Boolean
Also known as:
kind_of?
106
107
108
|
# File 'lib/dynamic_schema/compiler.rb', line 106
def is_a?( klass )
klass == ::DynamicSchema::Compiler || klass == ::BasicObject
end
|
#pretty_print(pp) ⇒ Object
114
115
116
|
# File 'lib/dynamic_schema/compiler.rb', line 114
def pretty_print( pp )
pp.pp( { schema: @compiled_schema } )
end
|
#to_s ⇒ Object
94
95
96
|
# File 'lib/dynamic_schema/compiler.rb', line 94
def to_s
inspect
end
|