class Dynamoid::Persistence::Import

@private

Public Class Methods

call(model_class, array_of_attributes) click to toggle source
# File lib/dynamoid/persistence/import.rb, line 9
def self.call(model_class, array_of_attributes)
  new(model_class, array_of_attributes).call
end
new(model_class, array_of_attributes) click to toggle source
# File lib/dynamoid/persistence/import.rb, line 13
def initialize(model_class, array_of_attributes)
  @model_class = model_class
  @array_of_attributes = array_of_attributes
end

Public Instance Methods

call() click to toggle source
# File lib/dynamoid/persistence/import.rb, line 18
def call
  models = @array_of_attributes.map(&method(:build_model))

  unless Dynamoid.config.backoff
    import(models)
  else
    import_with_backoff(models)
  end

  models.each do |m|
    m.new_record = false
    m.clear_changes_information
  end
  models
end

Private Instance Methods

array_of_dumped_attributes(models) click to toggle source
# File lib/dynamoid/persistence/import.rb, line 69
def array_of_dumped_attributes(models)
  models.map do |m|
    Dumping.dump_attributes(m.attributes, @model_class.attributes)
  end
end
build_model(attributes) click to toggle source
# File lib/dynamoid/persistence/import.rb, line 36
def build_model(attributes)
  attrs = attributes.symbolize_keys

  if @model_class.timestamps_enabled?
    time_now = DateTime.now.in_time_zone(Time.zone)
    attrs[:created_at] ||= time_now
    attrs[:updated_at] ||= time_now
  end

  @model_class.build(attrs).tap do |model|
    model.hash_key = SecureRandom.uuid if model.hash_key.blank?
  end
end
import(models) click to toggle source
# File lib/dynamoid/persistence/import.rb, line 65
def import(models)
  Dynamoid.adapter.batch_write_item(@model_class.table_name, array_of_dumped_attributes(models))
end
import_with_backoff(models) click to toggle source
# File lib/dynamoid/persistence/import.rb, line 50
def import_with_backoff(models)
  backoff = nil
  table_name = @model_class.table_name
  items = array_of_dumped_attributes(models)

  Dynamoid.adapter.batch_write_item(table_name, items) do |has_unprocessed_items|
    if has_unprocessed_items
      backoff ||= Dynamoid.config.build_backoff
      backoff.call
    else
      backoff = nil
    end
  end
end