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
42
|
# File 'lib/post_json/concerns/finder_methods.rb', line 39
def destroy(id)
value = id ? id.to_s.downcase : nil
execute { |documents| documents.destroy(value) }
end
|
#destroy_all(conditions = nil) ⇒ Object
44
45
46
|
# File 'lib/post_json/concerns/finder_methods.rb', line 44
def destroy_all(conditions = nil)
where(conditions).execute { |documents| documents.destroy_all }
end
|
#empty? ⇒ Boolean
48
49
50
|
# File 'lib/post_json/concerns/finder_methods.rb', line 48
def empty?
execute { |documents| documents.empty? }
end
|
#exists?(conditions = nil) ⇒ Boolean
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/post_json/concerns/finder_methods.rb', line 52
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/post_json/concerns/finder_methods.rb', line 65
def find(*args)
if args.length == 0
execute { |documents| documents.find }
elsif args.length == 1 && args[0].is_a?(Array)
ids = args[0].map{|id| id ? id.to_s.downcase : nil}
execute { |documents| documents.find(ids) }
elsif args.length == 1 && args[0].is_a?(Array) == false
id = args[0] ? args[0].to_s.downcase : nil
execute { |documents| documents.find(id) }
else
ids = args.map{|id| id ? id.to_s.downcase : nil}
execute { |documents| documents.find(ids) }
end
end
|
#find_by(*args) ⇒ Object
80
81
82
|
# File 'lib/post_json/concerns/finder_methods.rb', line 80
def find_by(*args)
where(*args).first
end
|
#find_by!(*args) ⇒ Object
84
85
86
|
# File 'lib/post_json/concerns/finder_methods.rb', line 84
def find_by!(*args)
find_by(*args) or raise ActiveRecord::RecordNotFound
end
|
#find_each(options = {}) ⇒ Object
88
89
90
|
# File 'lib/post_json/concerns/finder_methods.rb', line 88
def find_each(options = {})
execute { |documents| documents.find_each(options) }
end
|
#find_in_batches(options = {}) ⇒ Object
92
93
94
|
# File 'lib/post_json/concerns/finder_methods.rb', line 92
def find_in_batches(options = {})
execute { |documents| documents.find_in_batches(options) }
end
|
#first(limit = nil) ⇒ Object
96
97
98
99
100
101
102
|
# File 'lib/post_json/concerns/finder_methods.rb', line 96
def first(limit = nil)
if limit
limit(limit).execute { |documents| documents.first }
else
execute { |documents| documents.first }
end
end
|
#first! ⇒ Object
104
105
106
|
# File 'lib/post_json/concerns/finder_methods.rb', line 104
def first!
execute { |documents| documents.first! }
end
|
#first_or_create(attributes = {}) ⇒ Object
108
109
110
111
|
# File 'lib/post_json/concerns/finder_methods.rb', line 108
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
113
114
115
116
|
# File 'lib/post_json/concerns/finder_methods.rb', line 113
def first_or_initialize(attributes = {})
attributes = where_values_hash.with_indifferent_access.deep_merge(attributes)
first or model_class.new(attributes)
end
|
#ids ⇒ Object
118
119
120
|
# File 'lib/post_json/concerns/finder_methods.rb', line 118
def ids
pluck('id')
end
|
#last(limit = nil) ⇒ Object
122
123
124
|
# File 'lib/post_json/concerns/finder_methods.rb', line 122
def last(limit = nil)
reverse_order.first(limit)
end
|
#last! ⇒ Object
126
127
128
|
# File 'lib/post_json/concerns/finder_methods.rb', line 126
def last!
reverse_order.first!
end
|
#load ⇒ Object
130
131
132
|
# File 'lib/post_json/concerns/finder_methods.rb', line 130
def load
execute { |documents| documents.load }
end
|
#many? ⇒ Boolean
134
135
136
|
# File 'lib/post_json/concerns/finder_methods.rb', line 134
def many?
execute { |documents| documents.many? }
end
|
#pluck(*selectors) ⇒ Object
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/post_json/concerns/finder_methods.rb', line 138
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
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
182
183
184
185
186
187
188
189
190
191
192
193
|
# File 'lib/post_json/concerns/finder_methods.rb', line 157
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
195
196
197
|
# File 'lib/post_json/concerns/finder_methods.rb', line 195
def size
execute { |documents| documents.size }
end
|
#take(limit = nil) ⇒ Object
199
200
201
|
# File 'lib/post_json/concerns/finder_methods.rb', line 199
def take(limit = nil)
execute { |documents| documents.take(limit) }
end
|
#take! ⇒ Object
203
204
205
|
# File 'lib/post_json/concerns/finder_methods.rb', line 203
def take!
execute { |documents| documents.take! }
end
|
#to_a ⇒ Object
207
208
209
|
# File 'lib/post_json/concerns/finder_methods.rb', line 207
def to_a
execute { |documents| documents.to_a }
end
|
#to_sql ⇒ Object
211
212
213
|
# File 'lib/post_json/concerns/finder_methods.rb', line 211
def to_sql
execute { |documents| documents.to_sql }
end
|
#where_values_hash ⇒ Object
215
216
217
218
219
220
221
222
223
|
# File 'lib/post_json/concerns/finder_methods.rb', line 215
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
|