Class: Dynamini::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks, BatchOperations, Querying
Includes:
ActiveModel::Validations, ClientInterface, Dirty, Increment, TypeHandler
Defined in:
lib/dynamini/base.rb

Overview

Core db interface class.

Constant Summary

Constants included from Querying

Querying::OPTIONAL_QUERY_PARAMS

Constants included from TypeHandler

TypeHandler::GETTER_PROCS, TypeHandler::SETTER_PROCS

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BatchOperations

batch_delete, batch_find, dynamo_batch_save, import

Methods included from Querying

exists?, find, find_or_new, query

Methods included from TypeHandler

#handles, included

Methods included from Increment

#increment!

Methods included from Dirty

#changed, #changes, #mark, #new_record?

Methods included from ClientInterface

#delete_from_dynamo, included, #increment_to_dynamo, #save_to_dynamo, #touch_to_dynamo

Constructor Details

#initialize(attributes = {}, new_record = true) ⇒ Base

Instance Methods



72
73
74
75
76
77
78
79
# File 'lib/dynamini/base.rb', line 72

def initialize(attributes = {}, new_record = true)
  @new_record = new_record
  @attributes = {}
  clear_changes
  attributes.each do |k, v|
    write_attribute(k, v, new_record)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/dynamini/base.rb', line 182

def method_missing(name, *args, &block)
  if write_method?(name)
    write_attribute(attribute_name(name), args.first)
  elsif was_method?(name)
    __was(name)
  elsif read_method?(name)
    read_attribute(name)
  else
    super
  end
end

Class Attribute Details

.range_keyObject (readonly)

Returns the value of attribute range_key.



32
33
34
# File 'lib/dynamini/base.rb', line 32

def range_key
  @range_key
end

.secondary_indexObject (readonly)

Returns the value of attribute secondary_index.



32
33
34
# File 'lib/dynamini/base.rb', line 32

def secondary_index
  @secondary_index
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



20
21
22
# File 'lib/dynamini/base.rb', line 20

def attributes
  @attributes
end

Class Method Details

.create(attributes, options = {}) ⇒ Object



59
60
61
62
# File 'lib/dynamini/base.rb', line 59

def create(attributes, options = {})
  model = new(attributes, true)
  model if model.save(options)
end

.create!(attributes, options = {}) ⇒ Object



64
65
66
67
# File 'lib/dynamini/base.rb', line 64

def create!(attributes, options = {})
  model = new(attributes, true)
  model if model.save!(options)
end

.create_key_hash(hash_value, range_value = nil) ⇒ Object



168
169
170
171
172
# File 'lib/dynamini/base.rb', line 168

def self.create_key_hash(hash_value, range_value = nil)
  key_hash = {self.hash_key => hash_value}
  key_hash[self.range_key] = range_value if self.range_key
  key_hash
end

.hash_keyObject



55
56
57
# File 'lib/dynamini/base.rb', line 55

def hash_key
  @hash_key || :id
end

.set_hash_key(key) ⇒ Object



42
43
44
# File 'lib/dynamini/base.rb', line 42

def set_hash_key(key)
  @hash_key = key
end

.set_range_key(key) ⇒ Object



46
47
48
# File 'lib/dynamini/base.rb', line 46

def set_range_key(key)
  @range_key = key
end

.set_secondary_index(index_name, args) ⇒ Object



50
51
52
53
# File 'lib/dynamini/base.rb', line 50

def set_secondary_index(index_name, args)
  @secondary_index ||= {}
  @secondary_index[index_name.to_s] = {hash_key_name: args[:hash_key] || hash_key, range_key_name: args[:range_key]}
end

.set_table_name(name) ⇒ Object



38
39
40
# File 'lib/dynamini/base.rb', line 38

def set_table_name(name)
  @table_name = name
end

.table_nameObject



34
35
36
# File 'lib/dynamini/base.rb', line 34

def table_name
  @table_name ||= name.demodulize.tableize
end

Instance Method Details

#==(other) ⇒ Object



85
86
87
# File 'lib/dynamini/base.rb', line 85

def ==(other)
  hash_key == other.hash_key if other.is_a?(self.class)
end

#assign_attributes(attributes) ⇒ Object



89
90
91
92
93
94
# File 'lib/dynamini/base.rb', line 89

def assign_attributes(attributes)
  attributes.each do |key, value|
    write_attribute(key, value)
  end
  nil
end

#deleteObject



136
137
138
139
# File 'lib/dynamini/base.rb', line 136

def delete
  delete_from_dynamo
  self
end

#keysObject



81
82
83
# File 'lib/dynamini/base.rb', line 81

def keys
  [self.class.hash_key, self.class.range_key]
end

#save(options = {}) ⇒ Object



106
107
108
109
110
# File 'lib/dynamini/base.rb', line 106

def save(options = {})
  run_callbacks :save do
    @changes.empty? || (valid? && trigger_save(options))
  end
end

#save!(options = {}) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/dynamini/base.rb', line 112

def save!(options = {})
  run_callbacks :save do

    options[:validate] = true if options[:validate].nil?

    unless @changes.empty?
      if (options[:validate] && valid?) || !options[:validate]
        trigger_save(options)
      else
        raise StandardError, errors.full_messages
      end
    end
  end
end

#touch(options = {validate: true}) ⇒ Object

Raises:

  • (RuntimeError)


127
128
129
130
131
132
133
134
# File 'lib/dynamini/base.rb', line 127

def touch(options = {validate: true})
  raise RuntimeError, 'Cannot touch a new record.' if new_record?
  if (options[:validate] && valid?) || !options[:validate]
    trigger_touch
  else
    raise StandardError, errors.full_messages
  end
end

#update_attribute(key, value, options = {}) ⇒ Object



96
97
98
99
# File 'lib/dynamini/base.rb', line 96

def update_attribute(key, value, options = {})
  write_attribute(key, value)
  save!(options)
end

#update_attributes(attributes, options = {}) ⇒ Object



101
102
103
104
# File 'lib/dynamini/base.rb', line 101

def update_attributes(attributes, options = {})
  assign_attributes(attributes)
  save!(options)
end