Class: ARLoader::MethodDetail

Inherits:
Object
  • Object
show all
Defined in:
lib/ar_loader/method_detail.rb

Constant Summary collapse

@@default_values =
{}
@@prefixes =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_name, klass, operator, type, col_types = {}) ⇒ MethodDetail

Store the raw (client supplied) name against the active record klass(model), operator and types



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ar_loader/method_detail.rb', line 38

def initialize(client_name, klass, operator, type, col_types = {} )
  @klass, @name = klass, client_name

  if( MethodDetail::type_enum.member?(type.to_sym) )
    @operator_type = type
  else
    raise "Bad operator Type #{type} passed to Method Detail"
  end

  @operator = operator

  # Note : Not all assignments will currently have a column type, for example
  # those that are derived from a delegate_belongs_to
  @col_type = col_types[operator]
end

Instance Attribute Details

#col_typeObject (readonly)

Name is the raw, client supplied name



33
34
35
# File 'lib/ar_loader/method_detail.rb', line 33

def col_type
  @col_type
end

#current_valueObject (readonly)

Name is the raw, client supplied name



33
34
35
# File 'lib/ar_loader/method_detail.rb', line 33

def current_value
  @current_value
end

#nameObject (readonly)

Name is the raw, client supplied name



33
34
35
# File 'lib/ar_loader/method_detail.rb', line 33

def name
  @name
end

#operatorObject (readonly)

Returns the value of attribute operator.



35
36
37
# File 'lib/ar_loader/method_detail.rb', line 35

def operator
  @operator
end

#operator_typeObject (readonly)

Returns the value of attribute operator_type.



35
36
37
# File 'lib/ar_loader/method_detail.rb', line 35

def operator_type
  @operator_type
end

Class Method Details

.default_value(name) ⇒ Object



162
163
164
# File 'lib/ar_loader/method_detail.rb', line 162

def self.default_value(name)
  @@default_values[name]
end

.insistent_method_listObject



179
180
181
182
# File 'lib/ar_loader/method_detail.rb', line 179

def self.insistent_method_list
  @insistent_method_list ||= [:to_s, :to_i, :to_f, :to_b]
  @insistent_method_list
end

.prefix_value(name) ⇒ Object



170
171
172
# File 'lib/ar_loader/method_detail.rb', line 170

def self.prefix_value(name)
  @@prefixes[name]
end

.set_default_value(name, value) ⇒ Object



158
159
160
# File 'lib/ar_loader/method_detail.rb', line 158

def self.set_default_value( name, value )
  @@default_values[name] = value
end

.set_prefix(name, value) ⇒ Object



166
167
168
# File 'lib/ar_loader/method_detail.rb', line 166

def self.set_prefix( name, value )
  @@prefixes[name] = value
end

.type_enumObject



19
20
21
22
# File 'lib/ar_loader/method_detail.rb', line 19

def self.type_enum
  @type_enum ||= Set[:assignment, :belongs_to, :has_one, :has_many]
  @type_enum
end

Instance Method Details

#assign(record, value) ⇒ Object



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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/ar_loader/method_detail.rb', line 112

def assign(record, value )

  @current_value = validate_value(value)

  puts "WARNING nil value supplied for Column [#{@name}]" if(@current_value.nil?)

  if( operator_for(:belongs_to) )
  
    #puts "DEBUG : BELONGS_TO : #{@name} : #{operator} - Lookup #{@current_value} in DB"
    insistent_belongs_to(record, @current_value)

  elsif( operator_for(:has_many) )

    #puts "DEBUG : HAS_MANY :  #{@name} : #{operator}(#{operator_class}) - Lookup #{@current_value} in DB"
    if(value.is_a?(Array) || value.is_a?(operator_class))
      record.send(operator) << value
    else
      puts "ERROR #{value.class} - Not expected type for has_many #{operator} - cannot assign"
      # TODO -  Not expected type - maybe try to look it up somehow ?"
      #insistent_has_many(record, @current_value)
    end

  elsif( operator_for(:has_one) )

    #puts "DEBUG : HAS_MANY :  #{@name} : #{operator}(#{operator_class}) - Lookup #{@current_value} in DB"
    if(value.is_a?(operator_class))
      record.send(operator + '=', value)
    else
      puts "ERROR #{value.class} - Not expected type for has_one #{operator} - cannot assign"
      # TODO -  Not expected type - maybe try to look it up somehow ?"
      #insistent_has_many(record, @current_value)
    end

  elsif( operator_for(:assignment) && @col_type )
    #puts "DEBUG : COl TYPE defined for #{@name} : #{@assignment} => #{@current_value} #{@col_type.inspect}"
    #puts "DEBUG : COl TYPE CAST: #{@current_value} => #{@col_type.type_cast( @current_value ).inspect}"
    record.send( operator + '=' , @col_type.type_cast( @current_value ) )

  elsif( operator_for(:assignment) )
    #puts "DEBUG : Brute force assignment of value  #{@current_value} supplied for Column [#{@name}]"
    # brute force case for assignments without a column type (which enables us to do correct type_cast)
    # so in this case, attempt straightforward assignment then if that fails, basic ops such as to_s, to_i, to_f etc
    insistent_assignment(record, @current_value)
  end
end

#operator?(name) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/ar_loader/method_detail.rb', line 62

def operator?(name)
  operator == name
end

#operator_classObject

Return the operator’s expected class, if can be derived, else nil



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ar_loader/method_detail.rb', line 84

def operator_class()
  @operator_class ||= if(operator_for(:has_many) || operator_for(:belongs_to) || operator_for(:has_one))
    begin
      Kernel.const_get(operator.classify)
    rescue; ""; end

  elsif(@col_type)
    begin
      Kernel.const_get(@col_type.type.to_s.classify)
    rescue; nil; end
  else
    nil
  end

  @operator_class
end

#operator_class_nameObject

Return the operator’s expected class name, if can be derived, else nil



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ar_loader/method_detail.rb', line 67

def operator_class_name()
  @operator_class_name ||= if(operator_for(:has_many) || operator_for(:belongs_to) || operator_for(:has_one))
    begin
      Kernel.const_get(operator.classify)
      operator.classify
    rescue; ""; end
  
  elsif(@col_type)
    @col_type.type.to_s.classify
  else
    ""
  end

  @operator_class_name
end

#operator_for(type) ⇒ Object

Return the actual operator’s name for supplied method type where type one of :assignment, :has_one, :belongs_to, :has_many etc



57
58
59
60
# File 'lib/ar_loader/method_detail.rb', line 57

def operator_for( type )
  return operator if(@operator_type == type)
  nil
end

#ppObject



174
175
176
# File 'lib/ar_loader/method_detail.rb', line 174

def pp
  "#{@name} => #{operator}"
end

#validate_value(value) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/ar_loader/method_detail.rb', line 103

def validate_value(value)

  return @@default_values[@name] if(@@default_values[@name])

  return "#{@@prefixes[@name]}#{value}" if(@@prefixes[@name])

  value
end