errgonomic 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9f84b5db433f20f5b75dbfad878c881d7f536d6c169dd43a31527ca4e4380f54
4
+ data.tar.gz: 2415019deb923d5e5ba887786e457901883914039e23f2450ea7127c712c74da
5
+ SHA512:
6
+ metadata.gz: 1149999bdc13744158fbe5ddf5523528cccd32b8c0de1371d39da0d5cc0405f4be82c84b97ec74917bcd101dd3d8d6dbdc9d62ae6523dee5c6a101e507629c2d
7
+ data.tar.gz: ab8db80b9b3bec04a74ce464ab070c15ef6fcd8dd24af6fe8c4608f1ea256004c34806b07579deb5ca2626f5675c0eceb6818112f985cc9d904acc9e46de9962
data/.envrc ADDED
@@ -0,0 +1,2 @@
1
+ watch_file gemset.nix
2
+ use flake
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 3.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-02-27
4
+
5
+ - Initial release
@@ -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 Nick Zadrozny
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,72 @@
1
+ # Errgonomic
2
+
3
+ Errgonomic provides some lightweight, opinionated ergonomics for error handling in Ruby. These semantics are a blend of Rails `present?` conventions, and Rust `Option` and `Result` type combinators. Without going full Option and Result. Probably.
4
+
5
+ The `present_or` method takes what you might ordinarily write as `foo || default` with a possible nil or falsey value, and brings that to any other object that may be `blank?`.
6
+
7
+ ```ruby
8
+ nil.present_or("default")
9
+ # => "default"
10
+
11
+ [].present_or(["default"])
12
+ # => ["default"]
13
+ ```
14
+
15
+ We don't have static type checking here in Ruby, so the library is also _annoyingly_ pedantic about matching classes for the supplied default value.
16
+
17
+ ```ruby
18
+ [].present_or("uh-oh")
19
+ # => Type mismatch: default value is a String but original was a Array (Errgonomic::TypeMismatchError)
20
+ ```
21
+
22
+ When constructing that fallback object may be expensive, you can provide a block instead:
23
+
24
+ ```ruby
25
+ [].present_or_else { ["default"] }
26
+ # => ["default"]
27
+ ```
28
+
29
+ And when all else fails, you can control the failure, by raising an exception for blank objects. This can be preferable to sending a blank object to some other downstream code that may be expecting a value, causing an ambiguous failure.
30
+
31
+ ```ruby
32
+ irb(main):007> [].present_or_raise("foo")
33
+ # => foo (Errgonomic::NotPresentError)
34
+ ```
35
+
36
+ ## Installation
37
+
38
+ TODO: Replace `errgonomic` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
39
+
40
+ Install the gem and add to the application's Gemfile by executing:
41
+
42
+ ```bash
43
+ bundle add errgonomic
44
+ ```
45
+
46
+ If bundler is not being used to manage dependencies, install the gem by executing:
47
+
48
+ ```bash
49
+ gem install errgonomic
50
+ ```
51
+
52
+ ## Usage
53
+
54
+ TODO: Write usage instructions here
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
59
+
60
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/omc/errgonomic. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/nz/errgonomic/blob/main/CODE_OF_CONDUCT.md).
65
+
66
+ ## License
67
+
68
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
69
+
70
+ ## Code of Conduct
71
+
72
+ Everyone interacting in the Errgonomic project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/nz/errgonomic/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
data/flake.lock ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "nodes": {
3
+ "nixpkgs": {
4
+ "locked": {
5
+ "lastModified": 1740463929,
6
+ "narHash": "sha256-4Xhu/3aUdCKeLfdteEHMegx5ooKQvwPHNkOgNCXQrvc=",
7
+ "owner": "nixos",
8
+ "repo": "nixpkgs",
9
+ "rev": "5d7db4668d7a0c6cc5fc8cf6ef33b008b2b1ed8b",
10
+ "type": "github"
11
+ },
12
+ "original": {
13
+ "owner": "nixos",
14
+ "ref": "nixos-24.11",
15
+ "repo": "nixpkgs",
16
+ "type": "github"
17
+ }
18
+ },
19
+ "root": {
20
+ "inputs": {
21
+ "nixpkgs": "nixpkgs"
22
+ }
23
+ }
24
+ },
25
+ "root": "root",
26
+ "version": 7
27
+ }
data/flake.nix ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ description = "Errgonomic";
3
+
4
+ inputs = {
5
+ nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-24.11";
6
+ };
7
+
8
+ outputs =
9
+ inputs@{ self, nixpkgs, ... }:
10
+ let
11
+ allSystems = [
12
+ "x86_64-linux"
13
+ "aarch64-linux"
14
+ "x86_64-darwin"
15
+ "aarch64-darwin"
16
+ ];
17
+ overlays = [
18
+ (final: prev: {
19
+ gems = final.bundlerEnv {
20
+ name = "errgonomic";
21
+ gemdir = ./.;
22
+ # src = final.lib.cleanSource ../.;
23
+ };
24
+ })
25
+ ];
26
+ forAllSystems =
27
+ f:
28
+ nixpkgs.lib.genAttrs allSystems (
29
+ system:
30
+ f {
31
+ pkgs = import nixpkgs { inherit system overlays; };
32
+ inherit system;
33
+ }
34
+ );
35
+ in
36
+ {
37
+ devShells = forAllSystems (
38
+ { pkgs, ... }:
39
+ {
40
+ default = pkgs.mkShell {
41
+ buildInputs = [
42
+ pkgs.ruby
43
+ pkgs.bundix
44
+ pkgs.gems
45
+ ];
46
+ };
47
+ }
48
+ );
49
+
50
+ };
51
+ }
data/gemset.nix ADDED
@@ -0,0 +1,284 @@
1
+ {
2
+ ast = {
3
+ groups = ["default"];
4
+ platforms = [];
5
+ source = {
6
+ remotes = ["https://rubygems.org"];
7
+ sha256 = "04nc8x27hlzlrr5c2gn7mar4vdr0apw5xg22wp6m8dx3wqr04a0y";
8
+ type = "gem";
9
+ };
10
+ version = "2.4.2";
11
+ };
12
+ concurrent-ruby = {
13
+ groups = ["default"];
14
+ platforms = [];
15
+ source = {
16
+ remotes = ["https://rubygems.org"];
17
+ sha256 = "1ipbrgvf0pp6zxdk5ascp6i29aybz2bx9wdrlchjmpx6mhvkwfw1";
18
+ type = "gem";
19
+ };
20
+ version = "1.3.5";
21
+ };
22
+ diff-lcs = {
23
+ groups = ["default"];
24
+ platforms = [];
25
+ source = {
26
+ remotes = ["https://rubygems.org"];
27
+ sha256 = "0bnss89lcm3b1k3xcjd35grxqz5q040d12imd73qybwnfarggrx1";
28
+ type = "gem";
29
+ };
30
+ version = "1.6.0";
31
+ };
32
+ errgonomic = {
33
+ dependencies = ["concurrent-ruby"];
34
+ groups = ["default"];
35
+ platforms = [];
36
+ source = {
37
+ path = ./.;
38
+ type = "path";
39
+ };
40
+ version = "0.1.0";
41
+ };
42
+ json = {
43
+ groups = ["default"];
44
+ platforms = [];
45
+ source = {
46
+ remotes = ["https://rubygems.org"];
47
+ sha256 = "1p4l5ycdxfsr8b51gnvlvhq6s21vmx9z4x617003zbqv3bcqmj6x";
48
+ type = "gem";
49
+ };
50
+ version = "2.10.1";
51
+ };
52
+ language_server-protocol = {
53
+ groups = ["default"];
54
+ platforms = [];
55
+ source = {
56
+ remotes = ["https://rubygems.org"];
57
+ sha256 = "0scnz2fvdczdgadvjn0j9d49118aqm3hj66qh8sd2kv6g1j65164";
58
+ type = "gem";
59
+ };
60
+ version = "3.17.0.4";
61
+ };
62
+ lint_roller = {
63
+ groups = ["default"];
64
+ platforms = [];
65
+ source = {
66
+ remotes = ["https://rubygems.org"];
67
+ sha256 = "11yc0d84hsnlvx8cpk4cbj6a4dz9pk0r1k29p0n1fz9acddq831c";
68
+ type = "gem";
69
+ };
70
+ version = "1.1.0";
71
+ };
72
+ parallel = {
73
+ groups = ["default"];
74
+ platforms = [];
75
+ source = {
76
+ remotes = ["https://rubygems.org"];
77
+ sha256 = "1vy7sjs2pgz4i96v5yk9b7aafbffnvq7nn419fgvw55qlavsnsyq";
78
+ type = "gem";
79
+ };
80
+ version = "1.26.3";
81
+ };
82
+ parser = {
83
+ dependencies = ["ast" "racc"];
84
+ groups = ["default"];
85
+ platforms = [];
86
+ source = {
87
+ remotes = ["https://rubygems.org"];
88
+ sha256 = "18dcwrcnddvi8gl3hmbsb2cj1l7afxk2lh3jmhj90l95h1hn3gkx";
89
+ type = "gem";
90
+ };
91
+ version = "3.3.7.1";
92
+ };
93
+ racc = {
94
+ groups = ["default"];
95
+ platforms = [];
96
+ source = {
97
+ remotes = ["https://rubygems.org"];
98
+ sha256 = "0byn0c9nkahsl93y9ln5bysq4j31q8xkf2ws42swighxd4lnjzsa";
99
+ type = "gem";
100
+ };
101
+ version = "1.8.1";
102
+ };
103
+ rainbow = {
104
+ groups = ["default"];
105
+ platforms = [];
106
+ source = {
107
+ remotes = ["https://rubygems.org"];
108
+ sha256 = "0smwg4mii0fm38pyb5fddbmrdpifwv22zv3d3px2xx497am93503";
109
+ type = "gem";
110
+ };
111
+ version = "3.1.1";
112
+ };
113
+ rake = {
114
+ groups = ["default"];
115
+ platforms = [];
116
+ source = {
117
+ remotes = ["https://rubygems.org"];
118
+ sha256 = "17850wcwkgi30p7yqh60960ypn7yibacjjha0av78zaxwvd3ijs6";
119
+ type = "gem";
120
+ };
121
+ version = "13.2.1";
122
+ };
123
+ regexp_parser = {
124
+ groups = ["default"];
125
+ platforms = [];
126
+ source = {
127
+ remotes = ["https://rubygems.org"];
128
+ sha256 = "0qccah61pjvzyyg6mrp27w27dlv6vxlbznzipxjcswl7x3fhsvyb";
129
+ type = "gem";
130
+ };
131
+ version = "2.10.0";
132
+ };
133
+ rspec = {
134
+ dependencies = ["rspec-core" "rspec-expectations" "rspec-mocks"];
135
+ groups = ["default"];
136
+ platforms = [];
137
+ source = {
138
+ remotes = ["https://rubygems.org"];
139
+ sha256 = "14xrp8vq6i9zx37vh0yp4h9m0anx9paw200l1r5ad9fmq559346l";
140
+ type = "gem";
141
+ };
142
+ version = "3.13.0";
143
+ };
144
+ rspec-core = {
145
+ dependencies = ["rspec-support"];
146
+ groups = ["default"];
147
+ platforms = [];
148
+ source = {
149
+ remotes = ["https://rubygems.org"];
150
+ sha256 = "1r6zbis0hhbik1ck8kh58qb37d1qwij1x1d2fy4jxkzryh3na4r5";
151
+ type = "gem";
152
+ };
153
+ version = "3.13.3";
154
+ };
155
+ rspec-expectations = {
156
+ dependencies = ["diff-lcs" "rspec-support"];
157
+ groups = ["default"];
158
+ platforms = [];
159
+ source = {
160
+ remotes = ["https://rubygems.org"];
161
+ sha256 = "0n3cyrhsa75x5wwvskrrqk56jbjgdi2q1zx0irllf0chkgsmlsqf";
162
+ type = "gem";
163
+ };
164
+ version = "3.13.3";
165
+ };
166
+ rspec-mocks = {
167
+ dependencies = ["diff-lcs" "rspec-support"];
168
+ groups = ["default"];
169
+ platforms = [];
170
+ source = {
171
+ remotes = ["https://rubygems.org"];
172
+ sha256 = "1vxxkb2sf2b36d8ca2nq84kjf85fz4x7wqcvb8r6a5hfxxfk69r3";
173
+ type = "gem";
174
+ };
175
+ version = "3.13.2";
176
+ };
177
+ rspec-support = {
178
+ groups = ["default"];
179
+ platforms = [];
180
+ source = {
181
+ remotes = ["https://rubygems.org"];
182
+ sha256 = "1v6v6xvxcpkrrsrv7v1xgf7sl0d71vcfz1cnrjflpf6r7x3a58yf";
183
+ type = "gem";
184
+ };
185
+ version = "3.13.2";
186
+ };
187
+ rubocop = {
188
+ dependencies = ["json" "language_server-protocol" "parallel" "parser" "rainbow" "regexp_parser" "rubocop-ast" "ruby-progressbar" "unicode-display_width"];
189
+ groups = ["default"];
190
+ platforms = [];
191
+ source = {
192
+ remotes = ["https://rubygems.org"];
193
+ sha256 = "0zzrsdz23jmjc8iwilzhw5cifn4flzmsbwkzxlwa6qf6m80payws";
194
+ type = "gem";
195
+ };
196
+ version = "1.71.2";
197
+ };
198
+ rubocop-ast = {
199
+ dependencies = ["parser"];
200
+ groups = ["default"];
201
+ platforms = [];
202
+ source = {
203
+ remotes = ["https://rubygems.org"];
204
+ sha256 = "1zjpv3kw4ciwk0dh43zj17ws318vnirby1clmcy6j9mvr4mbxv40";
205
+ type = "gem";
206
+ };
207
+ version = "1.38.1";
208
+ };
209
+ rubocop-performance = {
210
+ dependencies = ["rubocop" "rubocop-ast"];
211
+ groups = ["default"];
212
+ platforms = [];
213
+ source = {
214
+ remotes = ["https://rubygems.org"];
215
+ sha256 = "10hv0lz54q34dlwx6vld0qx1fjskfb0nyb5c18cadrpmjnkqcbzj";
216
+ type = "gem";
217
+ };
218
+ version = "1.23.1";
219
+ };
220
+ ruby-progressbar = {
221
+ groups = ["default"];
222
+ platforms = [];
223
+ source = {
224
+ remotes = ["https://rubygems.org"];
225
+ sha256 = "0cwvyb7j47m7wihpfaq7rc47zwwx9k4v7iqd9s1xch5nm53rrz40";
226
+ type = "gem";
227
+ };
228
+ version = "1.13.0";
229
+ };
230
+ standard = {
231
+ dependencies = ["language_server-protocol" "lint_roller" "rubocop" "standard-custom" "standard-performance"];
232
+ groups = ["default"];
233
+ platforms = [];
234
+ source = {
235
+ remotes = ["https://rubygems.org"];
236
+ sha256 = "13ijzq7r0v0rm1yyba1jfw2s9r3kfxljwypfhzpnkrsag64kk2b5";
237
+ type = "gem";
238
+ };
239
+ version = "1.45.0";
240
+ };
241
+ standard-custom = {
242
+ dependencies = ["lint_roller" "rubocop"];
243
+ groups = ["default"];
244
+ platforms = [];
245
+ source = {
246
+ remotes = ["https://rubygems.org"];
247
+ sha256 = "0av55ai0nv23z5mhrwj1clmxpgyngk7vk6rh58d4y1ws2y2dqjj2";
248
+ type = "gem";
249
+ };
250
+ version = "1.0.2";
251
+ };
252
+ standard-performance = {
253
+ dependencies = ["lint_roller" "rubocop-performance"];
254
+ groups = ["default"];
255
+ platforms = [];
256
+ source = {
257
+ remotes = ["https://rubygems.org"];
258
+ sha256 = "1x298w3wmq8cavbsg903wc3arxp3xh2x8263brvy128436m732rd";
259
+ type = "gem";
260
+ };
261
+ version = "1.6.0";
262
+ };
263
+ unicode-display_width = {
264
+ dependencies = ["unicode-emoji"];
265
+ groups = ["default"];
266
+ platforms = [];
267
+ source = {
268
+ remotes = ["https://rubygems.org"];
269
+ sha256 = "1has87asspm6m9wgqas8ghhhwyf2i1yqrqgrkv47xw7jq3qjmbwc";
270
+ type = "gem";
271
+ };
272
+ version = "3.1.4";
273
+ };
274
+ unicode-emoji = {
275
+ groups = ["default"];
276
+ platforms = [];
277
+ source = {
278
+ remotes = ["https://rubygems.org"];
279
+ sha256 = "0ajk6rngypm3chvl6r0vwv36q1931fjqaqhjjya81rakygvlwb1c";
280
+ type = "gem";
281
+ };
282
+ version = "4.0.4";
283
+ };
284
+ }
@@ -0,0 +1,199 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "concurrent/map"
4
+
5
+ class Object
6
+ # An object is blank if it's false, empty, or a whitespace string.
7
+ # For example, +nil+, '', ' ', [], {}, and +false+ are all blank.
8
+ #
9
+ # This simplifies
10
+ #
11
+ # !address || address.empty?
12
+ #
13
+ # to
14
+ #
15
+ # address.blank?
16
+ #
17
+ # @return [true, false]
18
+ def blank?
19
+ respond_to?(:empty?) ? !!empty? : false
20
+ end
21
+
22
+ # An object is present if it's not blank.
23
+ #
24
+ # @return [true, false]
25
+ def present?
26
+ !blank?
27
+ end
28
+
29
+ # Returns the receiver if it's present otherwise returns +nil+.
30
+ # <tt>object.presence</tt> is equivalent to
31
+ #
32
+ # object.present? ? object : nil
33
+ #
34
+ # For example, something like
35
+ #
36
+ # state = params[:state] if params[:state].present?
37
+ # country = params[:country] if params[:country].present?
38
+ # region = state || country || 'US'
39
+ #
40
+ # becomes
41
+ #
42
+ # region = params[:state].presence || params[:country].presence || 'US'
43
+ #
44
+ # @return [Object]
45
+ def presence
46
+ self if present?
47
+ end
48
+ end
49
+
50
+ class NilClass
51
+ # +nil+ is blank:
52
+ #
53
+ # nil.blank? # => true
54
+ #
55
+ # @return [true]
56
+ def blank?
57
+ true
58
+ end
59
+
60
+ def present? # :nodoc:
61
+ false
62
+ end
63
+ end
64
+
65
+ class FalseClass
66
+ # +false+ is blank:
67
+ #
68
+ # false.blank? # => true
69
+ #
70
+ # @return [true]
71
+ def blank?
72
+ true
73
+ end
74
+
75
+ def present? # :nodoc:
76
+ false
77
+ end
78
+ end
79
+
80
+ class TrueClass
81
+ # +true+ is not blank:
82
+ #
83
+ # true.blank? # => false
84
+ #
85
+ # @return [false]
86
+ def blank?
87
+ false
88
+ end
89
+
90
+ def present? # :nodoc:
91
+ true
92
+ end
93
+ end
94
+
95
+ class Array
96
+ # An array is blank if it's empty:
97
+ #
98
+ # [].blank? # => true
99
+ # [1,2,3].blank? # => false
100
+ #
101
+ # @return [true, false]
102
+ alias_method :blank?, :empty?
103
+
104
+ def present? # :nodoc:
105
+ !empty?
106
+ end
107
+ end
108
+
109
+ class Hash
110
+ # A hash is blank if it's empty:
111
+ #
112
+ # {}.blank? # => true
113
+ # { key: 'value' }.blank? # => false
114
+ #
115
+ # @return [true, false]
116
+ alias_method :blank?, :empty?
117
+
118
+ def present? # :nodoc:
119
+ !empty?
120
+ end
121
+ end
122
+
123
+ class Symbol
124
+ # A Symbol is blank if it's empty:
125
+ #
126
+ # :''.blank? # => true
127
+ # :symbol.blank? # => false
128
+ alias_method :blank?, :empty?
129
+
130
+ def present? # :nodoc:
131
+ !empty?
132
+ end
133
+ end
134
+
135
+ class String
136
+ BLANK_RE = /\A[[:space:]]*\z/
137
+ ENCODED_BLANKS = Concurrent::Map.new do |h, enc|
138
+ h[enc] = Regexp.new(BLANK_RE.source.encode(enc), BLANK_RE.options | Regexp::FIXEDENCODING)
139
+ end
140
+
141
+ # A string is blank if it's empty or contains whitespaces only:
142
+ #
143
+ # ''.blank? # => true
144
+ # ' '.blank? # => true
145
+ # "\t\n\r".blank? # => true
146
+ # ' blah '.blank? # => false
147
+ #
148
+ # Unicode whitespace is supported:
149
+ #
150
+ # "\u00a0".blank? # => true
151
+ #
152
+ # @return [true, false]
153
+ def blank?
154
+ # The regexp that matches blank strings is expensive. For the case of empty
155
+ # strings we can speed up this method (~3.5x) with an empty? call. The
156
+ # penalty for the rest of strings is marginal.
157
+ empty? ||
158
+ begin
159
+ BLANK_RE.match?(self)
160
+ rescue Encoding::CompatibilityError
161
+ ENCODED_BLANKS[encoding].match?(self)
162
+ end
163
+ end
164
+
165
+ def present? # :nodoc:
166
+ !blank?
167
+ end
168
+ end
169
+
170
+ class Numeric # :nodoc:
171
+ # No number is blank:
172
+ #
173
+ # 1.blank? # => false
174
+ # 0.blank? # => false
175
+ #
176
+ # @return [false]
177
+ def blank?
178
+ false
179
+ end
180
+
181
+ def present?
182
+ true
183
+ end
184
+ end
185
+
186
+ class Time # :nodoc:
187
+ # No Time is blank:
188
+ #
189
+ # Time.now.blank? # => false
190
+ #
191
+ # @return [false]
192
+ def blank?
193
+ false
194
+ end
195
+
196
+ def present?
197
+ true
198
+ end
199
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Errgonomic
4
+ VERSION = "0.1.0"
5
+ end
data/lib/errgonomic.rb ADDED
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "errgonomic/version"
4
+
5
+ # The semantics here borrow heavily from ActiveSupport. Let's prefer that if
6
+ # loaded, otherwise just copypasta the bits we like. Or convince me to make that
7
+ # gem a dependency.
8
+ if !Object.methods.include?(:blank?)
9
+ require_relative "errgonomic/core_ext/blank"
10
+ end
11
+
12
+ module Errgonomic
13
+ class Error < StandardError; end
14
+
15
+ class NotPresentError < Error; end
16
+
17
+ class TypeMismatchError < Error; end
18
+ end
19
+
20
+ class Object
21
+ # Returns the receiver if it is present, otherwise raises a NotPresentError.
22
+ # This method is useful to enforce strong expectations, where it is preferable
23
+ # to fail early rather than risk causing an ambiguous error somewhere else.
24
+ #
25
+ # @param message [String] The error message to raise if the receiver is not present.
26
+ # @return [Object] The receiver if it is present, otherwise raises a NotPresentError.
27
+ def present_or_raise(message)
28
+ raise Errgonomic::NotPresentError, message if blank?
29
+ self
30
+ end
31
+
32
+ # Returns the receiver if it is present, otherwise returns the given value. If
33
+ # constructing the default value is expensive, consider using
34
+ # +present_or_else+.
35
+ #
36
+ # @param value [Object] The value to return if the receiver is not present.
37
+ # @return [Object] The receiver if it is present, otherwise the given value.
38
+ def present_or(value)
39
+ # TBD whether this is *too* strict
40
+ if value.class != self.class && self.class != NilClass
41
+ raise Errgonomic::TypeMismatchError, "Type mismatch: default value is a #{value.class} but original was a #{self.class}"
42
+ end
43
+
44
+ return self if present?
45
+
46
+ value
47
+ end
48
+
49
+ # Returns the receiver if it is present, otherwise returns the result of the
50
+ # block. Invoking a block may be preferable to returning a default value with
51
+ # +present_or+, if constructing the default value is expensive.
52
+ #
53
+ # @param block [Proc] The block to call if the receiver is not present.
54
+ # @return [Object] The receiver if it is present, otherwise the result of the block.
55
+ def present_or_else(&block)
56
+ return block.call if blank?
57
+ self
58
+ end
59
+ end
@@ -0,0 +1,4 @@
1
+ module Errgonomic
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: errgonomic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nick Zadrozny
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 1980-01-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: concurrent-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ description: Let's blend the Rails 'present' and 'blank' conventions with a few patterns
28
+ from Rust Option types.
29
+ email:
30
+ - nick@onemorecloud.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".envrc"
36
+ - ".rspec"
37
+ - ".standard.yml"
38
+ - CHANGELOG.md
39
+ - CODE_OF_CONDUCT.md
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - flake.lock
44
+ - flake.nix
45
+ - gemset.nix
46
+ - lib/errgonomic.rb
47
+ - lib/errgonomic/core_ext/blank.rb
48
+ - lib/errgonomic/version.rb
49
+ - sig/errgonomic.rbs
50
+ homepage: https://omc.io/
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ homepage_uri: https://omc.io/
55
+ source_code_uri: https://github.com/omc/errgonomic
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 3.0.0
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubygems_version: 3.5.22
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: Opinionated, ergonomic error handling for Ruby, inspired by Rails and Rust.
75
+ test_files: []