Module: PostJson::FinderMethods
Instance Method Summary
collapse
#assert_valid_indifferent_keys, #flatten_arguments, #join_arguments
#add_query, #except, #except!, #limit, #limit!, #offset, #offset!, #only, #only!, #order, #order!, #page, #page!, #query_clone, #query_tree, #query_tree_renew!, #reorder, #reorder!, #reverse_order, #reverse_order!, #where, #where!
Instance Method Details
#any? ⇒ Boolean
14
15
16
|
# File 'lib/post_json/concerns/finder_methods.rb', line 14
def any?
execute { |documents| documents.any? }
end
|
#blank? ⇒ Boolean
18
19
20
|
# File 'lib/post_json/concerns/finder_methods.rb', line 18
def blank?
execute { |documents| documents.blank? }
end
|
#count(column_name = nil, options = {}) ⇒ Object
22
23
24
25
26
27
28
29
|
# File 'lib/post_json/concerns/finder_methods.rb', line 22
def count(column_name = nil, options = {})
selector = if column_name.present?
define_selector(column_name)
else
column_name
end
execute { |documents| documents.count(selector, options) }
end
|
#delete(id_or_array) ⇒ Object
31
32
33
|
# File 'lib/post_json/concerns/finder_methods.rb', line 31
def delete(id_or_array)
where(id: id_or_array).delete_all
end
|
#delete_all(conditions = nil) ⇒ Object
35
36
37
|
# File 'lib/post_json/concerns/finder_methods.rb', line 35
def delete_all(conditions = nil)
where(conditions).execute { |documents| documents.delete_all }
end
|
#destroy(id) ⇒ Object
39
40
41
|
# File 'lib/post_json/concerns/finder_methods.rb', line 39
def destroy(id)
execute { |documents| documents.destroy(id) }
end
|
#destroy_all(conditions = nil) ⇒ Object
43
44
45
|
# File 'lib/post_json/concerns/finder_methods.rb', line 43
def destroy_all(conditions = nil)
where(conditions).execute { |documents| documents.destroy_all }
end
|
#empty? ⇒ Boolean
47
48
49
|
# File 'lib/post_json/concerns/finder_methods.rb', line 47
def empty?
execute { |documents| documents.empty? }
end
|
#exists?(conditions = nil) ⇒ Boolean
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/post_json/concerns/finder_methods.rb', line 51
def exists?(conditions = nil)
query = case conditions
when nil
self
when Numeric, String
where({id: conditions})
else
where(conditions)
end
query.execute { |documents| documents.exists? }
end
|
#find(*args) ⇒ Object
64
65
66
|
# File 'lib/post_json/concerns/finder_methods.rb', line 64
def find(*args)
execute { |documents| documents.find(*args) }
end
|
#find_by(*args) ⇒ Object
68
69
70
|
# File 'lib/post_json/concerns/finder_methods.rb', line 68
def find_by(*args)
where(*args).first
end
|
#find_by!(*args) ⇒ Object
72
73
74
|
# File 'lib/post_json/concerns/finder_methods.rb', line 72
def find_by!(*args)
find_by(*args) or raise ActiveRecord::RecordNotFound
end
|
#find_each(options = {}) ⇒ Object
76
77
78
|
# File 'lib/post_json/concerns/finder_methods.rb', line 76
def find_each(options = {})
execute { |documents| documents.find_each(options) }
end
|
#find_in_batches(options = {}) ⇒ Object
80
81
82
|
# File 'lib/post_json/concerns/finder_methods.rb', line 80
def find_in_batches(options = {})
execute { |documents| documents.find_in_batches(options) }
end
|
#first(limit = nil) ⇒ Object
84
85
86
87
88
89
90
|
# File 'lib/post_json/concerns/finder_methods.rb', line 84
def first(limit = nil)
if limit
limit(limit).execute { |documents| documents.first }
else
execute { |documents| documents.first }
end
end
|
#first! ⇒ Object
92
93
94
|
# File 'lib/post_json/concerns/finder_methods.rb', line 92
def first!
execute { |documents| documents.first! }
end
|
#first_or_create(attributes = {}) ⇒ Object
96
97
98
99
|
# File 'lib/post_json/concerns/finder_methods.rb', line 96
def first_or_create(attributes = {})
attributes = where_values_hash.with_indifferent_access.deep_merge(attributes)
first or create(attributes)
end
|
#first_or_initialize(attributes = {}) ⇒ Object
101
102
103
104
|
# File 'lib/post_json/concerns/finder_methods.rb', line 101
def first_or_initialize(attributes = {})
attributes = where_values_hash.with_indifferent_access.deep_merge(attributes)
first or model_class.new(attributes)
end
|
#ids ⇒ Object
106
107
108
|
# File 'lib/post_json/concerns/finder_methods.rb', line 106
def ids
pluck('id')
end
|
#last(limit = nil) ⇒ Object
110
111
112
|
# File 'lib/post_json/concerns/finder_methods.rb', line 110
def last(limit = nil)
reverse_order.first(limit)
end
|
#last! ⇒ Object
114
115
116
|
# File 'lib/post_json/concerns/finder_methods.rb', line 114
def last!
reverse_order.first!
end
|
#load ⇒ Object
118
119
120
|
# File 'lib/post_json/concerns/finder_methods.rb', line 118
def load
execute { |documents| documents.load }
end
|
#many? ⇒ Boolean
122
123
124
|
# File 'lib/post_json/concerns/finder_methods.rb', line 122
def many?
execute { |documents| documents.many? }
end
|
#pluck(*selectors) ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/post_json/concerns/finder_methods.rb', line 126
def pluck(*selectors)
selectors = join_arguments(*selectors)
if selectors == ""
[]
elsif selectors == "*"
execute { |documents| documents.pluck("\"#{table_name}\".__doc__body") }
elsif selectors == "id"
execute { |documents| documents.pluck("\"#{table_name}\".id") }
else
result = nil
execute { |documents| result = documents.pluck("json_selectors('#{selectors}', \"#{table_name}\".__doc__body)") }
if selectors.include?(",")
result
else
result.flatten(1)
end
end
end
|
#select(*selectors) ⇒ Object
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/post_json/concerns/finder_methods.rb', line 145
def select(*selectors)
selectors = selectors.flatten(1)
if selectors.length == 0
[]
elsif selectors.length == 1
selector = selectors[0]
case selector
when String, Symbol
selector = selector.to_s.gsub(/\s+/, '')
if selector == "*"
pluck("*").map { |body| body ? Hashie::Mash.new(body) : body }
else
selectors = selector.split(",")
if selectors.length == 1
select({selector => selector})
else
select(selectors)
end
end
when Hash
flat_hash = selector.flatten_hash
pluck(flat_hash.values).map do |row|
flat_body = if flat_hash.keys.length == 1
{flat_hash.keys[0] => row}
else
Hash[flat_hash.keys.zip(row)]
end
deep_hash = flat_body.deepen_hash
Hashie::Mash.new(deep_hash) if deep_hash
end
else
raise ArgumentError, "Invalid argument(s): #{selectors.inspect}"
end
else
select(Hash[selectors.zip(selectors)])
end
end
|
#size ⇒ Object
183
184
185
|
# File 'lib/post_json/concerns/finder_methods.rb', line 183
def size
execute { |documents| documents.size }
end
|
#take(limit = nil) ⇒ Object
187
188
189
|
# File 'lib/post_json/concerns/finder_methods.rb', line 187
def take(limit = nil)
execute { |documents| documents.take(limit) }
end
|
#take! ⇒ Object
191
192
193
|
# File 'lib/post_json/concerns/finder_methods.rb', line 191
def take!
execute { |documents| documents.take! }
end
|
#to_a ⇒ Object
195
196
197
|
# File 'lib/post_json/concerns/finder_methods.rb', line 195
def to_a
execute { |documents| documents.to_a }
end
|
#to_sql ⇒ Object
199
200
201
|
# File 'lib/post_json/concerns/finder_methods.rb', line 199
def to_sql
execute { |documents| documents.to_sql }
end
|
#where_values_hash ⇒ Object
203
204
205
206
207
208
209
210
211
|
# File 'lib/post_json/concerns/finder_methods.rb', line 203
def where_values_hash
where_equals = query_tree[:where_equal] || []
values_hash = where_equals.inject({}) do |result, where_equal|
key = where_equal[:attribute]
result[key] = where_equal[:argument]
result
end
values_hash.deepen_hash
end
|