Module: OceanDynamo::Persistence::ClassMethods

Defined in:
lib/ocean-dynamo/persistence.rb

Overview


Class methods

Instance Method Summary collapse

Instance Method Details

#_late_connect?Boolean

:nodoc:

Returns:

  • (Boolean)


83
84
85
86
87
88
# File 'lib/ocean-dynamo/persistence.rb', line 83

def _late_connect? # :nodoc:
  return false if table_connected
  return false unless table_connect_policy
  establish_db_connection
  true
end

#create(attributes = nil) {|object| ... } ⇒ Object

Yields:

  • (object)


17
18
19
20
21
22
# File 'lib/ocean-dynamo/persistence.rb', line 17

def create(attributes = nil, &block)
  object = new(attributes)
  yield(object) if block_given?
  object.save
  object
end

#create!(attributes = nil) {|object| ... } ⇒ Object

Yields:

  • (object)


25
26
27
28
29
30
# File 'lib/ocean-dynamo/persistence.rb', line 25

def create!(attributes = nil, &block)
  object = new(attributes)
  yield(object) if block_given?
  object.save!
  object
end

#delete(hash, range = nil) ⇒ Object

Class method to delete a record. Returns true if the record existed, false if it didn’t.



37
38
39
40
41
42
43
44
45
# File 'lib/ocean-dynamo/persistence.rb', line 37

def delete(hash, range=nil)
  _late_connect?
  keys = { table_hash_key.to_s => hash }
  keys[table_range_key] = range if table_range_key && range
  options = { key: keys,
              return_values: "ALL_OLD"
            }
  dynamo_table.delete_item(options).attributes ? true : false
end

#delete_allObject

Deletes all records without instantiating them first.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ocean-dynamo/persistence.rb', line 51

def delete_all
  _late_connect?
  ean = { "#H" => table_hash_key }
  ean["#R"] = table_range_key if table_range_key
  options = {
    consistent_read: true,
    projection_expression: "#H" + (table_range_key ? ", #R" : ""),
    expression_attribute_names: ean
  }
  in_batches :scan, options do |attrs|
    if table_range_key
      delete attrs[table_hash_key.to_s], attrs[table_range_key.to_s]
    else
      delete attrs[table_hash_key.to_s]
    end
  end
  nil
end

#destroy_allObject

Destroys all records after first instantiating them.



74
75
76
77
78
79
80
# File 'lib/ocean-dynamo/persistence.rb', line 74

def destroy_all
  _late_connect?
  in_batches :scan, { consistent_read: true } do |attrs|
    new._setup_from_dynamo(attrs).destroy
  end
  nil
end