Class: Dynamini::Base
Overview
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
batch_find, dynamo_batch_save, import
Methods included from Querying
exists?, find, find_or_new, query
#handles, included
Methods included from Increment
#increment!
Methods included from Dirty
#changed, #changes, #new_record?
#delete_from_dynamo, included, #increment_to_dynamo, #save_to_dynamo, #touch_to_dynamo
Constructor Details
#initialize(attributes = {}, new_record = true) ⇒ Base
73
74
75
76
77
78
79
80
|
# File 'lib/dynamini/base.rb', line 73
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
183
184
185
186
187
188
189
190
191
192
193
|
# File 'lib/dynamini/base.rb', line 183
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.
33
34
35
|
# File 'lib/dynamini/base.rb', line 33
def range_key
@range_key
end
|
.secondary_index ⇒ Object
Returns the value of attribute secondary_index.
33
34
35
|
# File 'lib/dynamini/base.rb', line 33
def secondary_index
@secondary_index
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
21
22
23
|
# File 'lib/dynamini/base.rb', line 21
def attributes
@attributes
end
|
Class Method Details
.create(attributes, options = {}) ⇒ Object
60
61
62
63
|
# File 'lib/dynamini/base.rb', line 60
def create(attributes, options = {})
model = new(attributes, true)
model if model.save(options)
end
|
.create!(attributes, options = {}) ⇒ Object
65
66
67
68
|
# File 'lib/dynamini/base.rb', line 65
def create!(attributes, options = {})
model = new(attributes, true)
model if model.save!(options)
end
|
.hash_key ⇒ Object
56
57
58
|
# File 'lib/dynamini/base.rb', line 56
def hash_key
@hash_key || :id
end
|
.set_hash_key(key) ⇒ Object
43
44
45
|
# File 'lib/dynamini/base.rb', line 43
def set_hash_key(key)
@hash_key = key
end
|
.set_range_key(key) ⇒ Object
47
48
49
|
# File 'lib/dynamini/base.rb', line 47
def set_range_key(key)
@range_key = key
end
|
.set_secondary_index(index_name, args) ⇒ Object
51
52
53
54
|
# File 'lib/dynamini/base.rb', line 51
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
39
40
41
|
# File 'lib/dynamini/base.rb', line 39
def set_table_name(name)
@table_name = name
end
|
.table_name ⇒ Object
35
36
37
|
# File 'lib/dynamini/base.rb', line 35
def table_name
@table_name ||= name.demodulize.tableize
end
|
Instance Method Details
#==(other) ⇒ Object
86
87
88
|
# File 'lib/dynamini/base.rb', line 86
def ==(other)
hash_key == other.hash_key if other.is_a?(self.class)
end
|
#assign_attributes(attributes) ⇒ Object
90
91
92
93
94
95
|
# File 'lib/dynamini/base.rb', line 90
def assign_attributes(attributes)
attributes.each do |key, value|
write_attribute(key, value)
end
nil
end
|
#delete ⇒ Object
137
138
139
140
|
# File 'lib/dynamini/base.rb', line 137
def delete
delete_from_dynamo
self
end
|
#keys ⇒ Object
82
83
84
|
# File 'lib/dynamini/base.rb', line 82
def keys
[self.class.hash_key, self.class.range_key]
end
|
#save(options = {}) ⇒ Object
107
108
109
110
111
|
# File 'lib/dynamini/base.rb', line 107
def save(options = {})
run_callbacks :save do
@changes.empty? || (valid? && trigger_save(options))
end
end
|
#save!(options = {}) ⇒ Object
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/dynamini/base.rb', line 113
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
128
129
130
131
132
133
134
135
|
# File 'lib/dynamini/base.rb', line 128
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
97
98
99
100
|
# File 'lib/dynamini/base.rb', line 97
def update_attribute(key, value, options = {})
write_attribute(key, value)
save!(options)
end
|
#update_attributes(attributes, options = {}) ⇒ Object
102
103
104
105
|
# File 'lib/dynamini/base.rb', line 102
def update_attributes(attributes, options = {})
assign_attributes(attributes)
save!(options)
end
|