@@ -2,8 +2,20 @@ module Elasticsearch
2
2
module Persistence
3
3
module Model
4
4
5
+ # This module contains the storage related features of {Elasticsearch::Persistence::Model}
6
+ #
5
7
module Store
6
- module ClassMethods
8
+ module ClassMethods #:nodoc:
9
+
10
+ # Creates a class instance, saves it, if validations pass, and returns it
11
+ #
12
+ # @example Create a new person
13
+ #
14
+ # Person.create name: 'John Smith'
15
+ # # => #<Person:0x007f889e302b30 ... @id="bG7yQDAXRhCi3ZfVcx6oAA", @name="John Smith" ...>
16
+ #
17
+ # @return [Object] The model instance
18
+ #
7
19
def create ( attributes , options = { } )
8
20
object = self . new ( attributes )
9
21
object . run_callbacks :create do
@@ -14,6 +26,23 @@ def create(attributes, options={})
14
26
end
15
27
16
28
module InstanceMethods
29
+
30
+ # Saves the model (if validations pass) and returns the response (or `false`)
31
+ #
32
+ # @example Save a valid model instance
33
+ #
34
+ # p = Person.new(name: 'John')
35
+ # p.save
36
+ # => {"_index"=>"people", ... "_id"=>"RzFSXFR0R8u1CZIWNs2Gvg", "_version"=>1, "created"=>true}
37
+ #
38
+ # @example Save an invalid model instance
39
+ #
40
+ # p = Person.new(name: nil)
41
+ # p.save
42
+ # # => false
43
+ #
44
+ # @return [Hash,FalseClass] The Elasticsearch response as a Hash or `false`
45
+ #
17
46
def save ( options = { } )
18
47
return false unless valid?
19
48
run_callbacks :save do
@@ -25,6 +54,15 @@ def save(options={})
25
54
end
26
55
end
27
56
57
+ # Deletes the model from Elasticsearch (if it's persisted), freezes it, and returns the response
58
+ #
59
+ # @example Delete a model instance
60
+ #
61
+ # p.destroy
62
+ # => {"_index"=>"people", ... "_id"=>"RzFSXFR0R8u1CZIWNs2Gvg", "_version"=>2 ...}
63
+ #
64
+ # @return [Hash] The Elasticsearch response as a Hash
65
+ #
28
66
def destroy ( options = { } )
29
67
raise DocumentNotPersisted , "Object not persisted: #{ self . inspect } " unless persisted?
30
68
@@ -37,6 +75,15 @@ def destroy(options={})
37
75
end
38
76
end ; alias :delete :destroy
39
77
78
+ # Updates the model (via Elasticsearch's "Update" API) and returns the response
79
+ #
80
+ # @example Update a model with partial attributes
81
+ #
82
+ # p.update name: 'UPDATED'
83
+ # => {"_index"=>"people", ... "_version"=>2}
84
+ #
85
+ # @return [Hash] The Elasticsearch response as a Hash
86
+ #
40
87
def update ( attributes = { } , options = { } )
41
88
raise DocumentNotPersisted , "Object not persisted: #{ self . inspect } " unless persisted?
42
89
@@ -48,6 +95,18 @@ def update(attributes={}, options={})
48
95
end
49
96
end ; alias :update_attributes :update
50
97
98
+ # Increments a numeric attribute (via Elasticsearch's "Update" API) and returns the response
99
+ #
100
+ # @example Increment the `salary` attribute by 1
101
+ #
102
+ # p.increment :salary
103
+ #
104
+ # @example Increment the `salary` attribute by 100
105
+ #
106
+ # p.increment :salary, 100
107
+ #
108
+ # @return [Hash] The Elasticsearch response as a Hash
109
+ #
51
110
def increment ( attribute , value = 1 , options = { } )
52
111
raise DocumentNotPersisted , "Object not persisted: #{ self . inspect } " unless persisted?
53
112
@@ -56,6 +115,18 @@ def increment(attribute, value=1, options={})
56
115
response
57
116
end
58
117
118
+ # Decrements a numeric attribute (via Elasticsearch's "Update" API) and returns the response
119
+ #
120
+ # @example Decrement the `salary` attribute by 1
121
+ #
122
+ # p.decrement :salary
123
+ #
124
+ # @example Decrement the `salary` attribute by 100
125
+ #
126
+ # p.decrement :salary, 100
127
+ #
128
+ # @return [Hash] The Elasticsearch response as a Hash
129
+ #
59
130
def decrement ( attribute , value = 1 , options = { } )
60
131
raise DocumentNotPersisted , "Object not persisted: #{ self . inspect } " unless persisted?
61
132
@@ -64,6 +135,18 @@ def decrement(attribute, value=1, options={})
64
135
response
65
136
end
66
137
138
+ # Updates the `updated_at` attribute, saves the model and returns the response
139
+ #
140
+ # @example Update the `updated_at` attribute (default)
141
+ #
142
+ # p.touch
143
+ #
144
+ # @example Update a custom attribute: `saved_on`
145
+ #
146
+ # p.touch :saved_on
147
+ #
148
+ # @return [Hash] The Elasticsearch response as a Hash
149
+ #
67
150
def touch ( attribute = :updated_at , options = { } )
68
151
raise DocumentNotPersisted , "Object not persisted: #{ self . inspect } " unless persisted?
69
152
raise ArgumentError , "Object does not have '#{ attribute } ' attribute" unless respond_to? ( attribute )
@@ -76,14 +159,26 @@ def touch(attribute=:updated_at, options={})
76
159
end
77
160
end
78
161
162
+ # Returns true when the model has been destroyed, false otherwise
163
+ #
164
+ # @return [TrueClass,FalseClass]
165
+ #
79
166
def destroyed?
80
167
!!@destroyed
81
168
end
82
169
170
+ # Returns true when the model has been already saved to the database, false otherwise
171
+ #
172
+ # @return [TrueClass,FalseClass]
173
+ #
83
174
def persisted?
84
175
!!@persisted && !destroyed?
85
176
end
86
177
178
+ # Returns true when the model has not been saved yet, false otherwise
179
+ #
180
+ # @return [TrueClass,FalseClass]
181
+ #
87
182
def new_record?
88
183
!persisted? && !destroyed?
89
184
end
0 commit comments