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.



39
40
41
42
43
44
# File 'lib/datashift/populator.rb', line 39

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

  @attribute_hash = {}
end

Instance Attribute Details

#attribute_hashObject

Returns the value of attribute attribute_hash.



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

def attribute_hash
  @attribute_hash
end

#original_dataObject

Returns the value of attribute original_data.



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

def original_data
  @original_data
end

#previous_valueObject

Returns the value of attribute previous_value.



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

def previous_value
  @previous_value
end

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Class Method Details

.attribute_hash_const_regexpObject



74
75
76
# File 'lib/datashift/populator.rb', line 74

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



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

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

.insistent_method_listObject



25
26
27
# File 'lib/datashift/populator.rb', line 25

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

.string_to_hash(str) ⇒ Object



293
294
295
# File 'lib/datashift/populator.rb', line 293

def self.string_to_hash( str )
  str.to_hash_object
end

Instance Method Details

#assign(method_binding, record) ⇒ Object



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
185
186
187
188
189
190
191
192
193
194
# File 'lib/datashift/populator.rb', line 142

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))

    DataShift::Populators::HasMany.call(record, value, method_binding)

  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
      DataShift::Populators::InsistentAssignment.call(record, value, operator)

      logger.debug("Assigned #{value} => [#{operator}]")
    end

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

    begin
      value ? record.send(operator, value) : record.send(operator)
    rescue => e
      logger.error e.backtrace.first
      raise DataProcessingError, "Method [#{operator}] could not process #{value} - #{e.inspect}"
    end

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

end

#assignment(record, value, model_method) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/datashift/populator.rb', line 196

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



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/datashift/populator.rb', line 268

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_belongs_to(method_binding, record, value) ⇒ Object

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



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/datashift/populator.rb', line 226

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



50
51
52
# File 'lib/datashift/populator.rb', line 50

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



57
58
59
60
61
# File 'lib/datashift/populator.rb', line 57

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)


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/datashift/populator.rb', line 85

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.casecmp('true').zero? || 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



63
64
65
66
67
68
# File 'lib/datashift/populator.rb', line 63

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

#value?Boolean

Returns:

  • (Boolean)


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

def value?
  !value.nil?
end