Class: DashOverlord::Models::V1::DynamoDb::Base

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/dash_overlord/models/v1/dynamo_db/base.rb

Direct Known Subclasses

Scan, Video

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allObject



35
36
37
38
39
40
# File 'lib/dash_overlord/models/v1/dynamo_db/base.rb', line 35

def self.all
  DynamoDb::Scan.new \
    client: client,
    table_name: table_name,
    resource_class: self
end

.clientObject



8
9
10
# File 'lib/dash_overlord/models/v1/dynamo_db/base.rb', line 8

def self.client
  @client ||= Aws::DynamoDB::Client.new
end

.create(attributes = {}) ⇒ Object



19
20
21
22
# File 'lib/dash_overlord/models/v1/dynamo_db/base.rb', line 19

def self.create(attributes = {})
  client.put_item \
    create_params(attributes)
end

.create_all(items) ⇒ Object



12
13
14
15
16
17
# File 'lib/dash_overlord/models/v1/dynamo_db/base.rb', line 12

def self.create_all(items)
  items.each_slice(25) do |batch|
    client.batch_write_item \
      create_all_params(batch)
  end
end

.find_by(attribute) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/dash_overlord/models/v1/dynamo_db/base.rb', line 24

def self.find_by(attribute)
  result = @client.get_item({
    table_name: table_name,
    key: attribute
  })

  return nil unless result.item

  self.new(result.item)
end

.ransack(filters) ⇒ Object

def self.page(page, last_evaluated_key)

DynamoDb::Scan.new \
  current_page: page,
  last_evaluated_key: last_evaluated_key,
  client: client,
  table_name: table_name,
  resource_class: self

end



55
56
57
58
59
60
61
62
63
# File 'lib/dash_overlord/models/v1/dynamo_db/base.rb', line 55

def self.ransack(filters)
  return unless filters

  DynamoDb::Scan.new \
    client: client,
    table_name: table_name,
    resource_class: self,
    filters: filters
end

.table_nameObject



65
66
67
68
69
# File 'lib/dash_overlord/models/v1/dynamo_db/base.rb', line 65

def self.table_name
  return @table_name if defined?(@table_name)

  @table_name = name.split('::').last.underscore
end

Instance Method Details

#assign_attributes(attributes) ⇒ Object



73
74
75
# File 'lib/dash_overlord/models/v1/dynamo_db/base.rb', line 73

def assign_attributes(attributes)
  attributes.each { |key, value| send("#{key}=", value) }
end

#result(args) ⇒ Object



42
43
44
# File 'lib/dash_overlord/models/v1/dynamo_db/base.rb', line 42

def result(args)
  self
end