Module: Elasticsearch::Persistence::Model::Store::InstanceMethods
- Defined in:
- lib/elasticsearch/persistence/model/store.rb
Instance Method Summary collapse
- #becomes(klass) ⇒ Object
-
#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
#becomes(klass) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/elasticsearch/persistence/model/store.rb', line 253 def becomes(klass) became = klass.new(attributes) changed_attributes = @changed_attributes if defined?(@changed_attributes) became.instance_variable_set("@changed_attributes", changed_attributes || {}) became.instance_variable_set("@new_record", new_record?) became.instance_variable_set("@destroyed", destroyed?) became.instance_variable_set("@errors", errors) became.instance_variable_set("@persisted", persisted?) became.instance_variable_set("@_id", _id) became.instance_variable_set("@_version", _version) became.instance_variable_set("@_index", _index) became.instance_variable_set("@_type", _type) became end |
#decrement(attribute, value = 1, options = {}) ⇒ Hash
Decrements a numeric attribute (via Elasticsearch’s “Update” API) and returns the response
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/elasticsearch/persistence/model/store.rb', line 180 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
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/elasticsearch/persistence/model/store.rb', line 90 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
233 234 235 |
# File 'lib/elasticsearch/persistence/model/store.rb', line 233 def destroyed? !!@destroyed end |
#increment(attribute, value = 1, options = {}) ⇒ Hash
Increments a numeric attribute (via Elasticsearch’s “Update” API) and returns the response
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/elasticsearch/persistence/model/store.rb', line 151 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
249 250 251 |
# File 'lib/elasticsearch/persistence/model/store.rb', line 249 def new_record? !persisted? && !destroyed? end |
#persisted? ⇒ TrueClass, FalseClass
Returns true when the model has been already saved to the database, false otherwise
241 242 243 |
# File 'lib/elasticsearch/persistence/model/store.rb', line 241 def persisted? !!@persisted && !destroyed? end |
#save(options = {}) ⇒ Hash, FalseClass
Saves the model (if validations pass) and returns the response (or ‘false`)
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/elasticsearch/persistence/model/store.rb', line 44 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 if new_record? response = run_callbacks :create do 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 else 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 end |
#touch(attribute = :updated_at, options = {}) ⇒ Hash
Updates the ‘updated_at` attribute, saves the model and returns the response
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/elasticsearch/persistence/model/store.rb', line 208 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
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/elasticsearch/persistence/model/store.rb', line 117 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 |