miscellany 0.1.4 → 0.1.5

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: 70a91bb987b7c99319cbe607cdfba6c32e9637a5a3fb8bec3ec38725339ea3c6
4
- data.tar.gz: f9607f408e91b1abadb51e73c4238b1a1d9f378fe294461efe11f3172783d9e2
3
+ metadata.gz: 0c92b625438d9027f73a5cd7b5f4ef168e1ed3e083b0c13924cb9f3f0e2db6bd
4
+ data.tar.gz: 0a52d0877c7eea6a0c2189efdd4c30cb65a3419abf149a8ae0ce4e63467741de
5
5
  SHA512:
6
- metadata.gz: f8c1ba0f079635d38ed0d3a29d3cb865899fb66112593625a2ae159061c5fd48f63cfa2300a825f671bdbd04e4e2317d6ccedd80702391bdadd7329c0a2deae9
7
- data.tar.gz: a73fd773078016b440b08243c4c6dc5477ff26cdbacb054f57d8b99b77c15305af7a1741d0c94b02399c3b59af7553254a23be5441d1d30948f4f51cb53b6ccf
6
+ metadata.gz: 9cbc439200d8a6c1604aa0153cbed6cfeead8636eb7330880371d2e8c6d2d412739e8a2d302c5dfd1cd9b038bec9f6de663f8fc7ddfd1fb1d8c99afe029e0306
7
+ data.tar.gz: d4bea4b5fd11c1830a8f18302c7f6c95d6f4c3fc147faf715f675850e7dee4e02d08632b05aa3e61ce4277de494a9784e6dc3ef55c8e92f99a0a22ba9a9d4fbc
@@ -16,7 +16,6 @@ module Miscellany
16
16
 
17
17
  class_methods do
18
18
  def bulk_destroy(**kwargs)
19
- return to_sql
20
19
  bulk_destroy_internal(self, **kwargs)
21
20
  end
22
21
 
@@ -8,12 +8,15 @@ module Miscellany
8
8
  # enumerator_of_some_kind.each { |item| batches << item }
9
9
  # batches.flush
10
10
  class BatchProcessor
11
- attr_reader :batch_size
11
+ attr_reader :batch_size, :ensure_once
12
12
 
13
- def initialize(of: 1000, &blk)
13
+ def initialize(of: 1000, ensure_once: false, &blk)
14
14
  @batch_size = of
15
15
  @block = blk
16
+ @ensure_once = ensure_once
16
17
  @current_batch = []
18
+
19
+ @flush_count = 0
17
20
  end
18
21
 
19
22
  def <<(item)
@@ -28,7 +31,7 @@ module Miscellany
28
31
  end
29
32
 
30
33
  def flush
31
- process_batch if @current_batch.present?
34
+ process_batch if @current_batch.present? || (@flush_count.zero? && ensure_once)
32
35
  end
33
36
 
34
37
  protected
@@ -36,6 +39,7 @@ module Miscellany
36
39
  def process_batch
37
40
  @block.call(@current_batch)
38
41
  @current_batch = []
42
+ @flush_count += 1
39
43
  end
40
44
  end
41
45
  end
@@ -17,7 +17,9 @@ module Miscellany
17
17
  default_sort: nil, valid_sorts: nil,
18
18
  &blk
19
19
  )
20
- normalized_sorts = normalize_sort_options(valid_sorts || queryset.column_names, default: default_sort)
20
+ valid_sorts ||= queryset.column_names if queryset.respond_to?(:column_names)
21
+ valid_sorts ||= []
22
+ normalized_sorts = normalize_sort_options(valid_sorts, default: default_sort)
21
23
 
22
24
  slice = Slice.build(
23
25
  queryset, slice_params,
@@ -85,7 +87,9 @@ module Miscellany
85
87
 
86
88
  def normalize_sort(sort, key: nil)
87
89
  sort = sort.to_s if sort.is_a?(Symbol)
88
- if sort.is_a?(String)
90
+ if sort.is_a?(Array)
91
+ sort = { **normalize_sort(sort[0]), **(sort[1] || {}) }
92
+ elsif sort.is_a?(String)
89
93
  m = sort.match(/^([\w\.]+)(?: (ASC|DESC)(!?))?$/)
90
94
  sort = { column: m[1], order: m[2], force_order: m[3].present? }.compact
91
95
  elsif sort.is_a?(Proc)
@@ -234,7 +238,12 @@ module Miscellany
234
238
  def sliced_items
235
239
  @sliced_items ||= begin
236
240
  if items.is_a?(Array)
237
- items
241
+ start, finish = slice_bounds
242
+ if start && finish
243
+ items[start...finish]
244
+ else
245
+ items
246
+ end
238
247
  elsif items.is_a?(Proc)
239
248
  items.call(self)
240
249
  elsif items.is_a?(ActiveRecord::Relation)
@@ -255,10 +264,20 @@ module Miscellany
255
264
  sorts << options[:valid_sorts][:default] if options.dig(:valid_sorts, :default).present?
256
265
 
257
266
  sorts.reduce(qset) do |qset, sort|
267
+ order = sort[:order] || 'ASC'
258
268
  if sort[:column].is_a?(Proc)
259
- sort[:column].call(qset, sort[:order] || 'ASC')
269
+ sort[:column].call(qset, order)
260
270
  else
261
- qset.order(sort[:column] => sort[:order] || 'ASC')
271
+ desired_nulls = (sort[:nulls] || :low).to_s.downcase.to_sym
272
+ nulls = case desired_nulls
273
+ when :last
274
+ 'LAST'
275
+ when :first
276
+ 'FIRST'
277
+ else
278
+ (desired_nulls == :high) == (order.to_s.upcase == 'DESC') ? 'FIRST' : 'LAST'
279
+ end
280
+ qset.order("#{sort[:column]} #{order} NULLS #{nulls}")
262
281
  end
263
282
  end
264
283
  else
@@ -124,7 +124,7 @@ module Miscellany
124
124
  if blk.is_a?(Hash)
125
125
  pv = ParamValidator.new(nil, self.context, params[pk])
126
126
  pv.parameter(i, **blk)
127
- ers = pv.errors
127
+ errs = pv.errors.store_for(i)
128
128
  else
129
129
  errs = ParamValidator.check(params[pk][i], context: context, &blk)
130
130
  end
@@ -1,3 +1,3 @@
1
1
  module Miscellany
2
- VERSION = "0.1.4".freeze
2
+ VERSION = "0.1.5".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: miscellany
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ethan Knapp
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-20 00:00:00.000000000 Z
11
+ date: 2022-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -146,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
146
  - !ruby/object:Gem::Version
147
147
  version: '0'
148
148
  requirements: []
149
- rubygems_version: 3.0.3
149
+ rubygems_version: 3.1.6
150
150
  signing_key:
151
151
  specification_version: 4
152
152
  summary: Gem for a bunch of random, re-usable Rails Concerns & Helpers