groovy 0.4.5 → 0.4.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/groovy/model.rb +1 -11
- data/lib/groovy/query.rb +29 -13
- data/lib/groovy/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2569117dfa3663d6c60ed950be2972c58116e7eaf6d21f5c6640cb91642fee4f
|
4
|
+
data.tar.gz: 33e1651825dd626dab979e9c504d5de1b7d8162363d42cbeca43873e370cad04
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 66b311fe79a3f9c8467ddbe9f863a2fd731b9c7dd9df2dbeb49fc1b8d62f4b7b506b2e0ca1f05c6ad7766ede89df0a26cb4a8eae8f96097ff613ab17cadde593
|
7
|
+
data.tar.gz: c1dbb4f84dfdb79d870a0e498acbc63d7e1268f5cfd1f0951fd16cc7d890b68a6d3679e903eb703b8de4a44b6765070594df0eb53b8cfbeb7ac9fb1499f5dd3f
|
data/lib/groovy/model.rb
CHANGED
@@ -187,16 +187,6 @@ module Groovy
|
|
187
187
|
query
|
188
188
|
end
|
189
189
|
|
190
|
-
def first(num = 1)
|
191
|
-
arr = limit(num)
|
192
|
-
num == 1 ? arr.first : arr
|
193
|
-
end
|
194
|
-
|
195
|
-
def last(num = 1)
|
196
|
-
arr = all.sort_by(_id: :desc).limit(num)
|
197
|
-
num == 1 ? arr.first : arr
|
198
|
-
end
|
199
|
-
|
200
190
|
def query
|
201
191
|
query_class.new(self, table)
|
202
192
|
end
|
@@ -208,7 +198,7 @@ module Groovy
|
|
208
198
|
end
|
209
199
|
end
|
210
200
|
|
211
|
-
[:select, :find_each, :find_by, :search, :where, :not, :sort_by, :limit, :offset, :paginate, :in_batches].each do |scope_method|
|
201
|
+
[:first, :last, :select, :find_each, :find_by, :search, :where, :not, :sort_by, :limit, :offset, :paginate, :in_batches].each do |scope_method|
|
212
202
|
define_method scope_method do |*args, &block|
|
213
203
|
query.public_send(scope_method, *args, &block)
|
214
204
|
end
|
data/lib/groovy/query.rb
CHANGED
@@ -3,6 +3,7 @@ module Groovy
|
|
3
3
|
class Query
|
4
4
|
|
5
5
|
include Enumerable
|
6
|
+
|
6
7
|
AND = '+'.freeze
|
7
8
|
NOT = '-'.freeze
|
8
9
|
PER_PAGE = 50.freeze
|
@@ -138,12 +139,12 @@ module Groovy
|
|
138
139
|
end
|
139
140
|
|
140
141
|
def limit(num)
|
141
|
-
|
142
|
+
sorting[:limit] = num
|
142
143
|
self
|
143
144
|
end
|
144
145
|
|
145
146
|
def offset(num)
|
146
|
-
|
147
|
+
sorting[:offset] = num
|
147
148
|
self
|
148
149
|
end
|
149
150
|
|
@@ -155,8 +156,8 @@ module Groovy
|
|
155
156
|
|
156
157
|
# sort_by(title: :asc)
|
157
158
|
def sort_by(hash)
|
158
|
-
if hash.is_a?(String) || hash.is_a?(Symbol) # e.g. 'title.desc' or :title (asc by default)
|
159
|
-
param, dir = hash.to_s.split(
|
159
|
+
if hash.is_a?(String) || hash.is_a?(Symbol) # e.g. 'title.desc', 'title desc' or :title (asc by default)
|
160
|
+
param, dir = hash.to_s.split(/\s|\./)
|
160
161
|
hash = {}
|
161
162
|
hash[param] = dir || 'asc'
|
162
163
|
end
|
@@ -207,23 +208,30 @@ module Groovy
|
|
207
208
|
@total_entries
|
208
209
|
end
|
209
210
|
|
210
|
-
def
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
211
|
+
def first(num = 1)
|
212
|
+
limit(num)
|
213
|
+
num == 1 ? records.first : records
|
214
|
+
end
|
215
|
+
|
216
|
+
def last(num = 1)
|
217
|
+
if sorting[:by]
|
218
|
+
get_last(num)
|
219
|
+
else # no sorting, so
|
220
|
+
sort_by(_id: :desc).limit(num)
|
221
|
+
# if last(2) or more, then re-sort by ascending ID
|
222
|
+
num == 1 ? records.first : records.sort { |a,b| a.id <=> b.id }
|
215
223
|
end
|
216
224
|
end
|
217
225
|
|
218
226
|
def in_batches(of: 1000, from: nil, &block)
|
219
|
-
|
220
|
-
|
227
|
+
sorting[:limit] = of
|
228
|
+
sorting[:offset] = from || 0
|
221
229
|
|
222
230
|
while results.any?
|
223
231
|
yield to_a
|
224
232
|
break if results.size < of
|
225
233
|
|
226
|
-
|
234
|
+
sorting[:offset] += of
|
227
235
|
@records = @results = nil # reset
|
228
236
|
end
|
229
237
|
end
|
@@ -237,6 +245,14 @@ module Groovy
|
|
237
245
|
private
|
238
246
|
attr_reader :model, :table, :options, :select_block
|
239
247
|
|
248
|
+
def get_last(count = 1)
|
249
|
+
if count > 1
|
250
|
+
records[(size-count)..-1]
|
251
|
+
else
|
252
|
+
records[size-1]
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
240
256
|
def add_param(param)
|
241
257
|
raise "Select block already given!" if select_block
|
242
258
|
raise "Duplicate param: #{param}" if parameters.include?(param)
|
@@ -265,7 +281,7 @@ module Groovy
|
|
265
281
|
|
266
282
|
@total_entries = set.size
|
267
283
|
|
268
|
-
debug "Sorting with #{sort_key_and_order}
|
284
|
+
debug "Sorting with #{sort_key_and_order} (options: #{sorting.inspect})"
|
269
285
|
set = set.sort(sort_key_and_order, {
|
270
286
|
limit: sorting[:limit],
|
271
287
|
offset: sorting[:offset], # [sorting[:offset], @total_entries].min
|
data/lib/groovy/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: groovy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomás Pollak
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-10-
|
11
|
+
date: 2020-10-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rroonga
|