Class: ROM::DynamoDB::Dataset

Inherits:
Object
  • Object
show all
Defined in:
lib/rom/dynamodb/dataset.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, operation: :query, config: {}, queries: []) ⇒ Dataset



8
9
10
11
12
13
# File 'lib/rom/dynamodb/dataset.rb', line 8

def initialize(name:, operation: :query, config: {}, queries: [])
  @name = name
  @operation = operation
  @config = config
  @queries = queries
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/rom/dynamodb/dataset.rb', line 4

def config
  @config
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/rom/dynamodb/dataset.rb', line 4

def name
  @name
end

#operationObject (readonly)

Returns the value of attribute operation.



4
5
6
# File 'lib/rom/dynamodb/dataset.rb', line 4

def operation
  @operation
end

#queriesObject

Returns the value of attribute queries.



6
7
8
# File 'lib/rom/dynamodb/dataset.rb', line 6

def queries
  @queries
end

Instance Method Details

#after(key, after, predicate = :ge) ⇒ Object



47
48
49
# File 'lib/rom/dynamodb/dataset.rb', line 47

def after(key, after, predicate = :ge)
  restrict_by(key, predicate, [after])
end

#ascendingObject



19
20
21
# File 'lib/rom/dynamodb/dataset.rb', line 19

def ascending
  restrict(scan_index_forward: true)
end

#batch_get(query = nil) ⇒ Object



55
56
57
# File 'lib/rom/dynamodb/dataset.rb', line 55

def batch_get(query = nil)
  append(:batch_get) { query }
end

#before(key, before, predicate = :le) ⇒ Object



51
52
53
# File 'lib/rom/dynamodb/dataset.rb', line 51

def before(key, before, predicate = :le)
  restrict_by(key, predicate, [before])
end

#between(key, after, before, predicate = :between) ⇒ Object



43
44
45
# File 'lib/rom/dynamodb/dataset.rb', line 43

def between(key, after, before, predicate = :between)
  restrict_by(key, predicate, [after, before])
end

#build(parts = queries) ⇒ Object



71
72
73
# File 'lib/rom/dynamodb/dataset.rb', line 71

def build(parts = queries)
  parts.inject(:deep_merge).merge(table_name: name)
end

#connectionObject



101
102
103
# File 'lib/rom/dynamodb/dataset.rb', line 101

def connection
  @connection ||= Aws::DynamoDB::Client.new(config)
end

#create(hash) ⇒ Object



82
83
84
85
# File 'lib/rom/dynamodb/dataset.rb', line 82

def create(hash)
  payload = build([{ item: hash }])
  connection.put_item(payload).attributes
end

#delete(hash) ⇒ Object



87
88
89
90
# File 'lib/rom/dynamodb/dataset.rb', line 87

def delete(hash)
  payload = build([{ key: hash }])
  connection.delete_item(payload).data
end

#descendingObject



23
24
25
# File 'lib/rom/dynamodb/dataset.rb', line 23

def descending
  restrict(scan_index_forward: false)
end

#each(&block) ⇒ Object



97
98
99
# File 'lib/rom/dynamodb/dataset.rb', line 97

def each(&block)
  each_item(build, &block)
end

#equal(key, val, predicate = :eq) ⇒ Object



39
40
41
# File 'lib/rom/dynamodb/dataset.rb', line 39

def equal(key, val, predicate = :eq)
  restrict_by(key, predicate, [val])
end

#execute(query) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/rom/dynamodb/dataset.rb', line 105

def execute(query)
  @response ||= case operation
  when :batch_get
    connection.send(operation, { request_items: { name => query } })
  else
    connection.send(operation, query).data
  end
end

#index(name) ⇒ Object



15
16
17
# File 'lib/rom/dynamodb/dataset.rb', line 15

def index(name)
  restrict(index_name: name)
end

#informationObject



92
93
94
95
# File 'lib/rom/dynamodb/dataset.rb', line 92

def information
  payload = build([{}])
  connection.describe_table(payload).table
end

#limit(num = nil) ⇒ Object



27
28
29
# File 'lib/rom/dynamodb/dataset.rb', line 27

def limit(num = nil)
  append { { limit: num } unless num.nil? }
end

#offset(key) ⇒ Object



31
32
33
# File 'lib/rom/dynamodb/dataset.rb', line 31

def offset(key)
  append { { exclusive_start_key: key } unless key.nil? }
end

#restrict(query = nil) ⇒ Object



59
60
61
# File 'lib/rom/dynamodb/dataset.rb', line 59

def restrict(query = nil)
  append(:query) { query }
end

#retrieve(query = nil) ⇒ Object



63
64
65
# File 'lib/rom/dynamodb/dataset.rb', line 63

def retrieve(query = nil)
  append(:get_item) { query }
end

#scan(query = nil) ⇒ Object



67
68
69
# File 'lib/rom/dynamodb/dataset.rb', line 67

def scan(query = nil)
  append(:scan) { query }
end

#select(keys) ⇒ Object



35
36
37
# File 'lib/rom/dynamodb/dataset.rb', line 35

def select(keys)
  restrict(select: "SPECIFIC_ATTRIBUTES", attributes_to_get: keys.collect(&:to_s))
end

#update(key, hash, action = 'PUT') ⇒ Object



75
76
77
78
79
80
# File 'lib/rom/dynamodb/dataset.rb', line 75

def update(key, hash, action = 'PUT')
  data = hash.delete_if { |k, _| key.keys.include?(k) }
  update = to_update_structure(data)
  payload = build([update, { key: key }])
  connection.update_item(payload).data
end