zx-result 0.0.1
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/.editorconfig +12 -0
- data/.github/workflows/ci.yml +28 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.rubocop.yml +67 -0
- data/.ruby-version +1 -0
- data/.vscode/settings.json +3 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +13 -0
- data/LICENSE +20 -0
- data/README.md +242 -0
- data/Rakefile +13 -0
- data/bin/console +15 -0
- data/bin/setup +9 -0
- data/lib/zx/result.rb +141 -0
- data/lib/zx/version.rb +3 -0
- data/lib/zx.rb +21 -0
- data/zx-result.gemspec +29 -0
- metadata +117 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 0256c3f98b2f2fa7a72ae9a3ab3e0db5700583c6275fa38b38b1111fb9c4f5f1
|
|
4
|
+
data.tar.gz: 13a29cde376b2d95d3c75500b917c3caaede5a7dac1b9e9d56111e73b2706711
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: efd4d61b29da7587bd6bc408ccf113d2f61ba5d30c5708ac7957dc8da78e0b022bab2a61f5938d307efb77bbd25806f74dfa516b508762b2741a75167a082e3f
|
|
7
|
+
data.tar.gz: 5630e13604ab28ad36cede46b812795b47994e35fdf34c350d407eca6981a77216a6fb3909afef13885ac58539f9986e95186b0de5bbfe73a68c9c553d88264e
|
data/.editorconfig
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
name: ci
|
|
2
|
+
|
|
3
|
+
on: [push]
|
|
4
|
+
|
|
5
|
+
permissions:
|
|
6
|
+
contents: read
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
rspec:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
strategy:
|
|
12
|
+
matrix:
|
|
13
|
+
ruby-version: ['2.7', '3.0', '3.1']
|
|
14
|
+
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v3
|
|
17
|
+
|
|
18
|
+
- name: Set up Ruby
|
|
19
|
+
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
|
|
20
|
+
# change this to (see https://github.com/ruby/setup-ruby#versioning):
|
|
21
|
+
# uses: ruby/setup-ruby@v1
|
|
22
|
+
uses: ruby/setup-ruby@2b019609e2b0f1ea1a2bc8ca11cb82ab46ada124
|
|
23
|
+
with:
|
|
24
|
+
ruby-version: ${{ matrix.ruby-version }}
|
|
25
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
|
26
|
+
|
|
27
|
+
- name: Run RSpec
|
|
28
|
+
run: bundle exec rspec --color
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
Style/Documentation:
|
|
2
|
+
Enabled: false
|
|
3
|
+
|
|
4
|
+
Style/DocumentationMethod:
|
|
5
|
+
Enabled: false
|
|
6
|
+
|
|
7
|
+
Style/ModuleFunction:
|
|
8
|
+
Enabled: false
|
|
9
|
+
|
|
10
|
+
Style/Lambda:
|
|
11
|
+
Enabled: false
|
|
12
|
+
|
|
13
|
+
Metrics/ClassLength:
|
|
14
|
+
Enabled: false
|
|
15
|
+
|
|
16
|
+
Metrics/MethodLength:
|
|
17
|
+
Max: 15
|
|
18
|
+
|
|
19
|
+
Layout/LineLength:
|
|
20
|
+
Max: 150
|
|
21
|
+
|
|
22
|
+
Layout/FirstArrayElementIndentation:
|
|
23
|
+
Enabled: false
|
|
24
|
+
|
|
25
|
+
Layout/MultilineMethodCallIndentation:
|
|
26
|
+
Enabled: false
|
|
27
|
+
|
|
28
|
+
Style/MutableConstant:
|
|
29
|
+
Enabled: true
|
|
30
|
+
|
|
31
|
+
Layout/FirstHashElementIndentation:
|
|
32
|
+
Enabled: false
|
|
33
|
+
|
|
34
|
+
Naming/MethodName:
|
|
35
|
+
Enabled: false
|
|
36
|
+
|
|
37
|
+
Style/FrozenStringLiteralComment:
|
|
38
|
+
Enabled: true
|
|
39
|
+
SafeAutoCorrect: true
|
|
40
|
+
Exclude:
|
|
41
|
+
- bin/**/*
|
|
42
|
+
- config.ru
|
|
43
|
+
- Gemfile
|
|
44
|
+
- Rakefile
|
|
45
|
+
|
|
46
|
+
AllCops:
|
|
47
|
+
DisabledByDefault: false
|
|
48
|
+
NewCops: enable
|
|
49
|
+
TargetRubyVersion: 2.7.6
|
|
50
|
+
# Exclude:
|
|
51
|
+
# - 'spec/factories/*.rb'
|
|
52
|
+
# - *.gemspec
|
|
53
|
+
|
|
54
|
+
Lint/AmbiguousBlockAssociation:
|
|
55
|
+
Exclude:
|
|
56
|
+
- 'spec/**/*.rb'
|
|
57
|
+
|
|
58
|
+
Metrics/BlockLength:
|
|
59
|
+
Exclude:
|
|
60
|
+
- 'spec/**/*.rb'
|
|
61
|
+
|
|
62
|
+
Style/PercentLiteralDelimiters:
|
|
63
|
+
PreferredDelimiters:
|
|
64
|
+
'%i': '()'
|
|
65
|
+
|
|
66
|
+
Lint/DeprecatedConstants:
|
|
67
|
+
Enabled: true
|
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
2.7.6
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -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 tadeuu@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 [https://contributor-covenant.org/version/1/4][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: https://contributor-covenant.org
|
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright 2023 Thadeu Esteves Jr
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<h1 align="center">🔃 Zx::Result</h1>
|
|
3
|
+
<p align="center"><i>Functional result object for Ruby</i></p>
|
|
4
|
+
</p>
|
|
5
|
+
|
|
6
|
+
<p align="center">
|
|
7
|
+
<a href="https://rubygems.org/gems/zx-result">
|
|
8
|
+
<img alt="Gem" src="https://img.shields.io/gem/v/zx-result.svg">
|
|
9
|
+
</a>
|
|
10
|
+
|
|
11
|
+
<a href="https://github.com/thadeu/zx-result/actions/workflows/ci.yml">
|
|
12
|
+
<img alt="Build Status" src="https://github.com/thadeu/zx-result/actions/workflows/ci.yml/badge.svg">
|
|
13
|
+
</a>
|
|
14
|
+
</p>
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
## Motivation
|
|
18
|
+
|
|
19
|
+
Because in sometimes, we need to create a safe return for our objects. This gem simplify this work.
|
|
20
|
+
|
|
21
|
+
## Documentation <!-- omit in toc -->
|
|
22
|
+
|
|
23
|
+
Version | Documentation
|
|
24
|
+
---------- | -------------
|
|
25
|
+
unreleased | https://github.com/thadeu/zx-result/blob/main/README.md
|
|
26
|
+
|
|
27
|
+
## Table of Contents <!-- omit in toc -->
|
|
28
|
+
- [Installation](#installation)
|
|
29
|
+
- [Usage](#usage)
|
|
30
|
+
|
|
31
|
+
## Compatibility
|
|
32
|
+
|
|
33
|
+
| kind | branch | ruby |
|
|
34
|
+
| -------------- | ------- | ------------------ |
|
|
35
|
+
| unreleased | main | >= 2.5.8, <= 3.1.x |
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
Use bundle
|
|
40
|
+
|
|
41
|
+
```ruby
|
|
42
|
+
bundle add zx-result
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
or add this line to your application's Gemfile.
|
|
46
|
+
|
|
47
|
+
```ruby
|
|
48
|
+
gem 'zx-result'
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
and then, require module
|
|
52
|
+
|
|
53
|
+
```ruby
|
|
54
|
+
require 'zx'
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Configuration
|
|
58
|
+
|
|
59
|
+
Without configuration, because we use only Ruby. ❤️
|
|
60
|
+
|
|
61
|
+
## Usage
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
### Success Type
|
|
65
|
+
|
|
66
|
+
```ruby
|
|
67
|
+
result = Zx.Success(5)
|
|
68
|
+
result.success? #=> true
|
|
69
|
+
result.failure? #=> false
|
|
70
|
+
result.value #=> 5
|
|
71
|
+
result.value! #=> 5 or raise
|
|
72
|
+
result.error #=> nil or raises an exception
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
```ruby
|
|
76
|
+
result = Zx.Success(5, type: :integer)
|
|
77
|
+
result.success? #=> true
|
|
78
|
+
result.failure? #=> false
|
|
79
|
+
result.value #=> 5
|
|
80
|
+
result.value! #=> 5 or raise
|
|
81
|
+
result.error #=> nil or raises an exception
|
|
82
|
+
result.type #=> :integer
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Failure Type
|
|
86
|
+
|
|
87
|
+
```ruby
|
|
88
|
+
result = Zx.Failure(:fizz)
|
|
89
|
+
result.success? #=> false
|
|
90
|
+
result.failure? #=> true
|
|
91
|
+
result.value #=> raises an exception
|
|
92
|
+
result.error #=> :fizz
|
|
93
|
+
result.type #=> :error
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
```ruby
|
|
97
|
+
result = Zx.Failure(:fizz, type: :not_found)
|
|
98
|
+
result.success? #=> false
|
|
99
|
+
result.failure? #=> true
|
|
100
|
+
result.value #=> raises an exception
|
|
101
|
+
result.error #=> :fizz
|
|
102
|
+
result.type #=> :not_found
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Map or Then
|
|
106
|
+
|
|
107
|
+
```ruby
|
|
108
|
+
result = Zx.Success(5, type: :integer)
|
|
109
|
+
.fmap{ |number| number + 5 }
|
|
110
|
+
.fmap{ |number| number + 5 }
|
|
111
|
+
.fmap{ |number| number + 5 }
|
|
112
|
+
.on_success(:integer) {|number| puts number } #=> 20
|
|
113
|
+
.on(:success, :integer) {|number| puts number } #=> 20
|
|
114
|
+
.on_success {|number| puts number } #=> 20
|
|
115
|
+
|
|
116
|
+
result.success? #=> true
|
|
117
|
+
result.failure? #=> false
|
|
118
|
+
result.value #=> 20
|
|
119
|
+
result.value! #=> 20 or raise
|
|
120
|
+
result.error #=> nil or raises a n exception
|
|
121
|
+
result.type #=> :integer
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
```ruby
|
|
125
|
+
result = Zx.Success(5, type: :integer)
|
|
126
|
+
.then{ |number| number + 5 }
|
|
127
|
+
.then{ |number| number + 5 }
|
|
128
|
+
.then{ |number| number + 5 }
|
|
129
|
+
.on_success{|number| puts number } #=> 20
|
|
130
|
+
|
|
131
|
+
result.success? #=> true
|
|
132
|
+
result.failure? #=> false
|
|
133
|
+
result.value #=> 20
|
|
134
|
+
result.value! #=> 20 or raise
|
|
135
|
+
result.error #=> nil or raises an exception
|
|
136
|
+
result.type #=> :integer
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
You can use one or multiples listeners in your result. We see some use cases.
|
|
140
|
+
|
|
141
|
+
**Simple composition**
|
|
142
|
+
|
|
143
|
+
```ruby
|
|
144
|
+
class AsIncluded
|
|
145
|
+
include Zx
|
|
146
|
+
|
|
147
|
+
def pass(...)
|
|
148
|
+
Success(...)
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def passthrough(value)
|
|
152
|
+
Success[value]
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def failed(error)
|
|
156
|
+
Failure[error, type: :error]
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
result = AsIncluded.new.pass('save record!')
|
|
161
|
+
|
|
162
|
+
result
|
|
163
|
+
.on(:success, :success) { expect(_1).to eq(a: 1) }
|
|
164
|
+
.on(:success, :mailer) { expect(_1).to eq(a: 1) }
|
|
165
|
+
.on(:success, :persisted) { expect(_1).to eq('save record!') }
|
|
166
|
+
.on(:success) { |value, (type)| expect([value, type]).to eq(['save record!', :persisted]) }
|
|
167
|
+
.on(:failure, :error) { expect(_1).to eq('on error') }
|
|
168
|
+
.on(:failure, :record_not_found) { expect(_1).to eq('not found user') }
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
**Simple Inherit**
|
|
172
|
+
|
|
173
|
+
```ruby
|
|
174
|
+
class AsInherited < Zx::Result
|
|
175
|
+
def pass(...)
|
|
176
|
+
Success(...)
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def passthrough(value)
|
|
180
|
+
Success[value]
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def failed(error)
|
|
184
|
+
Failure(error, type: :error)
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
result = AsInherited.new.pass('save record!')
|
|
189
|
+
|
|
190
|
+
result
|
|
191
|
+
.on(:success, :success) { expect(_1).to eq(a: 1) }
|
|
192
|
+
.on(:success, :mailer) { expect(_1).to eq(a: 1) }
|
|
193
|
+
.on(:success, :persisted) { expect(_1).to eq('save record!') }
|
|
194
|
+
.on(:success) { |value, (type)| expect([value, type]).to eq(['save record!', :persisted]) }
|
|
195
|
+
.on(:failure, :error) { expect(_1).to eq('on error') }
|
|
196
|
+
.on(:failure, :record_not_found) { expect(_1).to eq('not found user') }
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
You can use directly methods, for example:
|
|
200
|
+
|
|
201
|
+
```ruby
|
|
202
|
+
Zx::Result.Success(relation)
|
|
203
|
+
|
|
204
|
+
# or
|
|
205
|
+
|
|
206
|
+
Zx::Result::Success[relation]
|
|
207
|
+
|
|
208
|
+
# or
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
Zx::Success[relation]
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
```ruby
|
|
215
|
+
Zx::Result.Failure('error', type: :invalid)
|
|
216
|
+
|
|
217
|
+
# or
|
|
218
|
+
|
|
219
|
+
Zx::Result::Failure[:invalid_user, 'user was not found']
|
|
220
|
+
|
|
221
|
+
# or
|
|
222
|
+
|
|
223
|
+
Zx::Failure[:invalid_user, 'user was not found']
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
[⬆️ Back to Top](#table-of-contents-)
|
|
228
|
+
|
|
229
|
+
## Development
|
|
230
|
+
|
|
231
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
232
|
+
|
|
233
|
+
To install this gem onto your local machine, run `bundle 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).
|
|
234
|
+
|
|
235
|
+
## Contributing
|
|
236
|
+
|
|
237
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/thadeu/zx-result. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/thadeu/zx-result/blob/master/CODE_OF_CONDUCT.md).
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
## License
|
|
241
|
+
|
|
242
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'bundler/gem_tasks'
|
|
4
|
+
require 'rspec/core/rake_task'
|
|
5
|
+
require 'rubocop/rake_task'
|
|
6
|
+
|
|
7
|
+
RSpec::Core::RakeTask.new(:spec)
|
|
8
|
+
|
|
9
|
+
RuboCop::RakeTask.new(:rubocop) do |t|
|
|
10
|
+
t.options = ['--display-cop-names']
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
task default: %i(spec rubocop)
|
data/bin/console
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require 'bundler/setup'
|
|
5
|
+
require 'zx'
|
|
6
|
+
|
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
9
|
+
|
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
11
|
+
# require "pry"
|
|
12
|
+
# Pry.start
|
|
13
|
+
|
|
14
|
+
require 'irb'
|
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/zx/result.rb
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Zx
|
|
4
|
+
class Result
|
|
5
|
+
class FailureError < StandardError; end
|
|
6
|
+
|
|
7
|
+
def initialize
|
|
8
|
+
@value = nil
|
|
9
|
+
@success = true
|
|
10
|
+
@exception = false
|
|
11
|
+
@type = nil
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
attr_reader :value, :type
|
|
15
|
+
|
|
16
|
+
def error
|
|
17
|
+
@value unless type == :ok
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def success?
|
|
21
|
+
!!@success
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def failure?
|
|
25
|
+
!success?
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def value!
|
|
29
|
+
@value || raise(FailureError)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def deconstruct
|
|
33
|
+
[type, value]
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def deconstruct_keys(_)
|
|
37
|
+
{ type: type, value: value, error: error }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def on_unknown(&block)
|
|
41
|
+
__execute__(nil, &block)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def on_success(tag = nil, &block)
|
|
45
|
+
return self if failure?
|
|
46
|
+
|
|
47
|
+
__execute__(tag, &block)
|
|
48
|
+
|
|
49
|
+
self
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def on_failure(tag = nil, &block)
|
|
53
|
+
return self if success?
|
|
54
|
+
|
|
55
|
+
__execute__(tag, &block)
|
|
56
|
+
|
|
57
|
+
self
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def on(ontype, tag = nil, &block)
|
|
61
|
+
case ontype.to_sym
|
|
62
|
+
when :success then on_success(tag, &block)
|
|
63
|
+
when :failure then on_failure(tag, &block)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
alias >> on
|
|
67
|
+
alias | on
|
|
68
|
+
alias pipe on
|
|
69
|
+
|
|
70
|
+
def then(&block)
|
|
71
|
+
fmap(&block)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def fmap(&block)
|
|
75
|
+
return self if failure?
|
|
76
|
+
|
|
77
|
+
new_value = block.call @value
|
|
78
|
+
@value = new_value
|
|
79
|
+
|
|
80
|
+
self
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def failure!(value = nil, type: :error)
|
|
84
|
+
@type = type.to_sym
|
|
85
|
+
@success = false
|
|
86
|
+
@value = value
|
|
87
|
+
|
|
88
|
+
self
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def success!(value = nil, type: :ok)
|
|
92
|
+
@type = type.to_sym
|
|
93
|
+
@success = true
|
|
94
|
+
@value = value
|
|
95
|
+
|
|
96
|
+
self
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def __execute__(tag = nil, &block)
|
|
100
|
+
return block.call(@value, [@type, @success]) if tag.nil?
|
|
101
|
+
|
|
102
|
+
block.call(@value, [@type, @success]) if @type == tag.to_sym
|
|
103
|
+
end
|
|
104
|
+
private :__execute__
|
|
105
|
+
|
|
106
|
+
def Success(value = nil, options = {})
|
|
107
|
+
success!(value, type: options.fetch(:type, :ok))
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def Success!(value = nil, options = {})
|
|
111
|
+
success!(value, type: options.fetch(:type, :ok))
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def Failure(value = nil, options = {})
|
|
115
|
+
failure!(value, type: options.fetch(:type, :error))
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def Failure!(value = nil, options = {})
|
|
119
|
+
failure!(value, type: options.fetch(:type, :error))
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def self.Success(value = nil, options = {})
|
|
123
|
+
new.success!(value, type: options.fetch(:type, :ok))
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def self.Success!(...)
|
|
127
|
+
Success(...)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def self.Failure(value = nil, options = {})
|
|
131
|
+
new.failure!(value, type: options.fetch(:type, :error))
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def self.Failure!(...)
|
|
135
|
+
Failure(...)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
Success = ->(*kwargs) { Success(*kwargs) }
|
|
139
|
+
Failure = ->(*kwargs) { Failure(*kwargs) }
|
|
140
|
+
end
|
|
141
|
+
end
|
data/lib/zx/version.rb
ADDED
data/lib/zx.rb
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'zx/result'
|
|
4
|
+
|
|
5
|
+
module Zx
|
|
6
|
+
module Extendable
|
|
7
|
+
Success = ->(*kwargs) { Result.Success(*kwargs) }
|
|
8
|
+
Failure = ->(*kwargs) { Result.Failure(*kwargs) }
|
|
9
|
+
|
|
10
|
+
def Success(...)
|
|
11
|
+
Result.Success(...)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def Failure(...)
|
|
15
|
+
Result.Failure(...)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
include Extendable
|
|
20
|
+
extend Extendable
|
|
21
|
+
end
|
data/zx-result.gemspec
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
5
|
+
|
|
6
|
+
require 'zx/version'
|
|
7
|
+
|
|
8
|
+
Gem::Specification.new do |spec|
|
|
9
|
+
spec.name = 'zx-result'
|
|
10
|
+
spec.version = Zx::VERSION
|
|
11
|
+
spec.authors = ['Thadeu Esteves']
|
|
12
|
+
spec.email = ['tadeuu@gmail.com']
|
|
13
|
+
spec.summary = 'Functional result object for Ruby'
|
|
14
|
+
spec.description = 'Expose an methods to create result object flow'
|
|
15
|
+
spec.homepage = 'https://github.com/thadeu/zx-result'
|
|
16
|
+
spec.license = 'MIT'
|
|
17
|
+
|
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
spec.required_ruby_version = '>= 2.7.6'
|
|
23
|
+
spec.require_paths = ['lib']
|
|
24
|
+
|
|
25
|
+
spec.add_development_dependency 'bundler', '>= 1.14'
|
|
26
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
|
27
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
|
28
|
+
spec.add_development_dependency 'rubocop', '~> 0.70'
|
|
29
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: zx-result
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Thadeu Esteves
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2023-09-12 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.14'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.14'
|
|
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: rubocop
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0.70'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '0.70'
|
|
69
|
+
description: Expose an methods to create result object flow
|
|
70
|
+
email:
|
|
71
|
+
- tadeuu@gmail.com
|
|
72
|
+
executables: []
|
|
73
|
+
extensions: []
|
|
74
|
+
extra_rdoc_files: []
|
|
75
|
+
files:
|
|
76
|
+
- ".editorconfig"
|
|
77
|
+
- ".github/workflows/ci.yml"
|
|
78
|
+
- ".gitignore"
|
|
79
|
+
- ".rspec"
|
|
80
|
+
- ".rubocop.yml"
|
|
81
|
+
- ".ruby-version"
|
|
82
|
+
- ".vscode/settings.json"
|
|
83
|
+
- CODE_OF_CONDUCT.md
|
|
84
|
+
- Gemfile
|
|
85
|
+
- LICENSE
|
|
86
|
+
- README.md
|
|
87
|
+
- Rakefile
|
|
88
|
+
- bin/console
|
|
89
|
+
- bin/setup
|
|
90
|
+
- lib/zx.rb
|
|
91
|
+
- lib/zx/result.rb
|
|
92
|
+
- lib/zx/version.rb
|
|
93
|
+
- zx-result.gemspec
|
|
94
|
+
homepage: https://github.com/thadeu/zx-result
|
|
95
|
+
licenses:
|
|
96
|
+
- MIT
|
|
97
|
+
metadata: {}
|
|
98
|
+
post_install_message:
|
|
99
|
+
rdoc_options: []
|
|
100
|
+
require_paths:
|
|
101
|
+
- lib
|
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
103
|
+
requirements:
|
|
104
|
+
- - ">="
|
|
105
|
+
- !ruby/object:Gem::Version
|
|
106
|
+
version: 2.7.6
|
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
|
+
requirements:
|
|
109
|
+
- - ">="
|
|
110
|
+
- !ruby/object:Gem::Version
|
|
111
|
+
version: '0'
|
|
112
|
+
requirements: []
|
|
113
|
+
rubygems_version: 3.1.6
|
|
114
|
+
signing_key:
|
|
115
|
+
specification_version: 4
|
|
116
|
+
summary: Functional result object for Ruby
|
|
117
|
+
test_files: []
|