module OceanDynamo::HasMany

Public Class Methods

included(base) click to toggle source
# File lib/ocean-dynamo/associations/has_many.rb, line 4
def self.included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

reload(*) click to toggle source

Sets all has_many relations to nil.

Calls superclass method
# File lib/ocean-dynamo/associations/has_many.rb, line 100
def reload(*)
  result = super

  self.class.relations_of_type(:has_many).each do |klass|
    attr_name = klass.to_s.pluralize.underscore
    instance_variable_set("@#{attr_name}", nil)
  end

  result
end

Protected Instance Methods

condition_options(child_class) click to toggle source
# File lib/ocean-dynamo/associations/has_many.rb, line 115
def condition_options(child_class)
  hash_key = child_class.table_hash_key
  range_key = child_class.table_range_key
  child_class.condition_builder(hash_key, id, range_key, ">=", "0", consistent: true)
end
delete_children(child_class) click to toggle source

Delete all children without instantiating them first.

# File lib/ocean-dynamo/associations/has_many.rb, line 166
def delete_children(child_class)
  return if new_record?
  child_hash_key = child_class.table_hash_key.to_s
  child_range_key = child_class.table_range_key && child_class.table_range_key.to_s
  child_class.in_batches :query, condition_options(child_class) do |attrs|
    child_class.delete attrs[child_hash_key], child_range_key && attrs[child_range_key]
  end
end
map_children(child_class) { |child_class._setup_from_dynamo(attrs)| ... } click to toggle source

Takes a block and yields each child to it. Batched for scalability.

# File lib/ocean-dynamo/associations/has_many.rb, line 155
def map_children(child_class)
  return if new_record?
  child_class.in_batches :query, condition_options(child_class) do |attrs|
    yield child_class.new._setup_from_dynamo(attrs)
  end
end
nullify_children(child_class) click to toggle source

Set the hash key values of all children to the string “NULL”, thereby turning them into orphans. Note that we're not setting the key to NULL as this isn't possible in DynamoDB. Instead, we're using the literal string “NULL”.

Using :nullify is a Bad Idea on DynamoDB, as it has to first read, then delete, and then recreate each record. You should redesign your application to user either +:delete? (the default) or :destroy instead.

# File lib/ocean-dynamo/associations/has_many.rb, line 185
def nullify_children(child_class)
  return if new_record?
  opts = condition_options(child_class)
  child_hash_key = child_class.table_hash_key.to_s
  child_range_key = child_class.table_range_key && child_class.table_range_key.to_s
  child_class.in_batches :query, opts do |attrs|
    # There is no way in the DynamoDB API to update a primary key attribute.
    # Delete the child item and recreate it with the updated key.
    child_class.delete attrs[child_hash_key], child_range_key && attrs[child_range_key]
    attrs[child_hash_key] = "NULL"
    child_class.dynamo_table.put_item(item: attrs)
  end
end