Module: Modis::Persistence
- Defined in:
- lib/modis/persistence.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #destroy ⇒ Object
- #key ⇒ Object
- #new_record? ⇒ Boolean
- #persisted? ⇒ Boolean
- #reload ⇒ Object
- #save(args = {}) ⇒ Object
- #save!(args = {}) ⇒ Object
- #update_attribute(name, value) ⇒ Object
- #update_attributes(attrs) ⇒ Object
- #update_attributes!(attrs) ⇒ Object
Class Method Details
.included(base) ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/modis/persistence.rb', line 5 def self.included(base) base.extend ClassMethods base.instance_eval do class << self attr_reader :sti_child alias sti_child? sti_child end end end |
Instance Method Details
#destroy ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/modis/persistence.rb', line 126 def destroy self.class.transaction do |redis| run_callbacks :destroy do redis.pipelined do remove_from_indexes(redis) redis.srem(self.class.key_for(:all), id) redis.srem(self.class.sti_base_key_for(:all), id) if self.class.sti_child? redis.del(key) end end end end |
#key ⇒ Object
107 108 109 110 |
# File 'lib/modis/persistence.rb', line 107 def key return nil if new_record? self.class.sti_child? ? self.class.sti_base_key_for(id) : self.class.key_for(id) end |
#new_record? ⇒ Boolean
112 113 114 |
# File 'lib/modis/persistence.rb', line 112 def new_record? defined?(@new_record) ? @new_record : true end |
#persisted? ⇒ Boolean
103 104 105 |
# File 'lib/modis/persistence.rb', line 103 def persisted? true end |
#reload ⇒ Object
139 140 141 142 143 |
# File 'lib/modis/persistence.rb', line 139 def reload new_attributes = Modis.with_connection { |redis| self.class.attributes_for(redis, id) } initialize(new_attributes) self end |
#save(args = {}) ⇒ Object
116 117 118 119 120 |
# File 'lib/modis/persistence.rb', line 116 def save(args = {}) create_or_update(args) rescue Modis::RecordInvalid false end |
#save!(args = {}) ⇒ Object
122 123 124 |
# File 'lib/modis/persistence.rb', line 122 def save!(args = {}) create_or_update(args) || (raise RecordNotSaved) end |
#update_attribute(name, value) ⇒ Object
145 146 147 148 |
# File 'lib/modis/persistence.rb', line 145 def update_attribute(name, value) assign_attributes(name => value) save(validate: false) end |
#update_attributes(attrs) ⇒ Object
150 151 152 153 |
# File 'lib/modis/persistence.rb', line 150 def update_attributes(attrs) assign_attributes(attrs) save end |
#update_attributes!(attrs) ⇒ Object
155 156 157 158 |
# File 'lib/modis/persistence.rb', line 155 def update_attributes!(attrs) assign_attributes(attrs) save! end |