errgonomic 0.5.0 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a07fce97adead2a0d6b77c83f0c80c06d6ecb6633193f61454d961bea1a4acf2
4
- data.tar.gz: c6c66f5a03d8bea908b6b68cbe76c69f724012313348c3563bade4932946acc8
3
+ metadata.gz: 4dc41a36e372dc0af60cb4282d28c3fd92575c1b6999a41222e4be9623ead934
4
+ data.tar.gz: 6d5b368f9eabb77bfc1b3e459d18e29b6767681c08e83a77faf69df0bee7dcc0
5
5
  SHA512:
6
- metadata.gz: 8d6d7dbac95eeea0d1fcb1bade820a2c7ba83e464dda0d79052a7beee9f9a67bc4c53c1b87b38256189ffd8f8d6bf0e8f40dc2055db1395f2bc227c30a2075bf
7
- data.tar.gz: 522f8e75d45b543d1d8762be130d6a625dac1b8ac47a7b3eae5729649afedd8d0e493ecbb885b1aef42a887fc6a4a8ab4461ab0fd615d40d413b91da93da6ebf
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. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
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 'rspec/core/rake_task'
6
- RSpec::Core::RakeTask.new(:spec)
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[spec yard:doctest]
17
+ task default: %i[test yard:doctest]
data/gemset.nix CHANGED
@@ -269,7 +269,7 @@
269
269
  path = ./.;
270
270
  type = "path";
271
271
  };
272
- version = "0.5.0";
272
+ version = "0.6.0";
273
273
  };
274
274
  erubi = {
275
275
  groups = ["default" "development"];
@@ -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 and return its value. Block most return an Option.
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(3) } # => Some(3)
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
 
@@ -15,12 +15,9 @@ module Errgonomic
15
15
  ActiveRecord::Base.include(Errgonomic::Rails::ActiveRecordDelegateOptional)
16
16
  end
17
17
 
18
- # Wrapping optional associations requires that we include the module after
19
- # the class is first evaluated, so that it can define its associations for
20
- # later reflection.
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
@@ -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(self)
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
- @value = block.call(value)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Errgonomic
4
- VERSION = '0.5.0'
4
+ VERSION = '0.6.0'
5
5
  end
data/lib/errgonomic.rb CHANGED
@@ -27,10 +27,12 @@ module Errgonomic
27
27
 
28
28
  class NotPresentError < Error; end
29
29
 
30
- class TypeMismatchError < TypeError; end
30
+ class TypeMismatchError < Error; end
31
31
 
32
32
  class UnwrapError < Error
33
- def initialize(msg, value)
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
- @give_me_ambiguous_downstream_errors || true
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,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: errgonomic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Zadrozny