Class: Dynamini::Base
Overview
Constant Summary
Constants included
from Querying
Querying::OPTIONAL_QUERY_PARAMS
Constants included
from Adder
Adder::ADDABLE_TYPES
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
batch_delete, batch_find, import
Methods included from Querying
exists?, find, find_or_new, find_or_nil, query
Methods included from Adder
#add_to
Methods included from Increment
#increment!
Methods included from Dirty
#changed, #changes, #mark, #new_record?
#delete_from_dynamo, included, #increment_to_dynamo, #save_to_dynamo, #touch_to_dynamo
Constructor Details
#initialize(attributes = {}, new_record = true) ⇒ Base
75
76
77
78
79
80
81
82
|
# File 'lib/dynamini/base.rb', line 75
def initialize(attributes = {}, new_record = true)
@new_record = new_record
@attributes = {}
clear_changes
attributes.each do |k, v|
write_attribute(k, v, change: new_record)
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
185
186
187
188
189
190
191
192
193
194
195
|
# File 'lib/dynamini/base.rb', line 185
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_key ⇒ Object
Returns the value of attribute range_key.
35
36
37
|
# File 'lib/dynamini/base.rb', line 35
def range_key
@range_key
end
|
.secondary_index ⇒ Object
Returns the value of attribute secondary_index.
35
36
37
|
# File 'lib/dynamini/base.rb', line 35
def secondary_index
@secondary_index
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
23
24
25
|
# File 'lib/dynamini/base.rb', line 23
def attributes
@attributes
end
|
Class Method Details
.create(attributes, options = {}) ⇒ Object
62
63
64
65
|
# File 'lib/dynamini/base.rb', line 62
def create(attributes, options = {})
model = new(attributes, true)
model if model.save(options)
end
|
.create!(attributes, options = {}) ⇒ Object
67
68
69
70
|
# File 'lib/dynamini/base.rb', line 67
def create!(attributes, options = {})
model = new(attributes, true)
model if model.save!(options)
end
|
.hash_key ⇒ Object
58
59
60
|
# File 'lib/dynamini/base.rb', line 58
def hash_key
@hash_key || :id
end
|
.set_hash_key(key) ⇒ Object
45
46
47
|
# File 'lib/dynamini/base.rb', line 45
def set_hash_key(key)
@hash_key = key
end
|
.set_range_key(key) ⇒ Object
49
50
51
|
# File 'lib/dynamini/base.rb', line 49
def set_range_key(key)
@range_key = key
end
|
.set_secondary_index(index_name, args) ⇒ Object
53
54
55
56
|
# File 'lib/dynamini/base.rb', line 53
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
41
42
43
|
# File 'lib/dynamini/base.rb', line 41
def set_table_name(name)
@table_name = name
end
|
.table_name ⇒ Object
37
38
39
|
# File 'lib/dynamini/base.rb', line 37
def table_name
@table_name ||= name.demodulize.tableize
end
|
Instance Method Details
#==(other) ⇒ Object
88
89
90
|
# File 'lib/dynamini/base.rb', line 88
def ==(other)
hash_key == other.hash_key if other.is_a?(self.class)
end
|
#assign_attributes(attributes) ⇒ Object
92
93
94
95
96
97
|
# File 'lib/dynamini/base.rb', line 92
def assign_attributes(attributes)
attributes.each do |key, value|
write_attribute(key, value)
end
nil
end
|
#delete ⇒ Object
139
140
141
142
|
# File 'lib/dynamini/base.rb', line 139
def delete
delete_from_dynamo
self
end
|
#keys ⇒ Object
84
85
86
|
# File 'lib/dynamini/base.rb', line 84
def keys
[self.class.hash_key, self.class.range_key]
end
|
#save(options = {}) ⇒ Object
109
110
111
112
113
|
# File 'lib/dynamini/base.rb', line 109
def save(options = {})
run_callbacks :save do
@changes.empty? || (valid? && trigger_save(options))
end
end
|
#save!(options = {}) ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/dynamini/base.rb', line 115
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
130
131
132
133
134
135
136
137
|
# File 'lib/dynamini/base.rb', line 130
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
99
100
101
102
|
# File 'lib/dynamini/base.rb', line 99
def update_attribute(key, value, options = {})
write_attribute(key, value)
save!(options)
end
|
#update_attributes(attributes, options = {}) ⇒ Object
104
105
106
107
|
# File 'lib/dynamini/base.rb', line 104
def update_attributes(attributes, options = {})
assign_attributes(attributes)
save!(options)
end
|