Class: CSVStepImporter::Model::DAO

Inherits:
Node
  • Object
show all
Defined in:
lib/csv_step_importer/model/dao.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#children, #env, #parent

Instance Method Summary collapse

Methods inherited from Node

#add_children, #build_env, #run_validations!, #validate_children

Methods inherited from Base

#ancestors, #assign_attributes, #inspect, #persisted?, #save, #save!, set, #to_s, #update

Constructor Details

#initialize(parent:, row:, **attributes) ⇒ DAO

Logic



24
25
26
27
28
29
# File 'lib/csv_step_importer/model/dao.rb', line 24

def initialize(parent:, row:, **attributes)
  super parent: parent

  self.attributes = attributes
  self.row = row
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



6
7
8
# File 'lib/csv_step_importer/model/dao.rb', line 6

def attributes
  @attributes
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/csv_step_importer/model/dao.rb', line 6

def id
  @id
end

#rowObject

Returns the value of attribute row.



6
7
8
# File 'lib/csv_step_importer/model/dao.rb', line 6

def row
  @row
end

Instance Method Details

#create_or_updateObject



57
58
59
60
# File 'lib/csv_step_importer/model/dao.rb', line 57

def create_or_update
  # DAOs are usually processed in batches by the model and not saved one by one
  true
end

#current_timestampObject



62
63
64
# File 'lib/csv_step_importer/model/dao.rb', line 62

def current_timestamp
  model.cache[:updated_at] ||= (::ActiveRecord::Base.default_timezone == :utc ? ::Time.now.utc : ::Time.now).to_s(:db)
end

#link!Object

link this dao to a row



67
68
69
70
71
72
73
# File 'lib/csv_step_importer/model/dao.rb', line 67

def link!
  # add to cache with pluralized key
  (row.cache[model.cache_key(pluralize: true)] ||= []) << self

  # add to cache with singular key (for convenience)
  row.cache[model.cache_key(pluralize: false)] = self
end

#modelObject



31
32
33
# File 'lib/csv_step_importer/model/dao.rb', line 31

def model
  parent.parent
end

#unlink!(replace_with: nil) ⇒ Object

unlink this dao from the row and replace it with a different dao



76
77
78
79
80
81
82
83
84
85
# File 'lib/csv_step_importer/model/dao.rb', line 76

def unlink!(replace_with: nil)
  cached_daos = row.cache[model.cache_key(pluralize: true)]

  # remove from cache with pluralized key
  cached_daos.delete self
  cached_daos << replace_with

  # set any dao to cache with singular key (for convenience)
  row.cache[model.cache_key(pluralize: false)] = cached_daos.first
end

#valueObject

returns an array of all column values, used for batch importing



36
37
38
# File 'lib/csv_step_importer/model/dao.rb', line 36

def value
  @value ||= values_for columns
end

#value_for_key(key) ⇒ Object

retrieve a value for a key from the dao or row



41
42
43
44
45
46
47
48
49
# File 'lib/csv_step_importer/model/dao.rb', line 41

def value_for_key(key)
  if respond_to?(key)
    send key
  elsif attributes.include? key
    attributes[key]
  else
    row.send key
  end
end

#values_for(included_keys) ⇒ Object



51
52
53
54
55
# File 'lib/csv_step_importer/model/dao.rb', line 51

def values_for(included_keys)
  included_keys.each_with_object({}) do |key, values|
    values[key] = value_for_key key
  end
end