put 0.1.0 → 0.3.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: 91d380401f71b040bc67875987d94a474c6075e28e25197acf9eeea15bfa7d24
4
- data.tar.gz: 1f2c56fcb6258bb2d6762eff8aacebad67c83f405d0fc2bf9b863e8355daad8e
3
+ metadata.gz: ed8c542e17e5ecbc293a508843be66ab105238b761ed40e585980a2402ec6124
4
+ data.tar.gz: f7fef7196819fe61112639c965a4f5eca999dc0c72c94f6360574f02c94242f1
5
5
  SHA512:
6
- metadata.gz: a0048ac2c7ff377dc9f3944ff540786c8b352a6475e01c20db38839fe159ba109960e40c2ca6aa92b2d81b26097dc2c627d5270bdefa36f8f6d5858ed01493a1
7
- data.tar.gz: c11d0add1538cf3457d29b8a9a6d03ad11e8107f0e910af80a82c6699553852abb4e43180370e768dda8e8fbcc60d2518e3c93e4760a55d7f492fe3b1adb43ff
6
+ metadata.gz: 1b19851583cc9248409052e468a9bd913731eb04fe93ba16358965634740bbba5260626478fc6f7b5c7f4e2180c46e9fd6bdebbb6b6b19722a67209bfa020d23
7
+ data.tar.gz: 8c6216e264c6abe21db898b59c880f5aaa7276032a2f9b5c0db623f2496268f632b81e041b9e9fff2cd970c3158104f3b36881ae432de79088fb2c5f4ccbbffb
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## [0.3.0] - 2026-09-22
2
+
3
+ - Add Put.enum [#2](https://github.com/testdouble/put/issues/2)
4
+
5
+ ## [0.2.0] - 2026-09-22
6
+
7
+ - **[Breaking]** Drop support for Ruby 2.6; require Ruby >= 3.1
8
+
1
9
  ## [0.1.0] - 2022-09-22
2
10
 
3
11
  - Add Put.nils_first, Put.nils_last
data/Gemfile CHANGED
@@ -4,5 +4,10 @@ gemspec
4
4
 
5
5
  gem "debug"
6
6
  gem "rake"
7
- gem "minitest"
7
+ gem "minitest", "< 6"
8
+ gem "parallel", "< 2"
8
9
  gem "standard"
10
+ gem "ostruct"
11
+ gem "fiddle"
12
+ gem "racc"
13
+ gem "base64"
data/Gemfile.lock CHANGED
@@ -1,49 +1,70 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- put (0.1.0)
4
+ put (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- ast (2.4.2)
9
+ ast (2.4.3)
10
+ base64 (0.3.0)
10
11
  debug (1.6.2)
11
12
  irb (>= 1.3.6)
12
13
  reline (>= 0.3.1)
14
+ fiddle (1.1.8)
13
15
  io-console (0.5.11)
14
16
  irb (1.4.1)
15
17
  reline (>= 0.3.0)
16
- json (2.6.2)
17
- minitest (5.16.3)
18
- parallel (1.22.1)
19
- parser (3.1.2.1)
18
+ json (2.21.2)
19
+ language_server-protocol (3.17.0.6)
20
+ lint_roller (1.1.0)
21
+ minitest (5.27.0)
22
+ ostruct (0.6.3)
23
+ parallel (1.28.0)
24
+ parser (3.3.12.0)
20
25
  ast (~> 2.4.1)
26
+ racc
27
+ prism (1.9.0)
28
+ racc (1.8.1)
21
29
  rainbow (3.1.1)
22
30
  rake (13.0.6)
23
- regexp_parser (2.5.0)
31
+ regexp_parser (2.13.0)
24
32
  reline (0.3.1)
25
33
  io-console (~> 0.5)
26
- rexml (3.2.5)
27
- rubocop (1.35.1)
34
+ rubocop (1.88.2)
28
35
  json (~> 2.3)
29
- parallel (~> 1.10)
30
- parser (>= 3.1.2.1)
36
+ language_server-protocol (~> 3.17.0.2)
37
+ lint_roller (~> 1.1.0)
38
+ parallel (>= 1.10)
39
+ parser (>= 3.3.0.2)
31
40
  rainbow (>= 2.2.2, < 4.0)
32
- regexp_parser (>= 1.8, < 3.0)
33
- rexml (>= 3.2.5, < 4.0)
34
- rubocop-ast (>= 1.20.1, < 2.0)
41
+ regexp_parser (>= 2.9.3, < 3.0)
42
+ rubocop-ast (>= 1.49.0, < 2.0)
35
43
  ruby-progressbar (~> 1.7)
36
- unicode-display_width (>= 1.4.0, < 3.0)
37
- rubocop-ast (1.21.0)
38
- parser (>= 3.1.1.0)
39
- rubocop-performance (1.14.3)
40
- rubocop (>= 1.7.0, < 2.0)
41
- rubocop-ast (>= 0.4.0)
42
- ruby-progressbar (1.11.0)
43
- standard (1.16.1)
44
- rubocop (= 1.35.1)
45
- rubocop-performance (= 1.14.3)
46
- unicode-display_width (2.3.0)
44
+ unicode-display_width (>= 2.4.0, < 4.0)
45
+ rubocop-ast (1.50.0)
46
+ parser (>= 3.3.7.2)
47
+ prism (~> 1.7)
48
+ rubocop-performance (1.26.1)
49
+ lint_roller (~> 1.1)
50
+ rubocop (>= 1.75.0, < 2.0)
51
+ rubocop-ast (>= 1.47.1, < 2.0)
52
+ ruby-progressbar (1.13.0)
53
+ standard (1.56.0)
54
+ language_server-protocol (~> 3.17.0.2)
55
+ lint_roller (~> 1.0)
56
+ rubocop (~> 1.88.0)
57
+ standard-custom (~> 1.0.0)
58
+ standard-performance (~> 1.8)
59
+ standard-custom (1.0.2)
60
+ lint_roller (~> 1.0)
61
+ rubocop (~> 1.50)
62
+ standard-performance (1.9.0)
63
+ lint_roller (~> 1.1)
64
+ rubocop-performance (~> 1.26.0)
65
+ unicode-display_width (3.3.0)
66
+ unicode-emoji (~> 4.3)
67
+ unicode-emoji (4.3.0)
47
68
 
48
69
  PLATFORMS
49
70
  arm64-darwin-21
@@ -51,9 +72,14 @@ PLATFORMS
51
72
  x86_64-linux
52
73
 
53
74
  DEPENDENCIES
75
+ base64
54
76
  debug
55
- minitest
77
+ fiddle
78
+ minitest (< 6)
79
+ ostruct
80
+ parallel (< 2)
56
81
  put!
82
+ racc
57
83
  rake
58
84
  standard
59
85
 
data/README.md CHANGED
@@ -1,11 +1,13 @@
1
- # Put - put your things in order 💎
1
+ # Put puts your objects in order 💎
2
2
 
3
3
  Put pairs with
4
4
  [Enumerable#sort_by](https://ruby-doc.org/core-3.1.2/Enumerable.html#method-i-sort_by)
5
5
  to provide a more expressive, fault-tolerant, and configurable approach to
6
- sorting Ruby objects with multiple criteria.
6
+ sorting Ruby objects with multiple criteria. [Here's a screencast & short blog
7
+ post](https://blog.testdouble.com/talks/2022-09-28-a-better-way-to-sort-ruby-objects/)
8
+ that we put out, in case you're interested.
7
9
 
8
- # First, put Put in your Gemfile
10
+ ## Put "put" in your Gemfile
9
11
 
10
12
  You've probably already put a few gems in there, so why not put Put, too:
11
13
 
@@ -20,8 +22,8 @@ Of course after you push Put, your colleagues will wonder why you put Put there.
20
22
  A neat trick when applying complex sorting rules to a collection is to map them
21
23
  to an array of arrays of comparable values in priority order. It's a common
22
24
  approach (and a special subtype of what's called a [Schwartzian
23
- transform](https://en.wikipedia.org/wiki/Schwartzian_transform)), but this
24
- pattern doesn't have an widely-accepted name yet, so let's use code to explain.
25
+ transform](https://en.wikipedia.org/wiki/Schwartzian_transform)), but the
26
+ pattern doesn't have a widely-accepted name yet, so let's use code to explain.
25
27
 
26
28
  Suppose you have some people:
27
29
 
@@ -68,8 +70,8 @@ is almost begging for a code comment above it to explain its intent
68
70
  number](https://en.wikipedia.org/wiki/Magic_number_(programming))'s like `1` and
69
71
  `0`.
70
72
  * Any `nil` values will result in a bad time. If a person's `age` is nil, you'll
71
- get "_undefined method `*' for nil:NilClass_" `NoMethodError`
72
- * Relatedly, if any two items aren't comparable (e.g. `<=>` returns nil), you'll
73
+ get "_undefined method '*' for nil:NilClass_" `NoMethodError`
74
+ * Relatedly, if any two items aren't comparable (i.e. `<=>` returns nil), you'll
73
75
  be greeted with an inscrutable `ArgumentError` that just says "_comparison of
74
76
  Array with Array failed_"
75
77
 
@@ -95,8 +97,8 @@ The Put gem solves every one of the above issues:
95
97
  impenetrable `ArgumentError` messages whenever any two values turn out not to
96
98
  be comparable
97
99
 
98
- After reading this, your teammates are sure be glad they put you in charge of
99
- putting little gems like Put in the Gemfile.
100
+ After reading this, your teammates will be glad they put you in charge of
101
+ putting gems like Put in the Gemfile.
100
102
 
101
103
  ## When you Put it that way
102
104
 
@@ -116,8 +118,8 @@ either `Put.first` or `nil`, like this:
116
118
 
117
119
  ### Put.last
118
120
 
119
- When items that meet a certain condition should go to the bottom of the list,
120
- you can do the same sort of conditional expression with `Put.last`:
121
+ When a sort criteria should go to the bottom of the list, you can do the same
122
+ sort of conditional expression with `Put.last`:
121
123
 
122
124
  ```ruby
123
125
  %w[Jin drinks Gin on Gym day].sort_by { |s|
@@ -203,6 +205,31 @@ As you might be able to guess, `Put.nils_last` puts the nils last:
203
205
  } # => [:every, :counts, nil]
204
206
  ```
205
207
 
208
+ ### Put.enum(value, order:, nils_first: false)
209
+
210
+ When you're sorting by a discrete attribute — a role, a status, a tier — that
211
+ doesn't have a natural order of its own, `Put.enum` sorts a value by its
212
+ position in a list you provide, instead of you writing a `case`/`when`
213
+ translation table by hand:
214
+
215
+ ```ruby
216
+ [:director, :staff, :manager].sort_by { |role|
217
+ [Put.enum(role, order: [:staff, :manager, :director])]
218
+ } # => [:staff, :manager, :director]
219
+ ```
220
+
221
+ A value that isn't in `order` sorts after every value that is, rather than
222
+ raising:
223
+
224
+ ```ruby
225
+ [:manager, :contractor, :staff].sort_by { |role|
226
+ [Put.enum(role, order: [:staff, :manager, :director])]
227
+ } # => [:staff, :manager, :contractor]
228
+ ```
229
+
230
+ Like `Put.asc` and `Put.desc`, `Put.enum` also takes a `nils_first` keyword
231
+ argument (defaulting to `false`) for where `nil` values should sort.
232
+
206
233
  ### Put.debug(sorting_arrays)
207
234
 
208
235
  If you see "comparison of Array with Array failed" and you don't have any idea
@@ -226,4 +253,4 @@ conduct](https://testdouble.com/code-of-conduct) for all community interactions,
226
253
  including (but not limited to) one-on-one communications, public posts/comments,
227
254
  code reviews, pull requests, and GitHub issues. If violations occur, Test Double
228
255
  will take any action they deem appropriate for the infraction, up to and
229
- including blocking a user from the organization's repositories.
256
+ including blocking a user from the organization's repositories.
@@ -0,0 +1,10 @@
1
+ module Put
2
+ class PutsThing
3
+ class Enum < InOrder
4
+ def initialize(value, order:, nils_first: false)
5
+ rank = value.nil? ? nil : (order.index(value) || Float::INFINITY)
6
+ super(rank, nils_first: nils_first)
7
+ end
8
+ end
9
+ end
10
+ end
data/lib/put/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Put
2
- VERSION = "0.1.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/put.rb CHANGED
@@ -6,6 +6,7 @@ require_relative "put/puts_thing/anywhere"
6
6
  require_relative "put/puts_thing/first"
7
7
  require_relative "put/puts_thing/last"
8
8
  require_relative "put/puts_thing/in_order"
9
+ require_relative "put/puts_thing/enum"
9
10
  require_relative "put/puts_thing/ascending"
10
11
  require_relative "put/puts_thing/descending"
11
12
  require_relative "put/puts_thing/nil_order"
@@ -29,6 +30,10 @@ module Put
29
30
  PutsThing::Ascending.new(value, nils_first: nils_first)
30
31
  end
31
32
 
33
+ def self.enum(value, order:, nils_first: false)
34
+ PutsThing::Enum.new(value, order: order, nils_first: nils_first)
35
+ end
36
+
32
37
  def self.nils_first(value)
33
38
  PutsThing::NilsFirst.new(value)
34
39
  end
metadata CHANGED
@@ -1,16 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: put
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Searls
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2022-09-22 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies: []
13
- description:
14
12
  email:
15
13
  - searls@gmail.com
16
14
  executables: []
@@ -31,6 +29,7 @@ files:
31
29
  - lib/put/puts_thing/anywhere.rb
32
30
  - lib/put/puts_thing/ascending.rb
33
31
  - lib/put/puts_thing/descending.rb
32
+ - lib/put/puts_thing/enum.rb
34
33
  - lib/put/puts_thing/first.rb
35
34
  - lib/put/puts_thing/in_order.rb
36
35
  - lib/put/puts_thing/last.rb
@@ -38,7 +37,6 @@ files:
38
37
  - lib/put/puts_thing/nils_first.rb
39
38
  - lib/put/puts_thing/nils_last.rb
40
39
  - lib/put/version.rb
41
- - put.gemspec
42
40
  - sig/put.rbs
43
41
  homepage: https://github.com/testdouble/put
44
42
  licenses:
@@ -47,7 +45,6 @@ metadata:
47
45
  homepage_uri: https://github.com/testdouble/put
48
46
  source_code_uri: https://github.com/testdouble/put
49
47
  changelog_uri: https://github.com/testdouble/put/blob/main/CHANGELOG.md
50
- post_install_message:
51
48
  rdoc_options: []
52
49
  require_paths:
53
50
  - lib
@@ -55,15 +52,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
55
52
  requirements:
56
53
  - - ">="
57
54
  - !ruby/object:Gem::Version
58
- version: 2.6.0
55
+ version: 3.1.0
59
56
  required_rubygems_version: !ruby/object:Gem::Requirement
60
57
  requirements:
61
58
  - - ">="
62
59
  - !ruby/object:Gem::Version
63
60
  version: '0'
64
61
  requirements: []
65
- rubygems_version: 3.3.20
66
- signing_key:
62
+ rubygems_version: 4.0.21
67
63
  specification_version: 4
68
64
  summary: Put helps you write prioritized, multi-variate sort_by blocks
69
65
  test_files: []
data/put.gemspec DELETED
@@ -1,28 +0,0 @@
1
- require_relative "lib/put/version"
2
-
3
- Gem::Specification.new do |spec|
4
- spec.name = "put"
5
- spec.version = Put::VERSION
6
- spec.authors = ["Justin Searls"]
7
- spec.email = ["searls@gmail.com"]
8
-
9
- spec.summary = "Put helps you write prioritized, multi-variate sort_by blocks"
10
- spec.homepage = "https://github.com/testdouble/put"
11
- spec.license = "MIT"
12
- spec.required_ruby_version = ">= 2.6.0"
13
-
14
- spec.metadata["homepage_uri"] = spec.homepage
15
- spec.metadata["source_code_uri"] = spec.homepage
16
- spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
17
-
18
- # Specify which files should be added to the gem when it is released.
19
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
- spec.files = Dir.chdir(__dir__) do
21
- `git ls-files -z`.split("\x0").reject do |f|
22
- (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
23
- end
24
- end
25
- spec.bindir = "exe"
26
- spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
27
- spec.require_paths = ["lib"]
28
- end