type_balancer_rails 0.1.2 → 0.2.0
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: b638243aa1d2aa29637bf2696d7ce7e8625bed13c17bb73122ddb0d9f1356e6a
|
4
|
+
data.tar.gz: 4ac1aa33b569d4da75786e0a8a14ae35a5c2fb20fd3d86cdb1637ef630719809
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7fd6d6bcd22be7a11c2bd9d5181253c31f0df27966d7c4a68258c3a7e9b6690a42c4e7e4320d2ed543c4155caf2ae7a79e361202bf37661f2f6f6282f98ff29f
|
7
|
+
data.tar.gz: 22503105265aa58554a3e839d431ca3e4ac8d15f39c2b90a3099175cdd383bbdf4deb0982c97886a50ea6da71057258e308e8e4467ced627980c689dfd205f85
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.2.0] - 2024-04-27
|
4
|
+
|
5
|
+
- Refactored `balance_by_type` to only send `id` and type field to TypeBalancer for efficiency and clarity
|
6
|
+
- Ensured robust ordering of returned records based on balanced ids (supports flat and nested balancer output)
|
7
|
+
- Full TDD coverage for all new and refactored logic (unit and integration tests)
|
8
|
+
- Added GitHub Actions workflow for automated gem releases on tag push
|
9
|
+
|
3
10
|
## [0.1.0] - 2025-04-10
|
4
11
|
|
5
12
|
- Initial release
|
@@ -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,44 @@ 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
|
-
|
30
|
-
|
31
|
-
return empty_relation if balanced.nil?
|
32
|
-
|
33
|
-
# Handle pagination if requested
|
34
|
-
if options[:page] || options[:per_page]
|
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] || []
|
27
|
+
# Map to array of hashes with only id and type
|
28
|
+
id_and_type_hashes = records.map do |record|
|
29
|
+
{ id: record.id, type: record.send(type_field) }
|
39
30
|
end
|
31
|
+
balanced = TypeBalancer.balance(id_and_type_hashes, type_field: :type)
|
32
|
+
return empty_relation if balanced.nil?
|
40
33
|
|
41
|
-
|
42
|
-
|
34
|
+
paged = apply_pagination(balanced, options)
|
35
|
+
build_result(paged)
|
43
36
|
end
|
44
37
|
|
45
|
-
def all_records = to_a
|
46
|
-
|
47
38
|
private
|
48
39
|
|
40
|
+
def apply_pagination(records, options)
|
41
|
+
return records unless options[:page] || options[:per_page]
|
42
|
+
|
43
|
+
page = (options[:page] || 1).to_i
|
44
|
+
per_page = (options[:per_page] || 20).to_i
|
45
|
+
offset = (page - 1) * per_page
|
46
|
+
records[offset, per_page] || []
|
47
|
+
end
|
48
|
+
|
49
|
+
def build_result(balanced)
|
50
|
+
# Flatten in case balanced is a nested array
|
51
|
+
ids = balanced.flatten.map { |h| h[:id] }
|
52
|
+
# Map back to original records (works for both AR and TestRelation)
|
53
|
+
records_by_id = to_a.index_by(&:id)
|
54
|
+
ordered = ids.map { |id| records_by_id[id] }
|
55
|
+
self.class.new(ordered)
|
56
|
+
end
|
57
|
+
|
49
58
|
def empty_relation
|
50
|
-
|
59
|
+
if klass.respond_to?(:none)
|
60
|
+
klass.none
|
61
|
+
else
|
62
|
+
[]
|
63
|
+
end
|
51
64
|
end
|
52
65
|
|
53
66
|
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.
|
4
|
+
version: 0.2.0
|
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
|
@@ -115,7 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
115
115
|
- !ruby/object:Gem::Version
|
116
116
|
version: '0'
|
117
117
|
requirements: []
|
118
|
-
rubygems_version: 3.
|
118
|
+
rubygems_version: 3.4.19
|
119
119
|
signing_key:
|
120
120
|
specification_version: 4
|
121
121
|
summary: Rails integration for type_balancer
|