opted 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 89de79662dbd97a2f00ed20c0bcbb98410add58975fd067be4d9943eb48927fd
4
+ data.tar.gz: ebdc40eeb18a5d99abce924a4e51397144eb2ed4745ffed92172d73628e99305
5
+ SHA512:
6
+ metadata.gz: 3c2a9cd8978b300502ea76fe45d752e2e0182671f4f4ccb0480fb4ff58737c9fcf584a9efbc70dcaa3ad46f1dc66b0377d9499de55273d388207802ea1015be5
7
+ data.tar.gz: fc6c388de8e6e521165cdfa3e8e9b5b2349725b07eb70b7d6e54860efbba66cba85b6c22f8daa49ab382fea64f0e18e0a34a7f437b61dcd315ddbf4e5e2a5798
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1 @@
1
+ 2.5.0
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.16.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at mxhold@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in opted.gemspec
6
+ gemspec
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ opted (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.7.0)
12
+ rspec-core (~> 3.7.0)
13
+ rspec-expectations (~> 3.7.0)
14
+ rspec-mocks (~> 3.7.0)
15
+ rspec-core (3.7.0)
16
+ rspec-support (~> 3.7.0)
17
+ rspec-expectations (3.7.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.7.0)
20
+ rspec-mocks (3.7.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.7.0)
23
+ rspec-support (3.7.0)
24
+ yard (0.9.12)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ bundler (~> 1.16)
31
+ opted!
32
+ rake (~> 10.0)
33
+ rspec (~> 3.0)
34
+ yard (~> 0.9)
35
+
36
+ BUNDLED WITH
37
+ 1.16.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Max Holder
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.
@@ -0,0 +1,86 @@
1
+ # Opted
2
+
3
+ Inspired by [Rust](https://www.rust-lang.org/en-US/)'s [Result](https://doc.rust-lang.org/std/result/index.html) type, this gem provides basic value types to represent success (`Ok`) and failure (`Err`) results.
4
+
5
+ **Note**: this is mostly experimental. Since I've only used this for trivial things so far, I suggest using caution before using this anywhere important.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'opted'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ ## Usage
20
+
21
+ <!-- BEGIN CODE SAMPLE -->
22
+ ```ruby
23
+ # This gem doesn't polute the top-level namespace
24
+ # so it can be handy to alias the classes
25
+ Ok = Opted::Result::Ok
26
+ Err = Opted::Result::Err
27
+
28
+ # This code sample is run as a part of this gem's test suite
29
+ def assert(value)
30
+ fail "Invalid assertion in README code sample" unless value
31
+ end
32
+
33
+ assert Ok.new(1).unwrap! == 1
34
+
35
+ begin
36
+ Err.new(:whoops).unwrap!
37
+ rescue Opted::Result::UnwrapError => e
38
+ assert e.message =~ /Called #unwrap! on #<Opted::Result::Err:.* @error=:whoops>/
39
+ else
40
+ fail
41
+ end
42
+
43
+ result = Ok.new(1)
44
+
45
+ unwrapped_result = result.match do
46
+ ok { |value| value + 1 }
47
+ err { |error| fail "unreachable" }
48
+ end
49
+
50
+ assert unwrapped_result == 2
51
+
52
+ # disallows wrapping nil
53
+ begin
54
+ Ok.new(nil)
55
+ rescue ArgumentError => e
56
+ assert e.message =~ /can't wrap nil/
57
+ else
58
+ fail
59
+ end
60
+
61
+ begin
62
+ Err.new(nil)
63
+ rescue ArgumentError => e
64
+ assert e.message =~ /can't wrap nil/
65
+ else
66
+ fail
67
+ end
68
+ ```
69
+
70
+ ## Development
71
+
72
+ 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.
73
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
75
+
76
+ ## Contributing
77
+
78
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/opted. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
79
+
80
+ ## License
81
+
82
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
83
+
84
+ ## Code of Conduct
85
+
86
+ Everyone interacting in the Opted project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/opted/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "opted"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rspec' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 150) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rspec-core", "rspec")
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ require "opted/version"
2
+ require "opted/result"
3
+
4
+ module Opted
5
+ end
@@ -0,0 +1,15 @@
1
+ require "opted/result/ok"
2
+ require "opted/result/err"
3
+ require "opted/result/match"
4
+
5
+ module Opted
6
+ module Result
7
+ private_constant :Match
8
+ # Error raised from calling {Err#unwrap!} or {Ok#unwrap_err!}
9
+ class UnwrapError < RuntimeError
10
+ def initialize(method, caller)
11
+ super("Called ##{method} on #{caller}")
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,159 @@
1
+ module Opted
2
+ module Result
3
+ # @abstract This class exists purely to document the interface of both {Ok}
4
+ # and {Err} result types.
5
+ class AbstractResult
6
+ # If +self+ is {Ok}
7
+ # @example
8
+ # Ok.new(1).ok? # => true
9
+ # Err.new(:err).ok? # => false
10
+ # @return [Boolean]
11
+ def ok?
12
+ end
13
+
14
+ # If +self+ is {Err}
15
+ # @example
16
+ # Ok.new(1).err? # => false
17
+ # Err.new(:err).err? # => true
18
+ # @return [Boolean]
19
+ def err?
20
+ end
21
+
22
+ # Returns the inner value if {Ok}
23
+ # @example
24
+ # Ok.new(1).unwrap! # => 1
25
+ # Err.new(:err).unwrap! # => UnwrapError: Called #unwrap! on #<Opted::Result::Err:0x00007fbec7032798 @error=:err>
26
+ # @raise [UnwrapError] if {Err}
27
+ # @return [Object] value
28
+ def unwrap!
29
+ end
30
+
31
+ # Returns the inner error if {Err}
32
+ # @example
33
+ # Ok.new(1).unwrap_err! # => UnwrapError: Called #unwrap_err! on #<Opted::Result::Ok:0x00007fbec7901c20 @value=1>
34
+ # Err.new(:err).unwrap_err! # => :err
35
+ # @raise [UnwrapError] if {Ok}
36
+ # @return [Object] error
37
+ def unwrap_err!
38
+ end
39
+
40
+ # Returns a result of the same type wrapping the result of applying the
41
+ # block to the original inner value, leaving errors untouched
42
+ # @example
43
+ # Ok.new(1)
44
+ # .map { |value| value + 1 }
45
+ # .unwrap! # => 2
46
+ # Err.new(:err)
47
+ # .map { |value| value + 1 }
48
+ # .unwrap_err! # => :err
49
+ # @yieldparam [Object] value
50
+ # @return [Ok, Err] mapped result
51
+ def map(&block)
52
+ end
53
+
54
+ # Returns a result of the same type wrapping the result of applying the
55
+ # block to the original inner error, leaving values untouched
56
+ # @example
57
+ # Ok.new(1)
58
+ # .map_err { |error| error.upcase }
59
+ # .unwrap! # => 1
60
+ # Err.new(:err)
61
+ # .map_err { |error| error.upcase }
62
+ # .unwrap_err! # => :ERR
63
+ # @yieldparam [Object] error
64
+ # @return [Ok, Err] mapped result
65
+ def map_err(&block)
66
+ end
67
+
68
+ # Returns inner value if {Ok}, other value if {Err}
69
+ # @example
70
+ # Ok.new(1).unwrap_or(2) # => 1
71
+ # Err.new(:err).unwrap_or(2) # => 2
72
+ # @return [Object] inner value or other value
73
+ def unwrap_or(other_value)
74
+ end
75
+
76
+ # Returns other result if {Ok}, +self+ if {Err}
77
+ # @example
78
+ # Ok.new(1).and(Ok.new(2)) # => Ok.new(2)
79
+ # Ok.new(1).and(Err.new(:err)) # => Err.new(:err)
80
+ # Err.new(:e1).and(Ok.new(1)) # => Err.new(:e1)
81
+ # Err.new(:e1).and(Err.new(:e2)) # => Err.new(:e1)
82
+ # @param other [Ok, Err] other result
83
+ # @return [Ok, Err] self or other result
84
+ # @note Arguments provided will be eagerly evaluated. Use {#and_then} for
85
+ # lazy evaluation.
86
+ def and(other)
87
+ end
88
+
89
+ # Returns the result of calling the block with the inner value if {Ok},
90
+ # self if {Err}
91
+ # @yieldparam value [Object] inner value
92
+ # @yieldreturn [Ok, Err] mapped result
93
+ # @return [Ok, Err] self or mapped result
94
+ # @note This differs from {#map} in that it allows you to turn {Ok} into
95
+ # {Err}
96
+ # @example
97
+ # Ok.new(:foo)
98
+ # .and_then { |value| Err.new(value.upcase) }
99
+ # # => Err.new(:FOO)
100
+ # Err.new(:err)
101
+ # .and_then { |value| Err.new(value.upcase) }
102
+ # # => Err.new(:err)
103
+ def and_then(&block)
104
+ end
105
+
106
+ # Returns self if {Ok}, other result if {Err}
107
+ # @example
108
+ # Ok.new(1).or(Ok.new(2)) # => Ok.new(1)
109
+ # Ok.new(1).or(Err.new(:err)) # => Ok.new(1)
110
+ # Err.new(:e1).or(Ok.new(1)) # => Ok.new(:1)
111
+ # Err.new(:e1).or(Err.new(:e2)) # => Err.new(:e2)
112
+ # @param other [Ok, Err] other result
113
+ # @return [Ok, Err] self or other result
114
+ # @note Arguments provided will be eagerly evaluated. Use {#or_else} for
115
+ # lazy evaluation.
116
+ def or(other)
117
+ end
118
+
119
+ # Returns self if {Ok}, the result of calling the block with the inner
120
+ # error if {Err}
121
+ # @yieldparam value [Object] inner error
122
+ # @yieldreturn [Ok, Err] mapped result
123
+ # @return [Ok, Err] self or mapped result
124
+ # @note This differs from {#map_err} in that it allows you to turn {Err}
125
+ # into {Ok}
126
+ # @example
127
+ # Ok.new(:foo)
128
+ # .or_else { |error| Ok.new(error.upcase) }
129
+ # # => Ok.new(:foo)
130
+ # Err.new(:err)
131
+ # .or_else { |error| Ok.new(error.upcase) }
132
+ # # => Ok.new(:ERR)
133
+ def or_else(&block)
134
+ end
135
+
136
+ # Returns the result of running either the +ok+ or +err+ branch provided
137
+ # to the block based on if the result is {Ok} or {Err}
138
+ # @raise [RuntimeError] unless both +ok+ and +error+ branches are defined
139
+ # @return [Object] mapped value
140
+ # @yieldreturn [Object] mapped value
141
+ # @example
142
+ # Ok.new(1).match do
143
+ # ok { |value| value + 1 }
144
+ # err { |error| fail "unreachable" }
145
+ # end # => 2
146
+ #
147
+ # Err.new(:err).match do
148
+ # ok { |value| fail "unreachable" }
149
+ # err { |error| error.upcase }
150
+ # end # => :ERR
151
+ #
152
+ # Ok.new(1).match do
153
+ # ok { |value| value + 1 }
154
+ # end # => RuntimeError: Must match on both ok and err results
155
+ def match(&block)
156
+ end
157
+ end
158
+ end
159
+ end
@@ -0,0 +1,96 @@
1
+ module Opted
2
+ module Result
3
+ # Value object that represents an error result
4
+ class Err
5
+ # @param error [Object] a non-nil error to wrap
6
+ # @raise [ArgumentError] if provided error is nil
7
+ def initialize(error)
8
+ if error.nil?
9
+ fail ArgumentError.new("can't wrap nil")
10
+ else
11
+ @error = error
12
+ end
13
+ end
14
+
15
+ # If other object is also {Err} and wraps equivalent error
16
+ # @param other [Object] any object
17
+ # @return [Boolean]
18
+ def ==(other)
19
+ other.is_a?(Err) && unwrap_err! == other.unwrap_err!
20
+ end
21
+ alias_method :eql?, :==
22
+
23
+ # (see AbstractResult#ok?)
24
+ # @see Ok#ok?
25
+ def ok?
26
+ false
27
+ end
28
+
29
+ # (see AbstractResult#err?)
30
+ # @see Ok#err?
31
+ def err?
32
+ true
33
+ end
34
+
35
+ # (see AbstractResult#unwrap!)
36
+ # @see Ok#unwrap!
37
+ def unwrap!
38
+ fail UnwrapError.new(__method__, inspect)
39
+ end
40
+
41
+ # (see AbstractResult#unwrap_err!)
42
+ # @see Ok#unwrap_err!
43
+ def unwrap_err!
44
+ @error
45
+ end
46
+
47
+ # (see AbstractResult#map)
48
+ # @see Ok#map
49
+ def map
50
+ self
51
+ end
52
+
53
+ # (see AbstractResult#map_err)
54
+ # @see Ok#map_err
55
+ def map_err
56
+ Err.new(yield unwrap_err!)
57
+ end
58
+
59
+ # (see AbstractResult#unwrap_or)
60
+ # @see Ok#unwrap_or
61
+ def unwrap_or(other_value)
62
+ other_value
63
+ end
64
+
65
+ # (see AbstractResult#and)
66
+ # @see Ok#and
67
+ def and(_other)
68
+ self
69
+ end
70
+
71
+ # (see AbstractResult#and_then)
72
+ # @see Ok#and_then
73
+ def and_then
74
+ self
75
+ end
76
+
77
+ # (see AbstractResult#or)
78
+ # @see Ok#or
79
+ def or(other)
80
+ other
81
+ end
82
+
83
+ # (see AbstractResult#or_else)
84
+ # @see Ok#or_else
85
+ def or_else
86
+ yield unwrap_err!
87
+ end
88
+
89
+ # (see AbstractResult#match)
90
+ # @see Ok#match
91
+ def match(&block)
92
+ Match.match_error(unwrap_err!, &block)
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,77 @@
1
+ module Opted
2
+ module Result
3
+ module Match
4
+ def self.match_value(value, &block)
5
+ Matcher.new(OkMatch.new(value)).match(&block)
6
+ end
7
+
8
+ def self.match_error(error, &block)
9
+ Matcher.new(ErrMatch.new(error)).match(&block)
10
+ end
11
+
12
+ class Matcher
13
+ def initialize(match)
14
+ @match = match
15
+ end
16
+
17
+ def match(&block)
18
+ match = MatchWithBranchChecking.new(@match)
19
+ match.instance_eval(&block)
20
+ match.mapped_value_or_error
21
+ end
22
+ end
23
+
24
+ class OkMatch
25
+ attr_reader :mapped_value_or_error
26
+ def initialize(value)
27
+ @value = value
28
+ end
29
+
30
+ def ok
31
+ @mapped_value_or_error = yield @value
32
+ end
33
+
34
+ def err
35
+ end
36
+ end
37
+
38
+ class ErrMatch
39
+ attr_reader :mapped_value_or_error
40
+ def initialize(error)
41
+ @error = error
42
+ end
43
+
44
+ def ok
45
+ end
46
+
47
+ def err
48
+ @mapped_value_or_error = yield @error
49
+ end
50
+ end
51
+
52
+ class MatchWithBranchChecking
53
+ def initialize(match)
54
+ @match = match
55
+ end
56
+
57
+ def ok(&block)
58
+ @ok_called = true
59
+ @match.ok(&block)
60
+ end
61
+
62
+ def err(&block)
63
+ @err_called = true
64
+ @match.err(&block)
65
+ end
66
+
67
+ def mapped_value_or_error
68
+ if @ok_called && @err_called
69
+ @match.mapped_value_or_error
70
+ else
71
+ fail "Must match on both ok and err results"
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,96 @@
1
+ module Opted
2
+ module Result
3
+ # Value object that represents a successful result
4
+ class Ok
5
+ # @param value [Object] a non-nil value to wrap
6
+ # @raise [ArgumentError] if provided value is nil
7
+ def initialize(value)
8
+ if value.nil?
9
+ fail ArgumentError.new("can't wrap nil")
10
+ else
11
+ @value = value
12
+ end
13
+ end
14
+
15
+ # If other object is also {Ok} and wraps equivalent value
16
+ # @param other [Object] any object
17
+ # @return [Boolean]
18
+ def ==(other)
19
+ other.is_a?(Ok) && unwrap! == other.unwrap!
20
+ end
21
+ alias_method :eql?, :==
22
+
23
+ # (see AbstractResult#ok?)
24
+ # @see Err#ok?
25
+ def ok?
26
+ true
27
+ end
28
+
29
+ # (see AbstractResult#err?)
30
+ # @see Err#err?
31
+ def err?
32
+ false
33
+ end
34
+
35
+ # (see AbstractResult#unwrap!)
36
+ # @see Err#unwrap!
37
+ def unwrap!
38
+ @value
39
+ end
40
+
41
+ # (see AbstractResult#unwrap_err!)
42
+ # @see Err#unwrap_err!
43
+ def unwrap_err!
44
+ fail UnwrapError.new(__method__, inspect)
45
+ end
46
+
47
+ # (see AbstractResult#map)
48
+ # @see Err#map
49
+ def map
50
+ Ok.new(yield unwrap!)
51
+ end
52
+
53
+ # (see AbstractResult#map_err)
54
+ # @see Err#map_err
55
+ def map_err
56
+ self
57
+ end
58
+
59
+ # (see AbstractResult#unwrap_or)
60
+ # @see Err#unwrap_or
61
+ def unwrap_or(_other_value)
62
+ unwrap!
63
+ end
64
+
65
+ # (see AbstractResult#and)
66
+ # @see Err#and
67
+ def and(other)
68
+ other
69
+ end
70
+
71
+ # (see AbstractResult#and_then)
72
+ # @see Err#and_then
73
+ def and_then
74
+ yield unwrap!
75
+ end
76
+
77
+ # (see AbstractResult#or)
78
+ # @see Err#or
79
+ def or(_other)
80
+ self
81
+ end
82
+
83
+ # (see AbstractResult#or_else)
84
+ # @see Err#or_else
85
+ def or_else
86
+ self
87
+ end
88
+
89
+ # (see AbstractResult#match)
90
+ # @see Err#match
91
+ def match(&block)
92
+ Match.match_value(unwrap!, &block)
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,3 @@
1
+ module Opted
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,28 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "opted/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "opted"
8
+ spec.version = Opted::VERSION
9
+ spec.authors = ["Max Holder"]
10
+ spec.email = ["mxhold@gmail.com"]
11
+
12
+ spec.summary = "Result type in Ruby"
13
+ spec.description = "Provides Result type (like in Rust) in idiomatic Ruby"
14
+ spec.homepage = "https://github.com/mxhold/opted"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.16"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "yard", "~> 0.9"
28
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: opted
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Max Holder
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: yard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.9'
69
+ description: Provides Result type (like in Rust) in idiomatic Ruby
70
+ email:
71
+ - mxhold@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".ruby-version"
79
+ - ".travis.yml"
80
+ - CODE_OF_CONDUCT.md
81
+ - Gemfile
82
+ - Gemfile.lock
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/rspec
88
+ - bin/setup
89
+ - lib/opted.rb
90
+ - lib/opted/result.rb
91
+ - lib/opted/result/abstract_result.rb
92
+ - lib/opted/result/err.rb
93
+ - lib/opted/result/match.rb
94
+ - lib/opted/result/ok.rb
95
+ - lib/opted/version.rb
96
+ - opted.gemspec
97
+ homepage: https://github.com/mxhold/opted
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.7.3
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Result type in Ruby
121
+ test_files: []