module Commutator::Model

Basic CRUD wrapper for items in a dynamodb table.

This module is focused on collections of homogenous items within a single table.

TODO: support multiple tables

class Person

include Commutator::Model

attribute :first_name,
          :last_name,
          :email

attribute :age, type: :integer
attribute :favorite_color, writer: false

primary_key hash: :email, range: :age

def favorite_color=(color)
  @color = color.downcase
end

end

Public Class Methods

new(attrs = {}) click to toggle source
# File lib/commutator/model.rb, line 60
def initialize(attrs = {})
  assign_attributes(attrs.symbolize_keys)
end

Public Instance Methods

==(other) click to toggle source
# File lib/commutator/model.rb, line 98
def ==(other)
  self.class == other.class &&
    primary_key_hash == other.primary_key_hash &&
    primary_key_range == other.primary_key_range &&
    attributes == other.attributes
end
delete_item(options = nil) click to toggle source
# File lib/commutator/model.rb, line 86
def delete_item(options = nil)
  dynamo_request(:delete_item, options)
  return false if errors.present?

  @deleted = true
  freeze
end
delete_item_options() click to toggle source
# File lib/commutator/model.rb, line 72
def delete_item_options
  self.class.build_options_proxy(:delete_item, self)
end
deleted?() click to toggle source
# File lib/commutator/model.rb, line 94
def deleted?
  @deleted == true
end
put_item(options = nil) click to toggle source
# File lib/commutator/model.rb, line 76
def put_item(options = nil)
  dynamo_request(:put_item, options) unless invalid?
  errors.empty?
end
put_item_options() click to toggle source
# File lib/commutator/model.rb, line 64
def put_item_options
  self.class.build_options_proxy(:put_item, self)
end
update_item(options = nil) click to toggle source
# File lib/commutator/model.rb, line 81
def update_item(options = nil)
  dynamo_request(:update_item, options) unless invalid?
  errors.empty?
end
update_item_options() click to toggle source
# File lib/commutator/model.rb, line 68
def update_item_options
  self.class.build_options_proxy(:update_item, self)
end

Private Instance Methods

configure_default_delete_item(options) click to toggle source
# File lib/commutator/model.rb, line 117
def configure_default_delete_item(options)
  options
    .table_name(table_name)
    .with_key { |key| key[primary_key_hash_name] = primary_key_hash }

  return unless primary_key_range.present?

  options.with_key { |key| key[primary_key_range_name] = primary_key_range }
end
configure_default_put_item(options) click to toggle source
# File lib/commutator/model.rb, line 107
def configure_default_put_item(options)
  options
    .table_name(table_name)
    .item(attributes.stringify_keys)
end
configure_default_update_item(options) click to toggle source
# File lib/commutator/model.rb, line 113
def configure_default_update_item(options)
  options.table_name(table_name)
end
dynamo_request(operation, options) click to toggle source
# File lib/commutator/model.rb, line 127
def dynamo_request(operation, options)
  options ||= self.class.options_class(operation).new
  run_before_hooks(operation, options)
  client.send(operation, options)
rescue Aws::DynamoDB::Errors::ValidationException,
       Aws::DynamoDB::Errors::ServiceError => e
  errors.add(:base, e.message)
end