errgonomic 0.5.1 → 0.6.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 +4 -4
- data/README.md +15 -1
- data/Rakefile +6 -3
- data/gemset.nix +1 -1
- data/lib/errgonomic/option.rb +7 -6
- data/lib/errgonomic/rails.rb +3 -6
- data/lib/errgonomic/result.rb +8 -11
- data/lib/errgonomic/version.rb +1 -1
- data/lib/errgonomic.rb +5 -2
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4dc41a36e372dc0af60cb4282d28c3fd92575c1b6999a41222e4be9623ead934
|
|
4
|
+
data.tar.gz: 6d5b368f9eabb77bfc1b3e459d18e29b6767681c08e83a77faf69df0bee7dcc0
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e9e9b29acfc45cbfee45957b864c2ca0498765bd37a2066cd516a012a2397c6b6962695f5ff8844d63aeeaf177014cb16235559153d0d107557f0e4c4e35f26d
|
|
7
|
+
data.tar.gz: 1484855cc2d7b22649679924958d34694e9e7726969d179147877ebaf31a34089e83c55881b330f3b63bfa8edf1a52f0598da5cc2439b3bf7795a9e96e97580d
|
data/README.md
CHANGED
|
@@ -55,7 +55,21 @@ TODO: Write usage instructions here
|
|
|
55
55
|
|
|
56
56
|
## Development
|
|
57
57
|
|
|
58
|
-
After checking out the repo, run `bin/setup` to install dependencies.
|
|
58
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
59
|
+
|
|
60
|
+
This project encourages **red, green, refactor** when making changes. First, add or change a test that captures the desired behavior; next, run the tests to observe the failure message, confirming the test is useful; next, make the smallest code change(s) to make the test pass. Once tests pass, review your diff and look for opportunities to simplify or improve abstractions; make changes and iterate, running tests on each change to guard against regressions.
|
|
61
|
+
|
|
62
|
+
Run the doctest suite with:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
nix develop -c yard doctest
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Run all tests with:
|
|
69
|
+
|
|
70
|
+
```
|
|
71
|
+
nix develop -c rake
|
|
72
|
+
```
|
|
59
73
|
|
|
60
74
|
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
75
|
|
data/Rakefile
CHANGED
|
@@ -2,8 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
require 'bundler/gem_tasks'
|
|
4
4
|
|
|
5
|
-
require '
|
|
6
|
-
|
|
5
|
+
require 'rake/testtask'
|
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
|
7
|
+
t.libs << 'test'
|
|
8
|
+
t.test_files = FileList['test/**/*_test.rb']
|
|
9
|
+
end
|
|
7
10
|
|
|
8
11
|
require 'yard/doctest/rake'
|
|
9
12
|
YARD::Doctest::RakeTask.new do |task|
|
|
@@ -11,4 +14,4 @@ YARD::Doctest::RakeTask.new do |task|
|
|
|
11
14
|
task.pattern = 'lib/**/*.rb'
|
|
12
15
|
end
|
|
13
16
|
|
|
14
|
-
task default: %i[
|
|
17
|
+
task default: %i[test yard:doctest]
|
data/gemset.nix
CHANGED
data/lib/errgonomic/option.rb
CHANGED
|
@@ -257,7 +257,7 @@ module Errgonomic
|
|
|
257
257
|
return self if some?
|
|
258
258
|
|
|
259
259
|
val = block.call
|
|
260
|
-
if !val.is_a?(Errgonomic::Option::Any) && Errgonomic.give_me_ambiguous_downstream_errors?
|
|
260
|
+
if !val.is_a?(Errgonomic::Option::Any) && !Errgonomic.give_me_ambiguous_downstream_errors?
|
|
261
261
|
raise Errgonomic::ArgumentError.new, "block must return an Option, was #{val.class.name}"
|
|
262
262
|
end
|
|
263
263
|
|
|
@@ -275,16 +275,17 @@ module Errgonomic
|
|
|
275
275
|
other
|
|
276
276
|
end
|
|
277
277
|
|
|
278
|
-
# If self is Some, call the given block
|
|
278
|
+
# If self is Some, call the given block with the inner value and return
|
|
279
|
+
# its result. Block must return an Option.
|
|
279
280
|
#
|
|
280
281
|
# @example
|
|
281
|
-
# None().and_then { Some(1) } # => None()
|
|
282
|
-
# Some(2).and_then { Some(
|
|
282
|
+
# None().and_then { |x| Some(x + 1) } # => None()
|
|
283
|
+
# Some(2).and_then { |x| Some(x + 1) } # => Some(3)
|
|
283
284
|
def and_then(&block)
|
|
284
285
|
return self if none?
|
|
285
286
|
|
|
286
|
-
val = block.call
|
|
287
|
-
if Errgonomic.give_me_ambiguous_downstream_errors? && !val.is_a?(Errgonomic::Option::Any)
|
|
287
|
+
val = block.call(value)
|
|
288
|
+
if !Errgonomic.give_me_ambiguous_downstream_errors? && !val.is_a?(Errgonomic::Option::Any)
|
|
288
289
|
raise Errgonomic::ArgumentError.new, "block must return an Option, was #{val.class.name}"
|
|
289
290
|
end
|
|
290
291
|
|
data/lib/errgonomic/rails.rb
CHANGED
|
@@ -15,12 +15,9 @@ module Errgonomic
|
|
|
15
15
|
ActiveRecord::Base.include(Errgonomic::Rails::ActiveRecordDelegateOptional)
|
|
16
16
|
end
|
|
17
17
|
|
|
18
|
-
#
|
|
19
|
-
#
|
|
20
|
-
|
|
21
|
-
def self.setup_after
|
|
22
|
-
# todo
|
|
23
|
-
end
|
|
18
|
+
# Models opt in to ActiveRecordOptional individually via
|
|
19
|
+
# `include Errgonomic::Rails::ActiveRecordOptional`.
|
|
20
|
+
def self.setup_after; end
|
|
24
21
|
end
|
|
25
22
|
|
|
26
23
|
# Hook into Rails with a Railtie
|
data/lib/errgonomic/result.rb
CHANGED
|
@@ -142,7 +142,7 @@ module Errgonomic
|
|
|
142
142
|
return self if err?
|
|
143
143
|
|
|
144
144
|
res = block.call(value)
|
|
145
|
-
if !res.is_a?(Errgonomic::Result::Any) && Errgonomic.give_me_ambiguous_downstream_errors?
|
|
145
|
+
if !res.is_a?(Errgonomic::Result::Any) && !Errgonomic.give_me_ambiguous_downstream_errors?
|
|
146
146
|
raise Errgonomic::ArgumentError, 'and_then block must return a Result'
|
|
147
147
|
end
|
|
148
148
|
|
|
@@ -173,20 +173,18 @@ module Errgonomic
|
|
|
173
173
|
# Sorry about that, hopefully it helps your tests. Better than ambiguous
|
|
174
174
|
# downstream "undefined method" errors, probably.
|
|
175
175
|
#
|
|
176
|
-
# TODO yield the Err
|
|
177
|
-
#
|
|
178
176
|
# @param block [Proc]
|
|
179
177
|
#
|
|
180
178
|
# @example
|
|
181
|
-
# Ok(1).or_else { Ok(2) } # => Ok(1)
|
|
182
|
-
# Err(:o).or_else { Ok(1) } # => Ok(1)
|
|
183
|
-
# Err(:q).or_else { Err(:r) } # => Err(:r)
|
|
184
|
-
# Err(:s).or_else { "oops" } # => raise Errgonomic::ArgumentError, "or_else block must return a Result"
|
|
179
|
+
# Ok(1).or_else { |e| Ok(2) } # => Ok(1)
|
|
180
|
+
# Err(:o).or_else { |e| Ok(1) } # => Ok(1)
|
|
181
|
+
# Err(:q).or_else { |e| Err(:r) } # => Err(:r)
|
|
182
|
+
# Err(:s).or_else { |e| "oops" } # => raise Errgonomic::ArgumentError, "or_else block must return a Result"
|
|
185
183
|
def or_else(&block)
|
|
186
184
|
return self if ok?
|
|
187
185
|
|
|
188
|
-
res = block.call(
|
|
189
|
-
if !res.is_a?(Errgonomic::Result::Any) && Errgonomic.give_me_ambiguous_downstream_errors?
|
|
186
|
+
res = block.call(value)
|
|
187
|
+
if !res.is_a?(Errgonomic::Result::Any) && !Errgonomic.give_me_ambiguous_downstream_errors?
|
|
190
188
|
raise Errgonomic::ArgumentError, 'or_else block must return a Result'
|
|
191
189
|
end
|
|
192
190
|
|
|
@@ -249,8 +247,7 @@ module Errgonomic
|
|
|
249
247
|
def map(&block)
|
|
250
248
|
return self if err?
|
|
251
249
|
|
|
252
|
-
|
|
253
|
-
self
|
|
250
|
+
Ok(block.call(value))
|
|
254
251
|
end
|
|
255
252
|
|
|
256
253
|
# Refuse to serialize an unwrapped Result as a String. Results must be
|
data/lib/errgonomic/version.rb
CHANGED
data/lib/errgonomic.rb
CHANGED
|
@@ -30,7 +30,9 @@ module Errgonomic
|
|
|
30
30
|
class TypeMismatchError < Error; end
|
|
31
31
|
|
|
32
32
|
class UnwrapError < Error
|
|
33
|
-
|
|
33
|
+
attr_reader :value
|
|
34
|
+
|
|
35
|
+
def initialize(msg, value = nil)
|
|
34
36
|
super(msg)
|
|
35
37
|
@value = value
|
|
36
38
|
end
|
|
@@ -47,8 +49,9 @@ module Errgonomic
|
|
|
47
49
|
class SerializeError < TypeError; end
|
|
48
50
|
|
|
49
51
|
# A little bit of control over how pedantic we are in our runtime type checks.
|
|
52
|
+
# Default is false: we are pedantic and raise errors on type mismatches.
|
|
50
53
|
def self.give_me_ambiguous_downstream_errors?
|
|
51
|
-
|
|
54
|
+
!!@give_me_ambiguous_downstream_errors
|
|
52
55
|
end
|
|
53
56
|
|
|
54
57
|
# You can opt out of the pedantic runtime checks for lazy block evaluation,
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: errgonomic
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.6.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Nick Zadrozny
|
|
8
8
|
bindir: exe
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date: 1980-01-
|
|
10
|
+
date: 1980-01-01 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: concurrent-ruby
|
|
@@ -104,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
104
104
|
- !ruby/object:Gem::Version
|
|
105
105
|
version: '0'
|
|
106
106
|
requirements: []
|
|
107
|
-
rubygems_version: 3.
|
|
107
|
+
rubygems_version: 3.6.6
|
|
108
108
|
specification_version: 4
|
|
109
109
|
summary: Opinionated, ergonomic error handling for Ruby, inspired by Rails and Rust.
|
|
110
110
|
test_files: []
|