Class: Dynamini::Base
- Inherits:
-
Object
- Object
- Dynamini::Base
- Extended by:
- ActiveModel::Callbacks, BatchOperations, Querying, TypeHandler
- Includes:
- ActiveModel::Validations, Attributes, ClientInterface, Dirty, Increment
- 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
Constants included from Attributes
Attributes::ADDABLE_TYPES, Attributes::DELETED_TOKEN
Class Attribute Summary collapse
-
.range_key ⇒ Object
readonly
Returns the value of attribute range_key.
-
.secondary_index ⇒ Object
readonly
Returns the value of attribute secondary_index.
Attributes included from Attributes
Class Method Summary collapse
- .create(attributes, options = {}) ⇒ Object
- .create!(attributes, options = {}) ⇒ Object
- .hash_key ⇒ Object
- .set_hash_key(key, format = nil) ⇒ Object
- .set_range_key(key, format = nil) ⇒ Object
- .set_secondary_index(index_name, args = {}) ⇒ Object
- .set_table_name(name) ⇒ Object
- .table_name ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #delete ⇒ Object
-
#initialize(attributes = {}, new_record = true) ⇒ Base
constructor
Instance Methods.
- #keys ⇒ Object
- #save(options = {}) ⇒ Object
- #save!(options = {}) ⇒ Object
- #touch(options = {validate: true}) ⇒ Object
Methods included from BatchOperations
batch_delete, batch_find, import, scan
Methods included from Querying
exists?, find, find_or_new, find_or_nil, query
Methods included from TypeHandler
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 Attributes
#add_to, #assign_attributes, #delete_attribute, #delete_attribute!, #handled_attributes, #inspect, #update_attribute, #update_attributes
Methods included from Increment
Methods included from Dirty
#assign_transient_attribute, #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
78 79 80 81 82 83 84 85 |
# 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 |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Dynamini::Attributes
Class Attribute Details
.range_key ⇒ Object (readonly)
Returns the value of attribute range_key.
36 37 38 |
# File 'lib/dynamini/base.rb', line 36 def range_key @range_key end |
.secondary_index ⇒ Object (readonly)
Returns the value of attribute secondary_index.
36 37 38 |
# File 'lib/dynamini/base.rb', line 36 def secondary_index @secondary_index end |
Class Method Details
.create(attributes, options = {}) ⇒ Object
65 66 67 68 |
# File 'lib/dynamini/base.rb', line 65 def create(attributes, = {}) model = new(attributes, true) model if model.save() end |
.create!(attributes, options = {}) ⇒ Object
70 71 72 73 |
# File 'lib/dynamini/base.rb', line 70 def create!(attributes, = {}) model = new(attributes, true) model if model.save!() end |
.hash_key ⇒ Object
61 62 63 |
# File 'lib/dynamini/base.rb', line 61 def hash_key @hash_key || :id end |
.set_hash_key(key, format = nil) ⇒ Object
46 47 48 49 |
# 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) ⇒ Object
51 52 53 54 |
# 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 = {}) ⇒ Object
56 57 58 59 |
# 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) ⇒ Object
42 43 44 |
# File 'lib/dynamini/base.rb', line 42 def set_table_name(name) @table_name = name end |
.table_name ⇒ Object
38 39 40 |
# File 'lib/dynamini/base.rb', line 38 def table_name @table_name ||= name.demodulize.tableize end |
Instance Method Details
#==(other) ⇒ Object
91 92 93 |
# File 'lib/dynamini/base.rb', line 91 def ==(other) hash_key == other.hash_key if other.is_a?(self.class) end |
#delete ⇒ Object
124 125 126 127 |
# File 'lib/dynamini/base.rb', line 124 def delete delete_from_dynamo self end |
#keys ⇒ Object
87 88 89 |
# File 'lib/dynamini/base.rb', line 87 def keys [self.class.hash_key, self.class.range_key] end |
#save(options = {}) ⇒ Object
95 96 97 98 99 |
# File 'lib/dynamini/base.rb', line 95 def save( = {}) run_callbacks :save do @changes.empty? || (valid? && trigger_save()) end end |
#save!(options = {}) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/dynamini/base.rb', line 101 def save!( = {}) run_callbacks :save do [:validate] = true if [:validate].nil? unless @changes.empty? if ([:validate] && valid?) || ![:validate] trigger_save() else raise StandardError, errors. end end end end |
#touch(options = {validate: true}) ⇒ Object
115 116 117 118 119 120 121 122 |
# File 'lib/dynamini/base.rb', line 115 def touch( = {validate: true}) raise RuntimeError, 'Cannot touch a new record.' if new_record? if ([:validate] && valid?) || ![:validate] trigger_touch else raise StandardError, errors. end end |