Module: SimpleMapper::Persistence
- Defined in:
- lib/simple_mapper/persistence.rb
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#identifier ⇒ Object
readonly
Returns the value of attribute identifier.
-
#original_attributes ⇒ Object
readonly
Returns the value of attribute original_attributes.
-
#original_data ⇒ Object
Returns the value of attribute original_data.
Class Method Summary collapse
- .included(klass) ⇒ Object
-
.prepare_for_inheritance(klass) ⇒ Object
TODO: Write in inheritability for models.
Instance Method Summary collapse
-
#delete ⇒ Object
delete.
- #dirty? ⇒ Boolean
-
#formatted_data ⇒ Object
Reads the data from the object for saving back to the persisted store.
- #initialize(data = nil) ⇒ Object
- #persisted? ⇒ Boolean
-
#post(*args) ⇒ Object
sends a post request with self.data.
-
#put(*args) ⇒ Object
sends a put request with self.data.
-
#save ⇒ Object
persisted? ? put : post.
Instance Attribute Details
#identifier ⇒ Object (readonly)
Returns the value of attribute identifier.
159 160 161 |
# File 'lib/simple_mapper/persistence.rb', line 159 def identifier @identifier end |
#original_attributes ⇒ Object (readonly)
Returns the value of attribute original_attributes.
166 167 168 |
# File 'lib/simple_mapper/persistence.rb', line 166 def original_attributes @original_attributes end |
#original_data ⇒ Object
Returns the value of attribute original_data.
165 166 167 |
# File 'lib/simple_mapper/persistence.rb', line 165 def original_data @original_data end |
Class Method Details
.included(klass) ⇒ Object
8 9 10 11 12 |
# File 'lib/simple_mapper/persistence.rb', line 8 def self.included(klass) klass.extend ClassMethods require File.(File.dirname(__FILE__) + '/plugin_support') klass.send(:include, SimpleMapper::PluginSupport) end |
.prepare_for_inheritance(klass) ⇒ Object
TODO: Write in inheritability for models. Should this instead be a place that plugins can plug in a callback?
5 6 |
# File 'lib/simple_mapper/persistence.rb', line 5 def self.prepare_for_inheritance(klass) end |
Instance Method Details
#delete ⇒ Object
delete
201 202 203 204 205 206 207 208 209 |
# File 'lib/simple_mapper/persistence.rb', line 201 def delete if self.class.connection(@adapter || :default).delete(identifier) @persisted = false instance_variable_set('@'+self.class.identifier, nil) true else false end end |
#dirty? ⇒ Boolean
174 175 176 |
# File 'lib/simple_mapper/persistence.rb', line 174 def dirty? data != original_data end |
#formatted_data ⇒ Object
Reads the data from the object for saving back to the persisted store. This is provided as a default method, but you can overwrite it in your model.
170 171 172 |
# File 'lib/simple_mapper/persistence.rb', line 170 def formatted_data send("to_#{self.class.format_name}".to_sym) end |
#initialize(data = nil) ⇒ Object
156 157 158 |
# File 'lib/simple_mapper/persistence.rb', line 156 def initialize(data=nil) self.data = data unless data.nil? end |
#persisted? ⇒ Boolean
211 212 213 |
# File 'lib/simple_mapper/persistence.rb', line 211 def persisted? !!@persisted && !self.class.identifier.nil? && !self.send(self.class.identifier).nil? end |
#post(*args) ⇒ Object
sends a post request with self.data
192 193 194 195 196 197 198 |
# File 'lib/simple_mapper/persistence.rb', line 192 def post(*args) new_rec = self.class.extract_one(self.class.connection(@adapter || :default).post(formatted_data, *args)) raise "Request did not return an object" if new_rec.nil? self.data = new_rec.to_hash @persisted = true self end |
#put(*args) ⇒ Object
sends a put request with self.data
184 185 186 187 188 189 |
# File 'lib/simple_mapper/persistence.rb', line 184 def put(*args) new_rec = self.class.extract_one(self.class.connection(@adapter || :default).put(identifier, formatted_data, *args), identifier) raise "Request did not return an object" if new_rec.nil? self.data = new_rec.to_hash self end |
#save ⇒ Object
persisted? ? put : post
179 180 181 |
# File 'lib/simple_mapper/persistence.rb', line 179 def save persisted? ? put : post end |