Class: Dinamo::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/dinamo/adapter.rb

Defined Under Namespace

Modules: Glue

Instance Method Summary collapse

Constructor Details

#initialize(table_name: nil, **options) ⇒ Adapter

Returns a new instance of Adapter.



17
18
19
20
21
# File 'lib/dinamo/adapter.rb', line 17

def initialize(table_name: nil, **options)
  @options    = options
  @database   = Aws::DynamoDB::Client.new
  @table_name = table_name
end

Instance Method Details

#delete(key) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/dinamo/adapter.rb', line 51

def delete(key)
  find_or_abort!(**key)
  @database.update_item(
    table_name: @table_name,
    key: key
  )
end

#exist?(**keys) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/dinamo/adapter.rb', line 65

def exist?(**keys)
  !!get(**keys).item rescue false
end

#get(**keys) ⇒ Object



23
24
25
26
27
# File 'lib/dinamo/adapter.rb', line 23

def get(**keys)
  @database.get_item(table_name: @table_name, key: keys)
rescue
  handle_error! $!
end

#insert(**keys) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/dinamo/adapter.rb', line 29

def insert(**keys)
  @database.put_item(
    table_name: @table_name,
    item: keys,
    return_values: "ALL_OLD"
  )
rescue
  handle_error! $!
end

#serialize_attributes(attributes) ⇒ Object



59
60
61
62
63
# File 'lib/dinamo/adapter.rb', line 59

def serialize_attributes(attributes)
  attributes.each_with_object({}) do |(key, value), new_attributes|
    new_attributes[key] = { value: value, action: "PUT" }
  end
end

#update(key, attributes) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dinamo/adapter.rb', line 39

def update(key, attributes)
  find_or_abort!(**key)
  @database.update_item(
    table_name: @table_name,
    key: key,
    attribute_updates: serialize_attributes(attributes),
    return_values: "ALL_NEW"
  )
rescue
  handle_error! $!
end