ruby_result 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 +7 -0
- data/.gitignore +11 -0
- data/.rbenv-gemsets +1 -0
- data/.rspec +3 -0
- data/.ruby-version +1 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +153 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/ruby_result.rb +43 -0
- data/lib/ruby_result/version.rb +3 -0
- data/ruby_result.gemspec +33 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3847c6fa9fcba436ddfaaabd630efeae2f650f91
|
4
|
+
data.tar.gz: b82ef36162c5bcf63f505521b9a604a4a3a6d355
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5b3221cae0f045bb1b8dddb950ca40a6ef320f99ca63ad0e4e8f04b728887c5ff20b5f172dbb9cb07a3168f2893f003bbfc15f595f7f4ff67f64cbfacb1485c1
|
7
|
+
data.tar.gz: 1f6619553ccaabb034415ea4d373d0095ddedd2188567e50a51f985f96526bd0e0dc556a9b69ea4a56b17f868b68d84a196d0e52e8e65e7e1a9bb4039205e281
|
data/.gitignore
ADDED
data/.rbenv-gemsets
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
.gems
|
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.2.3
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at benjreinhart@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015-2016 Ben Reinhart
|
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,153 @@
|
|
1
|
+
# RubyResult
|
2
|
+
|
3
|
+
Provides a very simple set of objects for dealing with return values in a more structured and consistent way.
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
include RubyResult
|
7
|
+
|
8
|
+
case result = Something.perform(*args)
|
9
|
+
when Success then do_something
|
10
|
+
when Failure then do_something_else
|
11
|
+
end
|
12
|
+
```
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'ruby_result'
|
18
|
+
```
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install ruby_result
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
This gem provides two objects, `RubyResult::Success` and `RubyResult::Failure`. Each are initialized with one object, the `value`. The value can be anything, though for consistency I usually return a `Hash`. The gem is intended to help structure the handling of return values.
|
31
|
+
|
32
|
+
As an example, consider an ordering system where many things could fail when attempting to place an order. We'd want to wrap creating an order in a method or module but return an object that can represent success or failure and in each case provide some value.
|
33
|
+
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
module Orders
|
37
|
+
include RubyResult
|
38
|
+
|
39
|
+
def create_order(product, order_options:, customer_options:, shipping_address_options:, charge_options:)
|
40
|
+
result = nil
|
41
|
+
|
42
|
+
product.with_lock do
|
43
|
+
result = Failure(errors: ["Sold out"]) and raise ActiveRecord::Rollback if product.sold_out?
|
44
|
+
|
45
|
+
customer = Customer.find_or_create_by(customer_options)
|
46
|
+
result = Failure(errors: customer.errors.full_messages) and raise ActiveRecord::Rollback unless customer.valid?
|
47
|
+
|
48
|
+
address = Address.find_or_create_by(shipping_address_options.merge(product: purchaser))
|
49
|
+
result = Failure(errors: address.errors.full_messages) and raise ActiveRecord::Rollback unless address.valid?
|
50
|
+
|
51
|
+
order = Orders::Order.create(order_options)
|
52
|
+
result = Failure(errors: order.errors.full_messages) and raise ActiveRecord::Rollback unless order.valid?
|
53
|
+
|
54
|
+
charge = Orders::Charge.create(charge_options.merge(order: order))
|
55
|
+
result = Failure(errors: charge.errors.full_messages) and raise ActiveRecord::Rollback unless charge.valid?
|
56
|
+
|
57
|
+
result = Success(product: product, order: order, customer: customer, address: address, charge: charge)
|
58
|
+
end
|
59
|
+
|
60
|
+
result
|
61
|
+
end
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
Then we could call the above and handle the result in an Orders controller.
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
class OrdersController < ApplicationController
|
69
|
+
include RubyResult
|
70
|
+
|
71
|
+
def create
|
72
|
+
product = load_product(params[:product_id])
|
73
|
+
|
74
|
+
case result = Orders.create_order(product, order_options: order_options, customer_options: customer_options, shipping_address_options: shipping_address_options, charge_options: charge_options)
|
75
|
+
when Success
|
76
|
+
redirect_to customer_order_path(result.value[:customer], result.value[:order]), notice: "Successfully completed order"
|
77
|
+
when Failure
|
78
|
+
redirect_to product_path(product), alert: result.value[:errors].join(". ")
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
84
|
+
## API
|
85
|
+
|
86
|
+
#### RubyResult#Success(value)
|
87
|
+
|
88
|
+
Convenience method for constructing a `RubyResult::Success` object.
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
Success("hooray")
|
92
|
+
```
|
93
|
+
|
94
|
+
#### RubyResult#Failure(value)
|
95
|
+
|
96
|
+
Convenience method for constructing a `RubyResult::Failure` object.
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
Failure("error message")
|
100
|
+
```
|
101
|
+
|
102
|
+
#### RubyResult::[Success,Failure].new(value)
|
103
|
+
|
104
|
+
Create a new `Success` or `Failure` object with some arbitrary `value`.
|
105
|
+
|
106
|
+
#### RubyResult::[Success,Failure].===(other)
|
107
|
+
|
108
|
+
Compare `other` with `self`. If `other` is an instance of `self`, then it is true. Usefull in case statements.
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
case result = Something.perform(*args)
|
112
|
+
when Success then do_something
|
113
|
+
when Failure then do_something_else
|
114
|
+
end
|
115
|
+
```
|
116
|
+
|
117
|
+
#### RubyResult::[Success,Failure]#success?
|
118
|
+
|
119
|
+
Returns true if `self` is an instance of `RubyResult::Success`.
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
result = Something.perform(*args)
|
123
|
+
do_something if result.success?
|
124
|
+
```
|
125
|
+
|
126
|
+
#### RubyResult::[Success,Failure]#failure?
|
127
|
+
|
128
|
+
Returns true if `self` is an instance of `RubyResult::Failure`.
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
result = Something.perform(*args)
|
132
|
+
do_something if result.failure?
|
133
|
+
```
|
134
|
+
|
135
|
+
#### RubyResult::[Success,Failure]#value
|
136
|
+
|
137
|
+
Returns the value provided when constructing an `Success` or `Failure` object.
|
138
|
+
|
139
|
+
```ruby
|
140
|
+
result = Success(order: order)
|
141
|
+
result.value # { order: <#Order...> }
|
142
|
+
```
|
143
|
+
|
144
|
+
|
145
|
+
## Contributing
|
146
|
+
|
147
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/benjreinhart/ruby_result. 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.
|
148
|
+
|
149
|
+
|
150
|
+
## License
|
151
|
+
|
152
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
153
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "ruby_result"
|
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
|
data/bin/setup
ADDED
data/lib/ruby_result.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require "ruby_result/version"
|
2
|
+
|
3
|
+
module RubyResult
|
4
|
+
class AbstractResult
|
5
|
+
def self.===(other)
|
6
|
+
other.instance_of?(self)
|
7
|
+
end
|
8
|
+
|
9
|
+
attr_reader :value
|
10
|
+
|
11
|
+
def initialize(value)
|
12
|
+
@value = value
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Failure < AbstractResult
|
17
|
+
def failure?
|
18
|
+
true
|
19
|
+
end
|
20
|
+
|
21
|
+
def success?
|
22
|
+
false
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Success < AbstractResult
|
27
|
+
def failure?
|
28
|
+
false
|
29
|
+
end
|
30
|
+
|
31
|
+
def success?
|
32
|
+
true
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def Success(v)
|
37
|
+
Success.new(v)
|
38
|
+
end
|
39
|
+
|
40
|
+
def Failure(v)
|
41
|
+
Failure.new(v)
|
42
|
+
end
|
43
|
+
end
|
data/ruby_result.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ruby_result/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ruby_result"
|
8
|
+
spec.version = RubyResult::VERSION
|
9
|
+
spec.authors = ["Ben Reinhart"]
|
10
|
+
spec.email = ["benjreinhart@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Objects for representing success and error states.}
|
13
|
+
spec.description = %q{Objects for representing success and error states. Similar to OCaml's Result type.}
|
14
|
+
spec.homepage = "https://github.com/benjreinhart/ruby_result"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby_result
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben Reinhart
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-13 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.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
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
|
+
description: Objects for representing success and error states. Similar to OCaml's
|
56
|
+
Result type.
|
57
|
+
email:
|
58
|
+
- benjreinhart@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rbenv-gemsets"
|
65
|
+
- ".rspec"
|
66
|
+
- ".ruby-version"
|
67
|
+
- ".travis.yml"
|
68
|
+
- CODE_OF_CONDUCT.md
|
69
|
+
- Gemfile
|
70
|
+
- LICENSE.txt
|
71
|
+
- README.md
|
72
|
+
- Rakefile
|
73
|
+
- bin/console
|
74
|
+
- bin/setup
|
75
|
+
- lib/ruby_result.rb
|
76
|
+
- lib/ruby_result/version.rb
|
77
|
+
- ruby_result.gemspec
|
78
|
+
homepage: https://github.com/benjreinhart/ruby_result
|
79
|
+
licenses:
|
80
|
+
- MIT
|
81
|
+
metadata:
|
82
|
+
allowed_push_host: https://rubygems.org
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 2.4.5.1
|
100
|
+
signing_key:
|
101
|
+
specification_version: 4
|
102
|
+
summary: Objects for representing success and error states.
|
103
|
+
test_files: []
|