Module: Flex::ActiveModel::Storage::InstanceMethods

Defined in:
lib/flex/active_model/storage.rb

Instance Method Summary collapse

Instance Method Details

#deleteObject



66
67
68
69
# File 'lib/flex/active_model/storage.rb', line 66

def delete
  @skip_destroy_callbacks = true
  destroy
end

#destroyObject



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

Returns:

  • (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

Returns:

  • (Boolean)


88
89
90
# File 'lib/flex/active_model/storage.rb', line 88

def new_record?
  !@_id || !@_version
end

#persisted?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/flex/active_model/storage.rb', line 84

def persisted?
  !(new_record? || destroyed?)
end

#reloadObject



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(options={}, &block)
  perform_validations(options) ? 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!(options={}, &block)
  perform_validations(options) ? lock_update(&block) : raise(DocumentInvalidError, errors.full_messages.join(", "))
end

#save(options = {}) ⇒ Object



28
29
30
# File 'lib/flex/active_model/storage.rb', line 28

def save(options={})
  perform_validations(options) ? do_save : false
end

#save!(options = {}) ⇒ Object



32
33
34
# File 'lib/flex/active_model/storage.rb', line 32

def save!(options={})
  perform_validations(options) ? do_save : raise(DocumentInvalidError, errors.full_messages.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

Returns:

  • (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