type_balancer_rails 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5ebd00e75d4c63af53f5be038f01f920eb6e65dcf3c91ae079a10b20435d62ce
|
4
|
+
data.tar.gz: 67d328f6ae917d27f88455dce176073dd8da0cf8dd7bb2be460c886c7a56086f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a82e84484c322cd4ee02c92950cf3235daa989b21097c845a5e41a454084afe883ee15444729992e7a63b59318508dbd7f89a15d1abe3d371f8de2cc1e1c551
|
7
|
+
data.tar.gz: e427b4224fbed0bd83dcda7dd2ce97683dc1c7df54a2fb673ce15b9d545ee029f87637fb5a8cff685cb7570f7d0e7aeaf3c82e0a5466906773326a2d9d794486
|
@@ -19,9 +19,15 @@ module TypeBalancer
|
|
19
19
|
end
|
20
20
|
|
21
21
|
class_methods do
|
22
|
-
|
22
|
+
# Accepts either a symbol (type field) or options hash
|
23
|
+
def balance_by_type(type_field = nil, **options)
|
24
|
+
if type_field.is_a?(Hash)
|
25
|
+
options = type_field
|
26
|
+
type_field = options[:type_field]
|
27
|
+
end
|
28
|
+
|
23
29
|
self.type_balancer_options = {
|
24
|
-
type_field: options[:type_field] || :type
|
30
|
+
type_field: type_field || options[:type_field] || :type
|
25
31
|
}
|
26
32
|
|
27
33
|
return [] unless respond_to?(:all)
|
@@ -23,31 +23,43 @@ module TypeBalancer
|
|
23
23
|
records = to_a
|
24
24
|
return empty_relation if records.empty?
|
25
25
|
|
26
|
-
# Get type field from options or model configuration
|
27
26
|
type_field = fetch_type_field(options)
|
28
|
-
|
29
|
-
# Balance records using TypeBalancer
|
30
|
-
balanced = TypeBalancer.balance(records, type_field: type_field)
|
27
|
+
balanced = TypeBalancer.balance(records, type_field: type_field)
|
31
28
|
return empty_relation if balanced.nil?
|
32
29
|
|
33
|
-
|
34
|
-
|
35
|
-
page = (options[:page] || 1).to_i
|
36
|
-
per_page = (options[:per_page] || 20).to_i
|
37
|
-
offset = (page - 1) * per_page
|
38
|
-
balanced = balanced[offset, per_page] || []
|
39
|
-
end
|
40
|
-
|
41
|
-
# Return as relation
|
42
|
-
TestRelation.new(balanced)
|
30
|
+
paged = apply_pagination(balanced, options)
|
31
|
+
build_result(paged)
|
43
32
|
end
|
44
33
|
|
45
|
-
def all_records = to_a
|
46
|
-
|
47
34
|
private
|
48
35
|
|
36
|
+
def apply_pagination(records, options)
|
37
|
+
return records unless options[:page] || options[:per_page]
|
38
|
+
|
39
|
+
page = (options[:page] || 1).to_i
|
40
|
+
per_page = (options[:per_page] || 20).to_i
|
41
|
+
offset = (page - 1) * per_page
|
42
|
+
records[offset, per_page] || []
|
43
|
+
end
|
44
|
+
|
45
|
+
def build_result(balanced)
|
46
|
+
if klass.respond_to?(:where) && klass != TestModel
|
47
|
+
ids = balanced.map(&:id)
|
48
|
+
results = klass.where(id: ids).to_a
|
49
|
+
results_by_id = results.index_by(&:id)
|
50
|
+
ordered = ids.map { |id| results_by_id[id] }
|
51
|
+
self.class.new(ordered)
|
52
|
+
else
|
53
|
+
self.class.new(balanced)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
49
57
|
def empty_relation
|
50
|
-
|
58
|
+
if klass.respond_to?(:none)
|
59
|
+
klass.none
|
60
|
+
else
|
61
|
+
[]
|
62
|
+
end
|
51
63
|
end
|
52
64
|
|
53
65
|
def fetch_type_field(options)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: type_balancer_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Carl Smith
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-04-
|
11
|
+
date: 2025-04-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|