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



59
60
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
# File 'lib/dynamic_schema/compiler.rb', line 59

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 == ::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
# 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 )

  @compiled_schema[ name ] = options.merge( {
    type: ::Object,
    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

#classObject



97
98
99
# File 'lib/dynamic_schema/compiler.rb', line 97

def class
  ::DynamicSchema::Compiler
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

#compiledObject



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

#inspectObject



93
94
95
# File 'lib/dynamic_schema/compiler.rb', line 93

def inspect
  { schema: @compiled_schema }.inspect 
end

#is_a?(klass) ⇒ Boolean Also known as: kind_of?

Returns:

  • (Boolean)


101
102
103
# File 'lib/dynamic_schema/compiler.rb', line 101

def is_a?( klass )
  klass == ::DynamicSchema::Compiler || klass == ::BasicObject
end

#pretty_print(pp) ⇒ Object



109
110
111
# File 'lib/dynamic_schema/compiler.rb', line 109

def pretty_print( pp )
  pp.pp( { schema: @compiled_schema } )
end

#to_sObject



89
90
91
# File 'lib/dynamic_schema/compiler.rb', line 89

def to_s
  inspect
end