Module: Elasticsearch::Persistence::Model::Store::InstanceMethods
- Defined in:
- lib/elasticsearch/persistence/model/store.rb
Instance Method Summary collapse
-
#decrement(attribute, value = 1, options = {}) ⇒ Hash
Decrements a numeric attribute (via Elasticsearch’s “Update” API) and returns the response.
-
#destroy(options = {}) ⇒ Hash
(also: #delete)
Deletes the model from Elasticsearch (if it’s persisted), freezes it, and returns the response.
-
#destroyed? ⇒ TrueClass, FalseClass
Returns true when the model has been destroyed, false otherwise.
-
#increment(attribute, value = 1, options = {}) ⇒ Hash
Increments a numeric attribute (via Elasticsearch’s “Update” API) and returns the response.
-
#new_record? ⇒ TrueClass, FalseClass
Returns true when the model has not been saved yet, false otherwise.
-
#persisted? ⇒ TrueClass, FalseClass
Returns true when the model has been already saved to the database, false otherwise.
-
#save(options = {}) ⇒ Hash, FalseClass
Saves the model (if validations pass) and returns the response (or ‘false`).
-
#touch(attribute = :updated_at, options = {}) ⇒ Hash
Updates the ‘updated_at` attribute, saves the model and returns the response.
-
#update(attributes = {}, options = {}) ⇒ Hash
(also: #update_attributes)
Updates the model (via Elasticsearch’s “Update” API) and returns the response.
Instance Method Details
#decrement(attribute, value = 1, options = {}) ⇒ Hash
Decrements a numeric attribute (via Elasticsearch’s “Update” API) and returns the response
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/elasticsearch/persistence/model/store.rb', line 162 def decrement(attribute, value=1, ={}) raise DocumentNotPersisted, "Object not persisted: #{self.inspect}" unless persisted? .update index: self._index if self._index .update type: self._type if self._type response = self.class.gateway.update(self.id, { script: "ctx._source.#{attribute} = ctx._source.#{attribute} - #{value}"}.merge()) self[attribute] -= value @_index = response['_index'] @_type = response['_type'] @_version = response['_version'] response end |
#destroy(options = {}) ⇒ Hash Also known as: delete
Deletes the model from Elasticsearch (if it’s persisted), freezes it, and returns the response
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/elasticsearch/persistence/model/store.rb', line 76 def destroy(={}) raise DocumentNotPersisted, "Object not persisted: #{self.inspect}" unless persisted? run_callbacks :destroy do .update index: self._index if self._index .update type: self._type if self._type response = self.class.gateway.delete(self.id, ) @destroyed = true @persisted = false self.freeze response end end |
#destroyed? ⇒ TrueClass, FalseClass
Returns true when the model has been destroyed, false otherwise
215 216 217 |
# File 'lib/elasticsearch/persistence/model/store.rb', line 215 def destroyed? !!@destroyed end |
#increment(attribute, value = 1, options = {}) ⇒ Hash
Increments a numeric attribute (via Elasticsearch’s “Update” API) and returns the response
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/elasticsearch/persistence/model/store.rb', line 133 def increment(attribute, value=1, ={}) raise DocumentNotPersisted, "Object not persisted: #{self.inspect}" unless persisted? .update index: self._index if self._index .update type: self._type if self._type response = self.class.gateway.update(self.id, { script: "ctx._source.#{attribute} += #{value}"}.merge()) self[attribute] += value @_index = response['_index'] @_type = response['_type'] @_version = response['_version'] response end |
#new_record? ⇒ TrueClass, FalseClass
Returns true when the model has not been saved yet, false otherwise
231 232 233 |
# File 'lib/elasticsearch/persistence/model/store.rb', line 231 def new_record? !persisted? && !destroyed? end |
#persisted? ⇒ TrueClass, FalseClass
Returns true when the model has been already saved to the database, false otherwise
223 224 225 |
# File 'lib/elasticsearch/persistence/model/store.rb', line 223 def persisted? !!@persisted && !destroyed? end |
#save(options = {}) ⇒ Hash, FalseClass
Saves the model (if validations pass) and returns the response (or ‘false`)
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/elasticsearch/persistence/model/store.rb', line 46 def save(={}) return false unless valid? run_callbacks :save do .update id: self.id .update index: self._index if self._index .update type: self._type if self._type response = self.class.gateway.save(self, ) self[:updated_at] = Time.now.utc @_id = response['_id'] @_index = response['_index'] @_type = response['_type'] @_version = response['_version'] @persisted = true response end end |
#touch(attribute = :updated_at, options = {}) ⇒ Hash
Updates the ‘updated_at` attribute, saves the model and returns the response
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/elasticsearch/persistence/model/store.rb', line 190 def touch(attribute=:updated_at, ={}) raise DocumentNotPersisted, "Object not persisted: #{self.inspect}" unless persisted? raise ArgumentError, "Object does not have '#{attribute}' attribute" unless respond_to?(attribute) run_callbacks :touch do .update index: self._index if self._index .update type: self._type if self._type value = Time.now.utc response = self.class.gateway.update(self.id, { doc: { attribute => value.iso8601 }}.merge()) self[attribute] = value @_index = response['_index'] @_type = response['_type'] @_version = response['_version'] response end end |
#update(attributes = {}, options = {}) ⇒ Hash Also known as: update_attributes
Updates the model (via Elasticsearch’s “Update” API) and returns the response
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/elasticsearch/persistence/model/store.rb', line 101 def update(attributes={}, ={}) raise DocumentNotPersisted, "Object not persisted: #{self.inspect}" unless persisted? run_callbacks :update do .update index: self._index if self._index .update type: self._type if self._type attributes.update( { updated_at: Time.now.utc } ) response = self.class.gateway.update(self.id, { doc: attributes}.merge()) self.attributes = self.attributes.merge(attributes) @_index = response['_index'] @_type = response['_type'] @_version = response['_version'] response end end |