Class: Dynamini::Base
Overview
Constant Summary
Constants included
from Querying
Querying::OPTIONAL_QUERY_PARAMS
Constants included
from TypeHandler
TypeHandler::GETTER_PROCS, TypeHandler::SETTER_PROCS
Constants included
from Adder
Adder::ADDABLE_TYPES
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
attribute_callback, convert_elements, define_handled_getter, define_handled_setter, format_default, handle, handled_as?, handled_key, invalid_enumerable_value?, should_convert_elements?, validate_handle
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
79
80
81
82
83
84
85
86
|
# File 'lib/dynamini/base.rb', line 79
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
189
190
191
192
193
194
195
196
197
198
199
|
# File 'lib/dynamini/base.rb', line 189
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.
37
38
39
|
# File 'lib/dynamini/base.rb', line 37
def range_key
@range_key
end
|
.secondary_index ⇒ Object
Returns the value of attribute secondary_index.
37
38
39
|
# File 'lib/dynamini/base.rb', line 37
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
66
67
68
69
|
# File 'lib/dynamini/base.rb', line 66
def create(attributes, options = {})
model = new(attributes, true)
model if model.save(options)
end
|
.create!(attributes, options = {}) ⇒ Object
71
72
73
74
|
# File 'lib/dynamini/base.rb', line 71
def create!(attributes, options = {})
model = new(attributes, true)
model if model.save!(options)
end
|
.create_key_hash(hash_value, range_value = nil) ⇒ Object
174
175
176
177
178
179
|
# File 'lib/dynamini/base.rb', line 174
def self.create_key_hash(hash_value, range_value = nil)
key_hash = {self.hash_key => handled_key(self.hash_key, hash_value)}
key_hash[self.range_key] = handled_key(self.range_key, range_value) if self.range_key
key_hash
end
|
.hash_key ⇒ Object
62
63
64
|
# File 'lib/dynamini/base.rb', line 62
def hash_key
@hash_key || :id
end
|
.set_hash_key(key, format = nil) ⇒ Object
47
48
49
50
|
# File 'lib/dynamini/base.rb', line 47
def set_hash_key(key, format = nil)
@hash_key = key
handle(key, format) if format
end
|
.set_range_key(key, format = nil) ⇒ Object
52
53
54
55
|
# File 'lib/dynamini/base.rb', line 52
def set_range_key(key, format = nil)
@range_key = key
handle(key, format) if format
end
|
.set_secondary_index(index_name, args) ⇒ Object
57
58
59
60
|
# File 'lib/dynamini/base.rb', line 57
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
43
44
45
|
# File 'lib/dynamini/base.rb', line 43
def set_table_name(name)
@table_name = name
end
|
.table_name ⇒ Object
39
40
41
|
# File 'lib/dynamini/base.rb', line 39
def table_name
@table_name ||= name.demodulize.tableize
end
|
Instance Method Details
#==(other) ⇒ Object
92
93
94
|
# File 'lib/dynamini/base.rb', line 92
def ==(other)
hash_key == other.hash_key if other.is_a?(self.class)
end
|
#assign_attributes(attributes) ⇒ Object
96
97
98
99
100
101
|
# File 'lib/dynamini/base.rb', line 96
def assign_attributes(attributes)
attributes.each do |key, value|
write_attribute(key, value)
end
nil
end
|
#delete ⇒ Object
142
143
144
145
|
# File 'lib/dynamini/base.rb', line 142
def delete
delete_from_dynamo
self
end
|
#keys ⇒ Object
88
89
90
|
# File 'lib/dynamini/base.rb', line 88
def keys
[self.class.hash_key, self.class.range_key]
end
|
#save(options = {}) ⇒ Object
113
114
115
116
117
|
# File 'lib/dynamini/base.rb', line 113
def save(options = {})
run_callbacks :save do
@changes.empty? || (valid? && trigger_save(options))
end
end
|
#save!(options = {}) ⇒ Object
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/dynamini/base.rb', line 119
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
133
134
135
136
137
138
139
140
|
# File 'lib/dynamini/base.rb', line 133
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
103
104
105
106
|
# File 'lib/dynamini/base.rb', line 103
def update_attribute(key, value, options = {})
write_attribute(key, value)
save!(options)
end
|
#update_attributes(attributes, options = {}) ⇒ Object
108
109
110
111
|
# File 'lib/dynamini/base.rb', line 108
def update_attributes(attributes, options = {})
assign_attributes(attributes)
save!(options)
end
|