miscellany 0.1.4 → 0.1.7

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: 6e57424c3b049c0f32faf8cf0a7e0f7f85145de07dc8ac09e61b2c2c3b8dd926
4
+ data.tar.gz: 52c108a746be070473df4bc9c4bad66daf216809e72dea58a628b2c94d14923d
5
5
  SHA512:
6
- metadata.gz: f8c1ba0f079635d38ed0d3a29d3cb865899fb66112593625a2ae159061c5fd48f63cfa2300a825f671bdbd04e4e2317d6ccedd80702391bdadd7329c0a2deae9
7
- data.tar.gz: a73fd773078016b440b08243c4c6dc5477ff26cdbacb054f57d8b99b77c15305af7a1741d0c94b02399c3b59af7553254a23be5441d1d30948f4f51cb53b6ccf
6
+ metadata.gz: e9d8ddae9f677caa1c1c689a85abefaa64beb46cac97e9005be62c68b709ebe02bfb26791344bb58ab13cf6a557024d12bd8cbe667e88097c466cacf5148b31e
7
+ data.tar.gz: a8fe4f104a46d80c2af4fb0421716d34359fb0fabf35cb362b4ed33e284ee20a09054ec12fa14340127cadbf57c21e2982f6ed89459de54b7bf576eca395b501
@@ -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
 
@@ -33,7 +32,7 @@ module Miscellany
33
32
  def bulk_destroy_internal(items, **kwargs)
34
33
  options = {}
35
34
  options.merge!(kwargs)
36
- ClassCallbackExector.run_callbacks(model_class, :bulk_destroy, options: options) do
35
+ ClassCallbackExector.run_callbacks(model_class, :bulk_destroy, { options: options }) do
37
36
  if items.respond_to?(:find_in_batches)
38
37
  items.find_in_batches do |batch|
39
38
  _destroy_batch(batch, options)
@@ -48,6 +47,7 @@ module Miscellany
48
47
  ClassCallbackExector.run_callbacks(model_class, :destroy_batch, {
49
48
  model_class: model_class,
50
49
  batch: batch,
50
+ options: options,
51
51
  }) do
52
52
  model_class.destroy_bulk_batch(batch, options)
53
53
  end
@@ -85,7 +85,6 @@ module Miscellany
85
85
  env[k]
86
86
  end
87
87
  end
88
- @options = options
89
88
  end
90
89
 
91
90
  def self.run_callbacks(cls, callback, env={}, &blk)
@@ -35,14 +35,15 @@ module Miscellany
35
35
  comp = model.get_defined_computed(k)
36
36
  raise "Undefined ComputedColum :#{k}" if comp.nil?
37
37
 
38
- builder = ComputedBuilder.new(k, v, &comp)
38
+ builder = ComputedBuilder.new(all, k, v, &comp)
39
39
  builder.apply(query)
40
40
  end
41
41
  end
42
42
  end
43
43
 
44
44
  class ComputedBuilder
45
- def initialize(key, args, &blk)
45
+ def initialize(relation, key, args, &blk)
46
+ @relation = relation
46
47
  @key = key
47
48
  @args = args.is_a?(Array) ? args : [args]
48
49
  @block = blk
@@ -55,7 +56,7 @@ module Miscellany
55
56
  raise "Must provide either a value or a block" if arg == :not_given && !blk
56
57
 
57
58
  if arg == :not_given
58
- arg = blk.call
59
+ arg = @relation.instance_eval(&blk)
59
60
  end
60
61
 
61
62
  @compiled[m] = arg
@@ -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.7".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.7
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: 2023-03-29 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