Class: DynamicSchema::Struct

Inherits:
Object
  • Object
show all
Includes:
Validator
Defined in:
lib/dynamic_schema/struct.rb

Class Method Summary collapse

Methods included from Validator

#valid?, #validate, #validate!

Class Method Details

.compiled_schemaObject



134
135
136
137
# File 'lib/dynamic_schema/struct.rb', line 134

def compiled_schema
  @__compiled_schema || 
    ( superclass.respond_to?( :compiled_schema ) && superclass.compiled_schema )
end

.converterObject



139
140
141
# File 'lib/dynamic_schema/struct.rb', line 139

def converter
  @__converter || ( superclass.respond_to?( :converter ) && superclass.converter )
end

.define(inherit: nil, &block) ⇒ Object



6
7
8
9
# File 'lib/dynamic_schema/struct.rb', line 6

def define( inherit: nil, &block )
  builder = ::DynamicSchema.define( inherit: inherit, &block )
  new( builder.schema )
end

.new(*arguments, &block) ⇒ Object



11
12
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/dynamic_schema/struct.rb', line 11

def new( *arguments, &block )
  unless self == ::DynamicSchema::Struct
    super
  else
    __schema = arguments.first  
    ::Kernel.raise ::ArgumentError, "A Struct requires a schema." \
      unless __schema 
    __converter = ::DynamicSchema::Converter

    __compiled_schema = __compile_schema( __schema )
    __klass = ::Class.new( self ) do
      
      class << self
        def build( attributes = {}, &block )
          struct = new( attributes )
          block.call( struct ) if block
          struct
        end

        def build!( attributes = {}, &block )
          struct = build( attributes, &block )
          struct.validate!
          struct
        end
      end

      def initialize( attributes = nil )
        @attributes = attributes&.dup || {}
        @converted_attributes = {}
      end

      __compiled_schema.each do | property, criteria |
        
        key = ( criteria[ :as ] || property ).to_sym
        type = criteria[ :type ]
        default = criteria[ :default ]

        if type == ::Object
          define_method( property ) do
            @converted_attributes.fetch( key ) do
              value = @attributes[ key ]
              schema = criteria[ :schema ] ||= ( criteria[ :compiler ]&.compiled )
              return value unless schema
              klass = criteria[ :class ] ||= ::DynamicSchema::Struct.new( schema )
              @converted_attributes[ key ] = 
                if criteria[ :array ]
                  Array( value || default ).map { | v | klass.build( v || {} ) }
                else
                  klass.build( value || default )
                end
            end
          end
        elsif type
          define_method( property ) do 
            @converted_attributes.fetch( key ) do 
              value = @attributes[ key ] 
              @converted_attributes[ key ] = criteria[ :array ] ? 
                Array( value || default ).map { | v | __convert( v, to: type ) } : 
                __convert( value || default, to: type )
            end
          end
        else
          define_method( property ) do
            @attributes[ key ] || default
          end 
        end

        define_method( :"#{ property }=" ) do | value | 
          @converted_attributes.delete( key )
          @attributes[ key ] = value 
        end

      end
    
      [ :validate!, :validate, :valid? ].each do | method |
        define_method( method ) { super( @attributes ) }
      end

      def to_h
        result = {}
        self.compiled_schema.each do | property, criteria |
          key = criteria[ :as ] || property
          value = __object_to_h( self.send( property ) ) 
          result[ key ] = value unless value.nil?
        end
        result
      end

    private 

      def compiled_schema
        self.class.compiled_schema
      end

      def __object_to_h( object )
        case object
        when nil
          nil
        when ::Hash
          object.transform_values { | v | __object_to_h( v ) } unless object.empty?
        when ::Array
          object.map { | e | __object_to_h( e ) } unless object.empty?
        else
          if object.respond_to?( :to_h )
            __object_to_h( object.to_h )
          else
            object
          end
        end
      end

      def __convert( value, to: )
        self.class.converter.convert( value, to: to ) { | v | to.new( v ) rescue nil }
      end

    end
    __klass.instance_variable_set( :@__compiled_schema, __compiled_schema.dup )
    __klass.instance_variable_set( :@__converter, __converter )
    __klass.class_eval( &block ) if block
    __klass
  end
end