Class: CSVStepImporter::Model::DAO
- Defined in:
- lib/csv_step_importer/model/dao.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#id ⇒ Object
Returns the value of attribute id.
-
#row ⇒ Object
Returns the value of attribute row.
Attributes inherited from Node
Instance Method Summary collapse
- #create_or_update ⇒ Object
- #current_timestamp ⇒ Object
-
#initialize(parent:, row:, **attributes) ⇒ DAO
constructor
Logic.
-
#link! ⇒ Object
link this dao to a row.
- #model ⇒ Object
-
#unlink!(replace_with: nil) ⇒ Object
unlink this dao from the row and replace it with a different dao.
-
#value ⇒ Object
returns an array of all column values, used for batch importing.
-
#value_for_key(key) ⇒ Object
retrieve a value for a key from the dao or row.
- #values_for(included_keys) ⇒ Object
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
#attributes ⇒ Object
Returns the value of attribute attributes.
6 7 8 |
# File 'lib/csv_step_importer/model/dao.rb', line 6 def attributes @attributes end |
#id ⇒ Object
Returns the value of attribute id.
6 7 8 |
# File 'lib/csv_step_importer/model/dao.rb', line 6 def id @id end |
#row ⇒ Object
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_update ⇒ Object
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_timestamp ⇒ Object
62 63 64 |
# File 'lib/csv_step_importer/model/dao.rb', line 62 def 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 |
#model ⇒ Object
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 |
#value ⇒ Object
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 |