sortsmith 0.1.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5c5bfb3e13979aff55f28fd2a7a35e39989ca4d4b50d1d24e66ff3b280ae4467
4
+ data.tar.gz: 4bd7e565493601604d2386e9477353f2c7f9fd1008ad52cf90091e63210b511e
5
+ SHA512:
6
+ metadata.gz: 9035b9533826d73e4efd724d9b84cc92efc59760b02aafd96d334050b8936161f4935c3b5e32c26129c87935dee616e6ba5329b1d8cf8910133236c792ae6008
7
+ data.tar.gz: b27f07bf73948720d7da605385d190884d12ad89999cdb8900f84c65c94d7680c19555bd2122f43452546b361805287167d96d82e12a47cc61eb8f3c49881c6c
data/.envrc ADDED
@@ -0,0 +1 @@
1
+ use flake
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 3.3
data/CHANGELOG.md ADDED
@@ -0,0 +1,33 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ### Added
11
+
12
+ ### Changed
13
+
14
+ ### Removed
15
+
16
+ ## [0.1.1] - 12025-01-15
17
+
18
+ ### Changed
19
+ - Improved handling of non-string objects when sorting
20
+
21
+ ## [0.1.0] - 12025-01-14
22
+
23
+ ### Added
24
+ - Initial implementation of `Sortsmith::Sorter`
25
+ - Support for case-insensitive sorting
26
+ - Support for sorting by hash keys and object methods
27
+ - Ascending and descending sort directions
28
+ - Type checking with Steep/RBS
29
+ - GitHub Actions workflow for automated testing and type checking
30
+
31
+ [unreleased]: https://github.com/itsthedevman/sortsmith/compare/v0.1.1...HEAD
32
+ [0.2.0]: https://github.com/itsthedevman/sortsmith/compare/v0.1.0...v0.1.1
33
+ [0.1.0]: https://github.com/itsthedevman/sortsmith/compare/ac357965a1bc641d187333a5b032c5a423020ae9...v0.1.0
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Bryan "itsthedevman"
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,179 @@
1
+ # Sortsmith
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/sortsmith.svg)](https://badge.fury.io/rb/sortsmith)
4
+ [![Tests](https://github.com/itsthedevman/sortsmith/actions/workflows/main.yml/badge.svg)](https://github.com/itsthedevman/sortsmith/actions/workflows/main.yml)
5
+ ![Ruby Version](https://img.shields.io/badge/ruby-3.3.6-ruby)
6
+
7
+ Sortsmith is a flexible sorting library for Ruby that makes complex sorting operations simple and composable. It makes handling common sorting patterns like case-insensitive sorting of hashes and objects easy, while remaining extensible for custom sorting needs.
8
+
9
+ ## Features
10
+
11
+ - Builder pattern for chainable sorting configuration
12
+ - Built-in support for case-insensitive sorting
13
+ - Hash key and method/attribute sorting
14
+ - Flexible transformation pipeline
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem "sortsmith"
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ ```bash
27
+ $ bundle install
28
+ ```
29
+
30
+ Or install it yourself as:
31
+
32
+ ```bash
33
+ $ gem install sortsmith
34
+ ```
35
+
36
+ ## Usage
37
+
38
+ ### Basic Sorting
39
+
40
+ ```ruby
41
+ # Sort an array of Strings
42
+ users = ["Bob", "Alice", "Carol"]
43
+
44
+ sorted_users = Sortsmith::Sorter.new(users).sort
45
+
46
+ # Result: ["Alice", "Bob", "Carol"]
47
+ ```
48
+
49
+ ### Hash Sorting
50
+
51
+ ```ruby
52
+ # Sort an array of hashes by a key
53
+ users = [
54
+ { name: "Carol", age: 35 },
55
+ { name: "Bob", age: 25 },
56
+ { name: "Alice", age: 30 }
57
+ ]
58
+
59
+ sorted_users = Sortsmith::Sorter.new(users)
60
+ .by_key(:name)
61
+ .sort
62
+
63
+ # Result: [{ name: "Alice" }, { name: "Bob" }, { name: "Carol" }]
64
+ ```
65
+
66
+ ### Object Sorting
67
+
68
+ ```ruby
69
+ # Sort objects by method/attribute
70
+ class User < Data.define(:name)
71
+ end
72
+
73
+ users = [User.new(name: "Bob"), User.new(name: "Carol"), User.new(name: "Alice")]
74
+
75
+ sorted_users = Sortsmith::Sorter.new(users)
76
+ .by_method(:name)
77
+ .sort
78
+
79
+ # Result: [#<data User name="Alice">, #<data User name="Bob">, #<data User name="Carol">]
80
+ ```
81
+
82
+ ### Case Insensitive Sorting
83
+
84
+ ```ruby
85
+ users = [
86
+ {"name" => "bob"},
87
+ {"name" => "Billy"},
88
+ {"name" => "Alice"},
89
+ {"name" => "carol"},
90
+ {"name" => "Cassidy"},
91
+ {"name" => "alex"}
92
+ ]
93
+
94
+ # Order of methods does not matter
95
+ # However, the hash's key type does
96
+ sorted_users = Sortsmith::Sorter.new(users)
97
+ .case_insensitive
98
+ .by_key("name")
99
+ .sort
100
+
101
+ # Result: [{"name"=>"Alice"}, {"name"=>"alex"}, {"name"=>"Billy"}, {"name"=>"bob"}, {"name"=>"Cassidy"}, {"name"=>"carol"}]
102
+ ```
103
+
104
+ ### Reverse Sorting
105
+
106
+ ```ruby
107
+ # Sort in descending order
108
+ sorted_desc = Sortsmith::Sorter.new(array)
109
+ .by_attribute(:name)
110
+ .desc
111
+ .sort
112
+ ```
113
+
114
+ ## Development
115
+
116
+ ### Prerequisites
117
+
118
+ - Ruby 3.3.6
119
+ - Nix with Direnv (optional, but recommended)
120
+
121
+ ### Setting Up the Development Environment
122
+
123
+ With Nix:
124
+ ```bash
125
+ direnv allow
126
+ ```
127
+
128
+ Without Nix:
129
+ ```bash
130
+ bundle install
131
+ ```
132
+
133
+ ### Running Tests
134
+
135
+ ```bash
136
+ bundle exec rake test
137
+ ```
138
+
139
+ ### Type Checking
140
+
141
+ ```bash
142
+ bundle exec steep check
143
+ ```
144
+
145
+ ### Code Style
146
+
147
+ This project uses StandardRB. To check your code:
148
+
149
+ ```bash
150
+ bundle exec standardrb
151
+ ```
152
+
153
+ To automatically fix issues:
154
+
155
+ ```bash
156
+ bundle exec standardrb --fix
157
+ ```
158
+
159
+ ## Contributing
160
+
161
+ 1. Fork it
162
+ 2. Create your feature branch (`git checkout -b feature/my-new-feature`)
163
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
164
+ 4. Push to the branch (`git push origin feature/my-new-feature`)
165
+ 5. Create new Pull Request
166
+
167
+ Please note that this project is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md). By participating in this project you agree to abide by its terms.
168
+
169
+ ## License
170
+
171
+ The gem is available as open source under the terms of the [MIT License](LICENSE.md).
172
+
173
+ ## Changelog
174
+
175
+ See [CHANGELOG.md](CHANGELOG.md) for a list of changes.
176
+
177
+ ## Credits
178
+
179
+ - Author: Bryan "itsthedevman"
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[test standard]
data/Steepfile ADDED
@@ -0,0 +1,8 @@
1
+ # D = Steep::Diagnostic
2
+
3
+ target :lib do
4
+ signature "sig"
5
+ ignore_signature "sig/test"
6
+
7
+ check "lib"
8
+ end
data/flake.lock ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "nodes": {
3
+ "flake-utils": {
4
+ "inputs": {
5
+ "systems": "systems"
6
+ },
7
+ "locked": {
8
+ "lastModified": 1731533236,
9
+ "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
10
+ "owner": "numtide",
11
+ "repo": "flake-utils",
12
+ "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
13
+ "type": "github"
14
+ },
15
+ "original": {
16
+ "owner": "numtide",
17
+ "repo": "flake-utils",
18
+ "type": "github"
19
+ }
20
+ },
21
+ "nixpkgs": {
22
+ "locked": {
23
+ "lastModified": 1736798957,
24
+ "narHash": "sha256-qwpCtZhSsSNQtK4xYGzMiyEDhkNzOCz/Vfu4oL2ETsQ=",
25
+ "owner": "NixOS",
26
+ "repo": "nixpkgs",
27
+ "rev": "9abb87b552b7f55ac8916b6fc9e5cb486656a2f3",
28
+ "type": "github"
29
+ },
30
+ "original": {
31
+ "owner": "NixOS",
32
+ "ref": "nixos-unstable",
33
+ "repo": "nixpkgs",
34
+ "type": "github"
35
+ }
36
+ },
37
+ "root": {
38
+ "inputs": {
39
+ "flake-utils": "flake-utils",
40
+ "nixpkgs": "nixpkgs"
41
+ }
42
+ },
43
+ "systems": {
44
+ "locked": {
45
+ "lastModified": 1681028828,
46
+ "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
47
+ "owner": "nix-systems",
48
+ "repo": "default",
49
+ "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
50
+ "type": "github"
51
+ },
52
+ "original": {
53
+ "owner": "nix-systems",
54
+ "repo": "default",
55
+ "type": "github"
56
+ }
57
+ }
58
+ },
59
+ "root": "root",
60
+ "version": 7
61
+ }
data/flake.nix ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ description = "Ruby 3.3.6 development environment";
3
+
4
+ inputs = {
5
+ nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
6
+ flake-utils.url = "github:numtide/flake-utils";
7
+ };
8
+
9
+ outputs = { self, nixpkgs, flake-utils }:
10
+ flake-utils.lib.eachDefaultSystem (system:
11
+ let
12
+ pkgs = nixpkgs.legacyPackages.${system};
13
+ in
14
+ {
15
+ devShells.default = pkgs.mkShell {
16
+ buildInputs = with pkgs; [
17
+ (ruby_3_3.override {
18
+ jemallocSupport = true;
19
+ docSupport = false;
20
+ })
21
+
22
+ # Dependencies for native gems
23
+ pkg-config
24
+ openssl
25
+ readline
26
+ zstd
27
+ libyaml
28
+ ];
29
+
30
+ shellHook = ''
31
+ export GEM_HOME="$PWD/vendor/bundle"
32
+ export GEM_PATH="$GEM_HOME"
33
+ export PATH="$GEM_HOME/bin:$PATH"
34
+
35
+ echo "checking gems"
36
+ bundle check || bundle install
37
+ '';
38
+ };
39
+ }
40
+ );
41
+ }
@@ -0,0 +1,178 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sortsmith
4
+ class Sorter
5
+ #
6
+ # Creates a Sorter builder instance
7
+ #
8
+ # @param enumerable [Enumerable] The enumerable (Array, Hash) to sort
9
+ #
10
+ def initialize(enumerable)
11
+ @enumerable = enumerable
12
+ @pipeline = []
13
+ @direction = :asc
14
+ end
15
+
16
+ #
17
+ # Finalizes the Sorter instance and sorts the enumerable
18
+ #
19
+ # @return [Enumerable] The sorted enumerable
20
+ #
21
+ def sort
22
+ filter_steps = select_filter_steps
23
+ transformation_steps = select_transformation_steps
24
+
25
+ result =
26
+ @enumerable.sort do |left, right|
27
+ filter_steps.each do |step|
28
+ left = step.perform(left)
29
+ right = step.perform(right)
30
+ end
31
+
32
+ left_priority = type_priority(left)
33
+ right_priority = type_priority(right)
34
+
35
+ # Apply the transformation pipeline only for same-type comparisons
36
+ if left_priority == right_priority
37
+ left = apply_transformations(transformation_steps, left)
38
+ right = apply_transformations(transformation_steps, right)
39
+
40
+ left <=> right
41
+ else
42
+ left_priority <=> right_priority
43
+ end
44
+ end
45
+
46
+ (@direction == :asc) ? result : result.reverse
47
+ end
48
+
49
+ #
50
+ # Adds a "filter" step to the sort pipeline.
51
+ # Filter steps are used to get data from the current item being sorted
52
+ # These are performed before transformation steps
53
+ #
54
+ # @param & [Proc] The block to execute
55
+ #
56
+ # @return [Self] The sorter instance
57
+ #
58
+ def add_filter(&)
59
+ add_step(type: Step::FILTER, &)
60
+ end
61
+
62
+ #
63
+ # Adds a "transformation" step to the sort pipeline
64
+ # Transformation steps are used to transform data.
65
+ # These are performed after filter steps
66
+ #
67
+ # @param & [Proc] The block to execute
68
+ #
69
+ # @return [Self] The sorter instance
70
+ #
71
+ def add_transformation(&)
72
+ add_step(type: Step::TRANSFORMATION, &)
73
+ end
74
+
75
+ #
76
+ # Instructs the sorter to perform a fetch by key on the Hash being sorted
77
+ #
78
+ # @param key [String, Symbol, Any] The hash key to fetch
79
+ #
80
+ # @return [Self] The sorter instance
81
+ #
82
+ def by_key(key)
83
+ add_filter { |i| i&.fetch(key) }
84
+ self
85
+ end
86
+
87
+ #
88
+ # Instructs the sorter to perform a method call on the object being sorted
89
+ #
90
+ # @param method [String, Symbol] The method name to call
91
+ #
92
+ # @return [Self] The sorter instance
93
+ #
94
+ def by_method(method)
95
+ add_filter { |i| i&.public_send(method) }
96
+ end
97
+
98
+ alias_method :by_attribute, :by_method
99
+
100
+ #
101
+ # Instructs the sorter to sort by a case insensitive value
102
+ # This will prioritize capital letters first, followed by their lowercase counterparts
103
+ #
104
+ # @return [Self] The sorter instance
105
+ #
106
+ def case_insensitive
107
+ add_transformation do |item|
108
+ case item
109
+ when String
110
+ item.chars.flat_map { |c| [c.downcase, c] }
111
+ else
112
+ item
113
+ end
114
+ end
115
+ end
116
+
117
+ #
118
+ # Controls which direction the array will be sorted
119
+ #
120
+ # @return [Self] The sorter instance
121
+ #
122
+ def asc
123
+ @direction = :asc
124
+ self
125
+ end
126
+
127
+ alias_method :forward, :asc
128
+
129
+ #
130
+ # Controls which direction the array will be sorted
131
+ #
132
+ # @return [Self] The sorter instance
133
+ #
134
+ def desc
135
+ @direction = :desc
136
+ self
137
+ end
138
+
139
+ alias_method :reverse, :desc
140
+
141
+ private
142
+
143
+ def add_step(type:, &block)
144
+ @pipeline << Step.new(type:, block:)
145
+ self
146
+ end
147
+
148
+ def select_filter_steps
149
+ @pipeline.select { |s| s.type == Step::FILTER }
150
+ end
151
+
152
+ def select_transformation_steps
153
+ @pipeline.select { |s| s.type == Step::TRANSFORMATION }
154
+ end
155
+
156
+ def type_priority(value)
157
+ case value
158
+ when NilClass then 0
159
+ when Numeric then 1
160
+ when String then 2
161
+ when Array then 3
162
+ when Hash then 4
163
+ else
164
+ 5
165
+ end
166
+ end
167
+
168
+ def apply_transformations(steps, value)
169
+ result = value
170
+
171
+ steps.each do |step|
172
+ result = step.perform(result)
173
+ end
174
+
175
+ result
176
+ end
177
+ end
178
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sortsmith
4
+ #
5
+ # Represents a step in the sorting process
6
+ #
7
+ class Step < Data.define(:type, :block)
8
+ TYPES = [
9
+ TRANSFORMATION = :transformation,
10
+ FILTER = :filter
11
+ ].freeze
12
+
13
+ def perform(item)
14
+ block.call(item)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sortsmith
4
+ VERSION = "0.1.1"
5
+ end
data/lib/sortsmith.rb ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "sortsmith/sorter"
4
+ require_relative "sortsmith/step"
5
+ require_relative "sortsmith/version"
6
+
7
+ module Sortsmith
8
+ class Error < StandardError; end
9
+ end
@@ -0,0 +1,107 @@
1
+ module Sortsmith
2
+ interface _Sortable
3
+ def to_s: () -> String
4
+ def <=>: (self) -> Integer
5
+ end
6
+
7
+ class Sorter[Elem < _Sortable]
8
+ @enumerable: Enumerable[Elem]
9
+
10
+ @pipeline: Array[Step]
11
+
12
+ @direction: Symbol
13
+
14
+ #
15
+ # Creates a Sorter builder instance
16
+ #
17
+ # @param enumerable [Enumerable] The enumerable (Array, Hash) to sort
18
+ #
19
+ def initialize: (Enumerable[Elem] enumerable) -> void
20
+
21
+ #
22
+ # Finalizes the Sorter instance and sorts the enumerable
23
+ #
24
+ # @return [Enumerable] The sorted enumerable
25
+ #
26
+ def sort: () -> Enumerable[Elem]
27
+
28
+ #
29
+ # Adds a "filter" step to the sort pipeline.
30
+ # Filter steps are used to get data from the current item being sorted
31
+ # These are performed before transformation steps
32
+ #
33
+ # @param & [Proc] The block to execute
34
+ #
35
+ # @return [Self] The sorter instance
36
+ #
37
+ def add_filter: () { (untyped) -> untyped } -> self
38
+
39
+ #
40
+ # Adds a "transformation" step to the sort pipeline
41
+ # Transformation steps are used to transform data.
42
+ # These are performed after filter steps
43
+ #
44
+ # @param & [Proc] The block to execute
45
+ #
46
+ # @return [Self] The sorter instance
47
+ #
48
+ def add_transformation: () { (untyped) -> untyped } -> self
49
+
50
+ #
51
+ # Instructs the sorter to perform a fetch by key on the Hash being sorted
52
+ #
53
+ # @param key [String, Symbol, Any] The hash key to fetch
54
+ #
55
+ # @return [Self] The sorter instance
56
+ #
57
+ def by_key: ((String | Symbol | untyped) key) -> self
58
+
59
+ #
60
+ # Instructs the sorter to perform a method call on the object being sorted
61
+ #
62
+ # @param method [String, Symbol] The method name to call
63
+ #
64
+ # @return [Self] The sorter instance
65
+ #
66
+ def by_method: ((String | Symbol) method) -> self
67
+
68
+ alias by_attribute by_method
69
+
70
+ #
71
+ # Instructs the sorter to sort by a case insensitive value
72
+ #
73
+ # @return [Self] The sorter instance
74
+ #
75
+ def case_insensitive: () -> self
76
+
77
+ #
78
+ # Controls which direction the array will be sorted
79
+ #
80
+ # @return [Self] The sorter instance
81
+ #
82
+ def asc: () -> self
83
+
84
+ alias forward asc
85
+
86
+ #
87
+ # Controls which direction the array will be sorted
88
+ #
89
+ # @return [Self] The sorter instance
90
+ #
91
+ def desc: () -> self
92
+
93
+ alias reverse desc
94
+
95
+ private
96
+
97
+ def add_step: (type: Symbol) { (untyped) -> untyped } -> self
98
+
99
+ def select_filter_steps: () -> Array[Step]
100
+
101
+ def select_transformation_steps: () -> Array[Step]
102
+
103
+ def type_priority: (untyped value) -> Integer
104
+
105
+ def apply_transformations: (Array[Step] steps, untyped value) -> untyped
106
+ end
107
+ end
@@ -0,0 +1,28 @@
1
+ module Sortsmith
2
+ #
3
+ # Represents a step in the sorting process
4
+ #
5
+ class Step < ::Data
6
+ TYPES: ::Array[Symbol]
7
+
8
+ FILTER: Symbol
9
+
10
+ TRANSFORMATION: Symbol
11
+
12
+ attr_reader type: Symbol
13
+
14
+ attr_reader block: ^(untyped) -> untyped
15
+
16
+ def self.new: (Symbol type, untyped block) -> instance
17
+ | (type: Symbol, block: untyped) -> instance
18
+
19
+ def self.[]: (Symbol type, untyped block) -> instance
20
+ | (type: Symbol, block: untyped) -> instance
21
+
22
+ def self.members: () -> [ :type, :block ]
23
+
24
+ def members: () -> [ :type, :block ]
25
+
26
+ def perform: (untyped item) -> untyped
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module Sortsmith
2
+ VERSION: String
3
+ end
data/sig/sortsmith.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Sortsmith
2
+ class Error < StandardError
3
+ end
4
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sortsmith
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Bryan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 1980-01-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Sortsmith provides a flexible, chainable API for sorting Ruby collections.
14
+ It supports sorting by object keys, methods, case sensitivity, and custom transformations.
15
+ email:
16
+ - bryan@itsthedevman.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".envrc"
22
+ - ".standard.yml"
23
+ - CHANGELOG.md
24
+ - CODE_OF_CONDUCT.md
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - Steepfile
29
+ - flake.lock
30
+ - flake.nix
31
+ - lib/sortsmith.rb
32
+ - lib/sortsmith/sorter.rb
33
+ - lib/sortsmith/step.rb
34
+ - lib/sortsmith/version.rb
35
+ - sig/sortsmith.rbs
36
+ - sig/sortsmith/sorter.rbs
37
+ - sig/sortsmith/step.rbs
38
+ - sig/sortsmith/version.rbs
39
+ homepage: https://github.com/itsthedevman/sortsmith
40
+ licenses:
41
+ - MIT
42
+ metadata:
43
+ source_code_uri: https://github.com/itsthedevman/sortsmith
44
+ changelog_uri: https://github.com/itsthedevman/sortsmith/blob/main/CHANGELOG.md
45
+ bug_tracker_uri: https://github.com/itsthedevman/sortsmith/issues
46
+ documentation_uri: https://github.com/itsthedevman/sortsmith#readme
47
+ rubygems_mfa_required: 'true'
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 3.0.0
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubygems_version: 3.5.22
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: A flexible Ruby sorting library with a chainable API for transforming and
67
+ filtering collections. Supports sorting by keys, methods, case sensitivity, and
68
+ custom transformations.
69
+ test_files: []