class Dinamo::Adapter
Public Class Methods
new(table_name: nil, **options)
click to toggle source
# 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
Public Instance Methods
delete(key)
click to toggle source
# File lib/dinamo/adapter.rb, line 60 def delete(key) find_or_abort!(**key) @database.delete_item( table_name: @table_name, key: key ) end
exist?(**keys)
click to toggle source
# File lib/dinamo/adapter.rb, line 74 def exist?(**keys) !!get(**keys).item rescue false end
get(**keys)
click to toggle source
# 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)
click to toggle source
# File lib/dinamo/adapter.rb, line 38 def insert(**keys) @database.put_item( table_name: @table_name, item: keys, return_values: "ALL_OLD" ) rescue handle_error! $! end
partition(conditions)
click to toggle source
# File lib/dinamo/adapter.rb, line 29 def partition(conditions) @database.query( table_name: @table_name, key_conditions: make_key_conditions(**conditions) ) rescue handle_error! $! end
serialize_attributes(attributes)
click to toggle source
# File lib/dinamo/adapter.rb, line 68 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)
click to toggle source
# File lib/dinamo/adapter.rb, line 48 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
Private Instance Methods
find_or_abort!(**key)
click to toggle source
# File lib/dinamo/adapter.rb, line 80 def find_or_abort!(**key) item = get(**key) fail Exceptions::RecordNotFoundError, "Corresponding record (%p) can not be found" % key unless item && item.item item end
handle_error!(evar)
click to toggle source
# File lib/dinamo/adapter.rb, line 87 def handle_error!(evar) case evar when Aws::DynamoDB::Errors::ValidationException fail Dinamo::Exceptions::ValidationError, evar.message when Aws::DynamoDB::Errors::ResourceNotFoundException fail Dinamo::Exceptions::ResourceNotFoundError, evar.message else fail evar, evar.message end end
make_key_conditions(**conditions)
click to toggle source
# File lib/dinamo/adapter.rb, line 98 def make_key_conditions(**conditions) conditions.each_with_object({}) do |(key, val), serialized| serialized[key.to_s] = { attribute_value_list: val === Array ? val : Array(val), comparison_operator: "EQ" } end end