Module: Commutator::Model

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Model, Attributes, Hooks, TableConfiguration
Defined in:
lib/commutator/model.rb,
lib/commutator/model/hooks.rb,
lib/commutator/model/attributes.rb,
lib/commutator/model/table_configuration.rb

Overview

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

Defined Under Namespace

Modules: Attributes, ClassMethods, Hooks, TableConfiguration

Instance Method Summary collapse

Methods included from Hooks

#run_before_hooks

Methods included from TableConfiguration

#primary_key_hash, #primary_key_range, #table_name

Methods included from Attributes

#assign_attributes, #attribute_names, #attributes

Instance Method Details

#==(other) ⇒ Object



93
94
95
96
97
98
# File 'lib/commutator/model.rb', line 93

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) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/commutator/model.rb', line 81

def delete_item(options = nil)
  dynamo_request(:delete_item, options)
  return false if errors.present?

  @deleted = true
  freeze
end

#delete_item_optionsObject



67
68
69
# File 'lib/commutator/model.rb', line 67

def delete_item_options
  self.class.build_options_proxy(:delete_item, self)
end

#deleted?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/commutator/model.rb', line 89

def deleted?
  @deleted == true
end

#initialize(attrs = {}) ⇒ Object



55
56
57
# File 'lib/commutator/model.rb', line 55

def initialize(attrs = {})
  assign_attributes(attrs.symbolize_keys)
end

#put_item(options = nil) ⇒ Object



71
72
73
74
# File 'lib/commutator/model.rb', line 71

def put_item(options = nil)
  dynamo_request(:put_item, options) unless invalid?
  errors.empty?
end

#put_item_optionsObject



59
60
61
# File 'lib/commutator/model.rb', line 59

def put_item_options
  self.class.build_options_proxy(:put_item, self)
end

#update_item(options = nil) ⇒ Object



76
77
78
79
# File 'lib/commutator/model.rb', line 76

def update_item(options = nil)
  dynamo_request(:update_item, options) unless invalid?
  errors.empty?
end

#update_item_optionsObject



63
64
65
# File 'lib/commutator/model.rb', line 63

def update_item_options
  self.class.build_options_proxy(:update_item, self)
end