mona-result 0.1.0 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 170acae8e3dab4e7c9dec3b88b17cb8668a678290aafa5eb363da54261a5eab9
4
- data.tar.gz: 0b327151acf8487019f87f5b859b1f940d766e67c84479c29ef1a70a4d26aeb1
3
+ metadata.gz: af436f63f473104b9dd2b4bffb0c379e15032c5eee00fbd7b76491f8d7dc3ba0
4
+ data.tar.gz: 0a198685c28efc6a3d72b87daf6563fb5ca6876e1990063b659a311ba5bd91a3
5
5
  SHA512:
6
- metadata.gz: 45ca05476a2621a4d6e76a190b37852880b9c69d43470ec77ed5dc9dabf7ec9f6cbe9771777adc369f69ce421e8297f2e5c5ee66df8bf43b12c62d1e0ce3c53d
7
- data.tar.gz: 59684cbea28ce2392a1755edb70c9756786b1bf730900e5aaf0829a3a4a068a0742c6db907e3720d4533ff17ff57a2c7b5a72e7dd5d1364c4acfd4f6a4bafd80
6
+ metadata.gz: 545a03866806626c22141209e8e5a5b828f5a686cfa54d40a90a3015b5efa3ea6ba404fdb77b2b0869fec30b2f8e035e551b8d836601710324ef3f3cccab5e6a
7
+ data.tar.gz: 6886883d884629a9de5fe769016fbb0b44c5a107e6f49bc1c6688c7fd6efab2800da4d47c252985ca8676d7f9d69ae87fbb80e6faeda001c2385447137d6bbef
data/.rubocop.yml CHANGED
@@ -1,3 +1,7 @@
1
+ require:
2
+ - rubocop-minitest
3
+ - rubocop-rake
4
+
1
5
  AllCops:
2
6
  TargetRubyVersion: 3.1
3
7
  NewCops: enable
data/CHANGELOG.md CHANGED
@@ -1,4 +1,15 @@
1
- ## [Unreleased]
1
+ ## [0.1.3] - 2022-08-09
2
+
3
+ - Fix CHANGELOG rubygems link
4
+
5
+ ## [0.1.2] - 2022-08-09
6
+
7
+ - Fix gemspec links
8
+ - Improve documentation
9
+
10
+ ## [0.1.1] - 2022-08-09
11
+
12
+ - Adds basic documentation
2
13
 
3
14
  ## [0.1.0] - 2022-08-04
