Module: Flex::ActiveModel::Storage::InstanceMethods
- Defined in:
- lib/flex/active_model/storage.rb
Instance Method Summary collapse
- #delete ⇒ Object
- #destroy ⇒ Object
- #destroyed? ⇒ Boolean
- #merge_attributes(attributes) ⇒ Object
- #new_record? ⇒ Boolean
- #persisted? ⇒ Boolean
- #reload ⇒ Object
-
#safe_update(options = {}, &block) ⇒ Object
Optimistic Lock Update.
- #safe_update!(options = {}, &block) ⇒ Object
- #save(options = {}) ⇒ Object
- #save!(options = {}) ⇒ Object
- #update_attributes(attributes) ⇒ Object
- #valid?(context = nil) ⇒ Boolean
Instance Method Details
#delete ⇒ Object
66 67 68 69 |
# File 'lib/flex/active_model/storage.rb', line 66 def delete @skip_destroy_callbacks = true destroy end |
#destroy ⇒ Object
60 61 62 63 64 |
# File 'lib/flex/active_model/storage.rb', line 60 def destroy @destroyed = true flex.sync self.freeze end |
#destroyed? ⇒ Boolean
80 81 82 |
# File 'lib/flex/active_model/storage.rb', line 80 def destroyed? !!@destroyed end |
#merge_attributes(attributes) ⇒ Object
71 72 73 |
# File 'lib/flex/active_model/storage.rb', line 71 def merge_attributes(attributes) attributes.each {|name, value| send "#{name}=", value } end |
#new_record? ⇒ Boolean
88 89 90 |
# File 'lib/flex/active_model/storage.rb', line 88 def new_record? !@_id || !@_version end |
#persisted? ⇒ Boolean
84 85 86 |
# File 'lib/flex/active_model/storage.rb', line 84 def persisted? !(new_record? || destroyed?) end |
#reload ⇒ Object
21 22 23 24 25 26 |
# File 'lib/flex/active_model/storage.rb', line 21 def reload document = flex.get self.attributes = document['_source'] @_id = document['_id'] @_version = document['_version'] end |
#safe_update(options = {}, &block) ⇒ Object
Optimistic Lock Update
doc.safe_update do |d|
d.amount += 100
end
if you are trying to update a stale object, the block is yielded again with a fresh reloaded document and the document is saved only when it is not stale anymore (i.e. the _version has not changed since it has been loaded) read: www.elasticsearch.org/blog/2011/02/08/versioning.html
46 47 48 |
# File 'lib/flex/active_model/storage.rb', line 46 def safe_update(={}, &block) perform_validations() ? lock_update(&block) : false end |
#safe_update!(options = {}, &block) ⇒ Object
50 51 52 |
# File 'lib/flex/active_model/storage.rb', line 50 def safe_update!(={}, &block) perform_validations() ? lock_update(&block) : raise(DocumentInvalidError, errors..join(", ")) end |
#save(options = {}) ⇒ Object
28 29 30 |
# File 'lib/flex/active_model/storage.rb', line 28 def save(={}) perform_validations() ? do_save : false end |
#save!(options = {}) ⇒ Object
32 33 34 |
# File 'lib/flex/active_model/storage.rb', line 32 def save!(={}) perform_validations() ? do_save : raise(DocumentInvalidError, errors..join(", ")) end |
#update_attributes(attributes) ⇒ Object
75 76 77 78 |
# File 'lib/flex/active_model/storage.rb', line 75 def update_attributes(attributes) merge_attributes(attributes) save end |
#valid?(context = nil) ⇒ Boolean
54 55 56 57 58 |
# File 'lib/flex/active_model/storage.rb', line 54 def valid?(context = nil) context ||= (new_record? ? :create : :update) output = super(context) errors.empty? && output end |