class Dynamini::Base
Core db interface class.
Attributes
range_key[R]
secondary_index[R]
Public Class Methods
create(attributes, options = {})
click to toggle source
# File lib/dynamini/base.rb, line 65 def create(attributes, options = {}) model = new(attributes, true) model if model.save(options) end
create!(attributes, options = {})
click to toggle source
# File lib/dynamini/base.rb, line 70 def create!(attributes, options = {}) model = new(attributes, true) model if model.save!(options) end
hash_key()
click to toggle source
# File lib/dynamini/base.rb, line 61 def hash_key @hash_key || :id end
new(attributes = {}, new_record = true)
click to toggle source
Instance Methods
# File lib/dynamini/base.rb, line 78 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
set_hash_key(key, format = nil)
click to toggle source
# File lib/dynamini/base.rb, line 46 def set_hash_key(key, format = nil) @hash_key = key handle(key, format) if format end
set_range_key(key, format = nil)
click to toggle source
# File lib/dynamini/base.rb, line 51 def set_range_key(key, format = nil) @range_key = key handle(key, format) if format end
set_secondary_index(index_name, args = {})
click to toggle source
# File lib/dynamini/base.rb, line 56 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)
click to toggle source
# File lib/dynamini/base.rb, line 42 def set_table_name(name) @table_name = name end
table_name()
click to toggle source
# File lib/dynamini/base.rb, line 38 def table_name @table_name ||= name.demodulize.tableize end
Private Class Methods
create_key_hash(hash_value, range_value = nil)
click to toggle source
# File lib/dynamini/base.rb, line 159 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
Public Instance Methods
==(other)
click to toggle source
# File lib/dynamini/base.rb, line 91 def ==(other) hash_key == other.hash_key if other.is_a?(self.class) end
delete()
click to toggle source
# File lib/dynamini/base.rb, line 124 def delete delete_from_dynamo self end
keys()
click to toggle source
# File lib/dynamini/base.rb, line 87 def keys [self.class.hash_key, self.class.range_key] end
save(options = {})
click to toggle source
# File lib/dynamini/base.rb, line 95 def save(options = {}) run_callbacks :save do @changes.empty? || (valid? && trigger_save(options)) end end
save!(options = {})
click to toggle source
# File lib/dynamini/base.rb, line 101 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})
click to toggle source
# File lib/dynamini/base.rb, line 115 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
Private Instance Methods
generate_timestamps!()
click to toggle source
# File lib/dynamini/base.rb, line 148 def generate_timestamps! self.updated_at = Time.now.to_f self.created_at = Time.now.to_f if new_record? end
key()
click to toggle source
# File lib/dynamini/base.rb, line 153 def key key_hash = {self.class.hash_key => @attributes[self.class.hash_key]} key_hash[self.class.range_key] = @attributes[self.class.range_key] if self.class.range_key key_hash end
trigger_save(options = {})
click to toggle source
# File lib/dynamini/base.rb, line 131 def trigger_save(options = {}) generate_timestamps! unless options[:skip_timestamps] updates = ItemSplitter.split(attribute_updates) updates.each do |u| save_to_dynamo(u) end clear_changes @new_record = false true end
trigger_touch()
click to toggle source
# File lib/dynamini/base.rb, line 142 def trigger_touch generate_timestamps! touch_to_dynamo true end