Class: AdaptiveConfiguration::Scaffold

Inherits:
BasicObject
Includes:
PP::ObjectMixin
Defined in:
lib/adaptive_configuration/scaffold.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = nil, definitions:, converters:) ⇒ Scaffold

Returns a new instance of Scaffold.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/adaptive_configuration/scaffold.rb', line 8

def initialize( values = nil, definitions:, converters: )
  raise ArgumentError, 'The Scaffold initialitization attributes must be a Hash pr Hash-like.'\
    unless values.nil? || ( values.respond_to?( :[] ) && values.respond_to?( :key? ) )

  @converters = converters&.dup 
  @definitions = definitions&.dup 
  @values = {}
  @errors = []
  
  @definitions.each do | key, definition |
    name = definition[ :as ] || key 
    if definition.key?( :default )
      self.__send__( key, definition[ :default ] )
      # note: this is needed to know when an array parameter which was initially assigned
      #       to a default should be replaced rather than appended
      definition[ :default_assigned ] = true
    end
    self.__send__( key, values[ key ] ) if values && values.key?( key ) 
  end

end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



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
133
134
135
136
137
138
139
140
# File 'lib/adaptive_configuration/scaffold.rb', line 81

def method_missing( method, *args, &block )

  if @definitions.key?( method )
    definition = @definitions[ method ]
    name = definition[ :as ] || method

    unless definition[ :array ] 
      if definition[ :type ] == ::Object
        value = args.first 
        context = @values[ name ]
        if context.nil? || value  
          context = 
            Scaffold.new( 
              value,
              converters: @converters, 
              definitions: definition[ :definitions ] 
            )
        end
        context.instance_eval( &block ) if block
        @values[ name ] = context 
      else 
        value = args.first
        new_value = definition[ :type ] ? __coerce_value( definition[ :type ], value ) : value
        @values[ name ] = new_value.nil? ? value : new_value
      end
    else
      @values[ name ] = definition[ :default_assigned ] ? 
        ::Array.new : 
        @values[ name ] || ::Array.new
      if definition[ :type ] == ::Object
        values = [ args.first ].flatten
        values = values.map do | v |
          context = Scaffold.new( 
            v,
            converters: @converters, 
            definitions: definition[ :definitions ] 
          )
          context.instance_eval( &block ) if block
          context
        end
        @values[ name ].concat( values ) 
      else
        values = ::Kernel.method( :Array ).call( args.first )
        if type = definition[ :type ]
          values = values.map do | v | 
            new_value = __coerce_value( type, v )
            new_value.nil? ? v : new_value
          end 
        end
        @values[ name ].concat( values )
      end
    end

    definition[ :default_assigned ] = false
    @values[ name ]
  else
    super
  end

end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



6
7
8
# File 'lib/adaptive_configuration/scaffold.rb', line 6

def errors
  @errors
end

Instance Method Details

#classObject



71
72
73
# File 'lib/adaptive_configuration/scaffold.rb', line 71

def class
  ::AdaptiveConfiguration::Scaffold
end

#empty?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/adaptive_configuration/scaffold.rb', line 34

def empty?
  @values.empty?
end

#inspectObject



61
62
63
# File 'lib/adaptive_configuration/scaffold.rb', line 61

def inspect
  { values: @values, definitions: @definitions }.inspect 
end

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

Returns:

  • (Boolean)


75
76
77
# File 'lib/adaptive_configuration/scaffold.rb', line 75

def is_a?( klass )
  klass == ::AdaptiveConfiguration::Scaffold || klass == ::BasicObject
end

#nil?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/adaptive_configuration/scaffold.rb', line 30

def nil?
  false  
end

#pretty_print(pp) ⇒ Object



66
67
68
# File 'lib/adaptive_configuration/scaffold.rb', line 66

def pretty_print( pp )
  pp.pp( { values: @values, definitions: @definitions } )
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/adaptive_configuration/scaffold.rb', line 142

def respond_to?( method, include_private = false )
  @definitions.key?( method ) || self.class.instance_methods.include?( method ) 
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/adaptive_configuration/scaffold.rb', line 146

def respond_to_missing?( method, include_private = false )
  @definitions.key?( method ) || self.class.instance_methods.include?( method ) 
end

#to_hObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/adaptive_configuration/scaffold.rb', line 38

def to_h
  recursive_to_h = ->( object ) do
    case object
    when ::NilClass
      nil
    when ::AdaptiveConfiguration::Scaffold
      recursive_to_h.call( object.to_h )
    when ::Hash
      object.transform_values { | value | recursive_to_h.call( value ) }
    when ::Array
      object.map { | element | recursive_to_h.call( element ) }
    else
      object.respond_to?( :to_h ) ? object.to_h : object
    end
  end

  recursive_to_h.call( @values )
end

#to_sObject



57
58
59
# File 'lib/adaptive_configuration/scaffold.rb', line 57

def to_s
  inspect
end