Module: DynamoRecord::Persistence

Extended by:
ActiveSupport::Concern
Included in:
Document
Defined in:
lib/dynamo_record/persistence.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#destroyObject



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/dynamo_record/persistence.rb', line 105

def destroy
  options = self.class.default_options
  key = { 'id' => self.id }
  if range_key = self.class.range_key
    key[range_key] = self.class.dump_field(self.read_attribute(range_key), self.class.attributes[range_key])
  end

  response = self.class.client.delete_item(
    options.merge(key: key )
  )
end

#saveObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/dynamo_record/persistence.rb', line 89

def save
  options = self.class.default_options
  if id.nil?
    self.id = SecureRandom.uuid
  elsif @new_record # New item. Don't overwrite if id exists
    options.merge!(condition_expression: 'id <> :s', expression_attribute_values: { ':s' => self.id })
  end

  options.merge!(item: self.class.unload(attributes))
  response = self.class.client.put_item(options)
  @new_record = false
  true
rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException
  false
end