4
15
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mona-result (0.1.0)
4
+ mona-result (0.1.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -33,14 +33,14 @@ GEM
33
33
  rbs (2.6.0)
34
34
  regexp_parser (2.5.0)
35
35
  rexml (3.2.5)
36
- rubocop (1.33.0)
36
+ rubocop (1.34.0)
37
37
  json (~> 2.3)
38
38
  parallel (~> 1.10)
39
- parser (>= 3.1.0.0)
39
+ parser (>= 3.1.2.1)
40
40
  rainbow (>= 2.2.2, < 4.0)
41
41
  regexp_parser (>= 1.8, < 3.0)
42
42
  rexml (>= 3.2.5, < 4.0)
43
- rubocop-ast (>= 1.19.1, < 2.0)
43
+ rubocop-ast (>= 1.20.0, < 2.0)
44
44
  ruby-progressbar (~> 1.7)
45
45
  unicode-display_width (>= 1.4.0, < 3.0)
46
46
  rubocop-ast (1.21.0)
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # Mona::Result
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mona/result`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ Provides a result monad, with OK holding a value, and Err holding a failure, with optional reason, and metadata.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ Also provides a dict result monad, a hash-like result, which provides `#sequence` whose semantics mirror _do notation_
6
6
 
7
7
  ## Installation
8
8
 
@@ -16,7 +16,86 @@ If bundler is not being used to manage dependencies, install the gem by executin
16
16
 
17
17
  ## Usage
18
18
 
19
- TODO: Write usage instructions here
19
+ ```ruby
20
+ Result = Mona::Result
21
+
22
+ # basic OK
23
+ success = Result.ok(1) # => #<OK 1>
24
+ success.ok? # => true
25
+ success.err? # => false
26
+ success.value # => 1
27
+ success.and_tap { puts "it is #{_1}" }.and_then { _1 * 5 }.value_or { :nope }
28
+ # OUT: it is 1
29
+ # => 5
30
+
31
+ # basic Err
32
+ failure = Result.err(4, :not_prime) # => #<Err 4 reason: :not_prime>
33
+ failure.ok? # => false
34
+ failure.err? # => true
35
+ failure.failure # => 4
36
+ failure.reason # => :not_prime
37
+ failure.and_tap { puts "it is #{_1}" }.and_then { _1 * 5 }.value_or { :nope } # => :nope
38
+
39
+ # Dict with sequence, example using a fictional repository.
40
+ # #set can take a [Symbol, Symbol] key argument where left is OK key, right is Err key
41
+ result = Result.dict do |d|
42
+ d.set :user, UserRepo.find user_id
43
+ d.set :input, PostInput.valid(attributes)
44
+ d.set [:post, :input], PostRepo.create user_id: user_id, **d[:input]
45
+ end
46
+ # if find user fails: # => #<Err {:user=>NotFoundError} reason: :not_found, key: :user>
47
+ # if validation fails: # => #<Err {:user=>User, :input=>PostInput(invalid)} reason: :invalid, key: :input>
48
+ # if create fails: # => #<Err {:user=>User, :input=>PostInput(db err)} reason: :db_err, key: :input>
49
+ # if all ok: # => #<OK: {:user=>User, :post=>Post}>
50
+
51
+ # Action - a callable sequence
52
+ divide = Result.action do |x, y|
53
+ set :numerator, x
54
+ set :divisor, y.zero? ? Result.err(y, :is_zero) : y
55
+ puts "ready to calculate answer"
56
+ set :answer, numerator / divisor
57
+ puts "answer was: #{answer}"
58
+ end
59
+
60
+ divide.call(12, 4)
61
+ # OUT: ready to calculate answer
62
+ # OUT: answer was: 3
63
+ # => #<OK {:numerator=>12, :divisor=>4, :answer=>3}>
64
+
65
+ result = divide.call(12, 0) # => #<Err {:numerator=>12, :divisor=>0} reason: :is_zero, key: :divisor>
66
+ result.err? # => true
67
+ result.failure # => {:numerator=>12, :divisor=>0}
68
+ result.reason # => :is_zero
69
+ result.meta # => {:key=>:divisor}
70
+ ```
71
+
72
+ Pattern matching is supported in the following way:
73
+
74
+ ```ruby
75
+ case result
76
+ in :ok, value
77
+ in :err, failure, reason, { **meta }
78
+ end
79
+
80
+ case result
81
+ in ok:
82
+ in err:, reason:, **meta
83
+ end
84
+ ```
85
+
86
+ You can also match a result, using similar semantics, the difference from using case/pattern-match is that a
87
+ `Mona::Result::NoMatchError` is raised, which keeps a reference to the result (which is useful for differentially
88
+ handling errors in an enclosing scope - not currently possible with ruby's `NoMatchingPatternError` which does not
89
+ contain any information about the failed match)
90
+
91
+ ```ruby
92
+ Mona::Result.match(result) do |on|
93
+ on.ok { |value| puts "ok" }
94
+ on.err(key: :x) { |failure, reason, **meta| puts "error with meta :key => :x" }
95
+ on.err(:invalid) { |failure, reason, **meta| puts "error with reason :invalid" }
96
+ on.err { |failure, reason, **meta| puts "error" }
97
+ end
98
+ ```
20
99
 
21
100
  ## Development
22
101
 
@@ -26,7 +105,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
26
105
 
27
106
  ## Contributing
28
107
 
29
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mona-result.
108
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mona-rb/mona-result.
30
109
 
31
110
  ## License
32
111
 
data/Rakefile CHANGED
@@ -3,6 +3,7 @@
3
3
  require "bundler/gem_tasks"
4
4
  require "rake/testtask"
5
5
 
6
+ desc "Run steep check"
6
7
  task :steep do
7
8
  system "steep check" or raise "Steep type checking failed"
8
9
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Mona
4
4
  module Result
5
- VERSION = "0.1.0"
5
+ VERSION = "0.1.3"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mona-result
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian White
@@ -10,7 +10,7 @@ bindir: exe
10
10
  cert_chain: []
11
11
  date: 2022-08-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Mona::Result provides a result monad, and dict_result monad
13
+ description: Mona::Result provides a result monad, and dict result monad with do-notation
14
14
  email:
15
15
  - ian.w.white@gmail.com
16
16
  executables: []
@@ -35,14 +35,14 @@ files:
35
35
  - lib/mona/result/sequence.rb
36
36
  - lib/mona/result/version.rb
37
37
  - sig/mona/result.rbs
38
- homepage: https://github.com/mona/mona-result
38
+ homepage: https://github.com/mona-rb/mona-result
39
39
  licenses:
40
40
  - MIT
41
41
  metadata:
42
42
  rubygems_mfa_required: 'true'
43
- homepage_uri: https://github.com/mona/mona-result
44
- source_code_uri: https://github.com/mona/mona-result
45
- changelog_uri: https://github.com/mona/mona-result/CHANGELOG.md
43
+ homepage_uri: https://github.com/mona-rb/mona-result
44
+ source_code_uri: https://github.com/mona-rb/mona-result
45
+ changelog_uri: https://github.com/mona-rb/mona-result/blob/main/CHANGELOG.md
46
46
  post_install_message:
47
47
  rdoc_options: []
48
48
  require_paths:
@@ -61,5 +61,5 @@ requirements: []
61
61
  rubygems_version: 3.3.7
62
62
  signing_key:
63
63
  specification_version: 4
64
- summary: Mona::Result is a result Monad for ruby
64
+ summary: Mona::Result is a result monad for ruby
65
65
  test_files: []