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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f2e7655a8fefa2f17295105acb6ba387f275eb158c0babed04675d566f736766
4
- data.tar.gz: 5a9b94a767f58b033a11044d3b7992911a6cde0ad068955c76d85bdd2c0464b9
3
+ metadata.gz: 2569117dfa3663d6c60ed950be2972c58116e7eaf6d21f5c6640cb91642fee4f
4
+ data.tar.gz: 33e1651825dd626dab979e9c504d5de1b7d8162363d42cbeca43873e370cad04
5
5
  SHA512:
6
- metadata.gz: 2c8813e0920559c6422f8129ca40e51c42a6db26efc65c9f2d9a1d47d8f5f6d5638f9677f970a38d36a180c93cf610b99b7adc372579eaff92ae4d8bd9aa41ae
7
- data.tar.gz: d12ed329507b8a5c191308e198a5434f8226fd24bf4f25c8b2b8d64a832c05df39ff9946dddd53b7da5df11c2d14bc9ab97e4dcf84d67e60c9c2811d0c0122b6
6
+ metadata.gz: 66b311fe79a3f9c8467ddbe9f863a2fd731b9c7dd9df2dbeb49fc1b8d62f4b7b506b2e0ca1f05c6ad7766ede89df0a26cb4a8eae8f96097ff613ab17cadde593
7
+ data.tar.gz: c1dbb4f84dfdb79d870a0e498acbc63d7e1268f5cfd1f0951fd16cc7d890b68a6d3679e903eb703b8de4a44b6765070594df0eb53b8cfbeb7ac9fb1499f5dd3f
@@ -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
@@ -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
- @sorting[:limit] = num
142
+ sorting[:limit] = num
142
143
  self
143
144
  end
144
145
 
145
146
  def offset(num)
146
- @sorting[:offset] = num
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 last(count = 1)
211
- if count > 1
212
- records[(size-count)..-1]
213
- else
214
- records[size-1]
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
- @sorting[:limit] = of
220
- @sorting[:offset] = from || 0
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
- @sorting[:offset] += of
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}, #{sorting.inspect}"
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
@@ -1,3 +1,3 @@
1
1
  module Groovy
2
- VERSION = '0.4.5'.freeze
2
+ VERSION = '0.4.6'.freeze
3
3
  end
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.5
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-13 00:00:00.000000000 Z
11
+ date: 2020-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rroonga