Class: DataShift::Populator

Inherits:
Object
  • Object
show all
Extended by:
Delimiters, Logging
Includes:
Delimiters, Logging
Defined in:
lib/datashift/populator.rb

Instance Attribute Summary collapse

Attributes included from Delimiters

#attribute_list_end, #attribute_list_start, #csv_delimiter, #key_value_sep, #text_delim

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

logdir, logdir=, logger, verbose

Methods included from Delimiters

column_delim, column_delim=, eol, multi_assoc_delim, multi_assoc_delim=, multi_facet_delim, multi_value_delim, multi_value_delim=, name_value_delim, name_value_delim=, setmulti_facet_delim

Constructor Details

#initialize(transformer = nil) ⇒ Populator

Returns a new instance of Populator.



36
37
38
39
40
41
# File 'lib/datashift/populator.rb', line 36

def initialize(transformer = nil)
  # reset
  @transformer = transformer || Transformation.factory

  @attribute_hash = {}
end

Instance Attribute Details

#attribute_hashObject

Returns the value of attribute attribute_hash.



32
33
34
# File 'lib/datashift/populator.rb', line 32

def attribute_hash
  @attribute_hash
end

#original_dataObject

Returns the value of attribute original_data.



34
35
36
# File 'lib/datashift/populator.rb', line 34

def original_data
  @original_data
end

#previous_valueObject

Returns the value of attribute previous_value.



34
35
36
# File 'lib/datashift/populator.rb', line 34

def previous_value
  @previous_value
end

#valueObject

Returns the value of attribute value.



32
33
34
# File 'lib/datashift/populator.rb', line 32

def value
  @value
end

Class Method Details

.attribute_hash_const_regexpObject



71
72
73
# File 'lib/datashift/populator.rb', line 71

def self.attribute_hash_const_regexp
  @attribute_hash_const_regexp ||= Regexp.new( attribute_list_start + '.*' + attribute_list_end)
end

.insistent_find_by_listObject

When looking up an association, when no field provided, try each of these in turn till a match i.e find_by_name, find_by_title, find_by_id



28
29
30
# File 'lib/datashift/populator.rb', line 28

def self.insistent_find_by_list
  @insistent_find_by_list ||= [:name, :title, :id]
end

.insistent_method_listObject



22
23
24
# File 'lib/datashift/populator.rb', line 22

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

.string_to_hash(str) ⇒ Object



315
316
317
# File 'lib/datashift/populator.rb', line 315

def self.string_to_hash( str )
  str.to_hash_object
end

Instance Method Details

#assign(method_binding, record) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/datashift/populator.rb', line 140

def assign(method_binding, record)

  model_method = method_binding.model_method

  operator = model_method.operator

  klass = model_method.klass

  if model_method.operator_for(:belongs_to)
    insistent_belongs_to(method_binding, record, value)
  elsif  model_method.operator_for(:has_many)
    assign_has_many(method_binding, record)
  elsif  model_method.operator_for(:has_one)

    if value.is_a?(model_method.klass)
      record.send(operator + '=', value)
    else
      logger.error("Cannot assign value [#{value.inspect}]")
      logger.error("Value was Type (#{value.class}) - Required Type for has_one #{operator} is [#{klass}]")
    end

  elsif  model_method.operator_for(:assignment)

    if model_method.connection_adapter_column

      return if check_process_enum(record, model_method )  # TOFIX .. enum section probably belongs in prepare_data

      assignment(record, value, model_method)

    else
      logger.debug("Brute force assignment of value  #{value} => [#{operator}]")
      # 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, value, operator)
    end

  elsif model_method.operator_for(:method)
    logger.debug("Method delegation assignment of value  #{value} => [#{operator}]")
    insistent_assignment(record, value, operator)

  else
    logger.warn("Cannot assign via [#{operator}] to #{record.inspect} ")
  end

end

#assignment(record, value, model_method) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/datashift/populator.rb', line 186

def assignment(record, value, model_method)

  operator = model_method.operator
  connection_adapter_column = model_method.connection_adapter_column

  begin
    if(connection_adapter_column.respond_to? :type_cast)
      logger.debug("Assignment via [#{operator}] to [#{value}] (CAST TYPE [#{model_method.connection_adapter_column.type_cast(value).inspect}])")

      record.send( operator + '=', model_method.connection_adapter_column.type_cast( value ) )

    else
      logger.debug("Assignment via [#{operator}] to [#{value}] (NO CAST)")

      # Good guide on diff ways to set attributes
      #   http://www.davidverhasselt.com/set-attributes-in-activerecord/
      if(DataShift::Configuration.call.update_and_validate)
        record.update( operator => value)
      else
        record.send( operator + '=', value)
      end
    end
  rescue => e
    logger.error e.backtrace.first
    logger.error("Assignment failed #{e.inspect}")
    raise DataProcessingError, "Failed to set [#{value}] via [#{operator}] due to ERROR : #{e.message}"
  end
end

#check_process_enum(record, model_method) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/datashift/populator.rb', line 290

def check_process_enum(record, model_method)

  klass = model_method.klass
  operator = model_method.operator

  if klass.respond_to?(operator.pluralize)

    enums = klass.send(operator.pluralize)

    logger.debug("Checking for enum - #{enums.inspect} - #{value.parameterize.underscore}" )

    if enums.is_a?(Hash) && enums.keys.include?(value.parameterize.underscore)
      # ENUM
      logger.debug("[#{operator}] Appears to be an ENUM - setting to [#{value}])")

      # TODO: - now we know this column is an enum set operator type to :enum to save this check in future
      # probably requires changes above to just assign enum directly without this check
      model_method.operator_for(:assignment)

      record.send( operator + '=', value.parameterize.underscore)
      return true
    end
  end
end

#insistent_assignment(record, value, operator) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/datashift/populator.rb', line 215

def insistent_assignment(record, value, operator)

  op = operator + '=' unless operator.include?('=')

  # TODO: - fix this crap - perhaps recursion ??
  begin
    record.send(op, value)
  rescue
    begin
      op = operator.downcase
      op += '=' unless operator.include?('=')

      record.send(op, value)

    rescue => e

      Populator.insistent_method_list.each do |f|
        begin
          record.send(op, value.send(f) )
          break
        rescue => e
          if f == Populator.insistent_method_list.last
            logger.error(e.inspect)
            logger.error("Failed to assign [#{value}] via operator #{operator}")
            raise DataProcessingError, "Failed to assign [#{value}] to #{operator}" unless value.nil?
          end
        end
      end
    end
  end
end

#insistent_belongs_to(method_binding, record, value) ⇒ Object

Attempt to find the associated object via id, name, title .…



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/datashift/populator.rb', line 248

def insistent_belongs_to(method_binding, record, value )

  operator = method_binding.operator

  klass = method_binding.model_method.operator_class

  if value.class == klass
    logger.info("Populator assigning #{value} to belongs_to association #{operator}")
    record.send(operator) << value
  else

    unless method_binding.klass.respond_to?('where')
      raise CouldNotAssignAssociation, "Populator failed to assign [#{value}] to belongs_to [#{operator}]"
    end

    # Try the default field names

    # TODO: - add find by operators from headers or configuration to  insistent_find_by_list
    Populator.insistent_find_by_list.each do |find_by|
      begin

        item = klass.where(find_by => value).first_or_create

        next unless item

        logger.info("Populator assigning #{item.inspect} to belongs_to association #{operator}")
        record.send(operator + '=', item)
        break

      rescue => e
        logger.error(e.inspect)
        logger.error("Failed attempting to find belongs_to for #{method_binding.pp}")
        if find_by == Populator.insistent_method_list.last
          raise CouldNotAssignAssociation,
                "Populator failed to assign [#{value}] to belongs_to association [#{operator}]" unless value.nil?
        end
      end
    end

  end
end

#prepare_and_assign(context, record, data) ⇒ Object

Prepare the data to be populated, then assign to the Db record



47
48
49
# File 'lib/datashift/populator.rb', line 47

def prepare_and_assign(context, record, data)
  prepare_and_assign_method_binding(context.method_binding, record, data)
end

#prepare_and_assign_method_binding(method_binding, record, data) ⇒ Object

This is the most pertinent hook for derived Processors, where you can provide custom population messages for specific Method bindings



54
55
56
57
58
# File 'lib/datashift/populator.rb', line 54

def prepare_and_assign_method_binding(method_binding, record, data)
  prepare_data(method_binding, data)

  assign(method_binding, record)
end

#prepare_data(method_binding, data) ⇒ Object

Check supplied value, validate it, and if required :

set to provided default value
prepend any provided prefixes
add any provided postfixes

Rtns : tuple of [:value, :attribute_hash]

Raises:

  • (NilDataSuppliedError)


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
# File 'lib/datashift/populator.rb', line 82

def prepare_data(method_binding, data)

  connection_adapter_column = method_binding.model_method.connection_adapter_column


  raise NilDataSuppliedError, 'No method_binding supplied for prepare_data' unless method_binding

  @original_data = data

  begin

    if(data.is_a?(ActiveRecord::Relation)) # Rails 4 - query no longer returns an array
      @value = data.to_a

    elsif(data.class.ancestors.include?(ActiveRecord::Base) || data.is_a?(Array))
      @value = data

    elsif(!DataShift::Guards.jruby? &&
      (data.is_a?(Spreadsheet::Formula) || data.class.ancestors.include?(Spreadsheet::Formula)) )

      @value = data.value  # TOFIX jruby/apache poi equivalent ?

    elsif(connection_adapter_column && connection_adapter_column.cast_type.is_a?(ActiveRecord::Type::Boolean))

      # DEPRECATION WARNING: You attempted to assign a value which is not explicitly `true` or `false` ("0.00")
      # to a boolean column. Currently this value casts to `false`.
      # This will change to match Ruby's semantics, and will cast to `true` in Rails 5.
      # If you would like to maintain the current behavior, you should explicitly handle the values you would like cast to `false`.

      @value = if(data.in? [true, false])
                 data
               else
                 (data.to_s.downcase == "true" || data.to_s.to_i == 1) ? true : false
               end
    else
      @value = data.to_s

      @attribute_hash = @value.slice!( Populator.attribute_hash_const_regexp )

      if attribute_hash && !attribute_hash.empty?
        @attribute_hash = Populator.string_to_hash( attribute_hash )
        logger.info "Populator found attribute hash :[#{attribute_hash.inspect}]"
      else
        @attribute_hash = {}
      end
    end

    run_transforms(method_binding)

  rescue => e
    logger.error(e.message)
    logger.error("Populator stacktrace: #{e.backtrace.first}")
    raise DataProcessingError, "Populator failed to prepare data [#{value}] for #{method_binding.pp}"
  end

  [value, attribute_hash]
end

#resetObject



60
61
62
63
64
65
# File 'lib/datashift/populator.rb', line 60

def reset
  @value = nil
  @previous_value = nil
  @original_data = nil
  @attribute_hash = {}
end

#value?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/datashift/populator.rb', line 67

def value?
  !value.nil?
end