lamy_result 0.3.0 → 0.4.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 +4 -4
- data/CHANGELOG.md +6 -0
- data/Gemfile.lock +1 -1
- data/README.md +36 -25
- data/Rakefile +3 -3
- data/lib/lamy_result/lamy.rb +32 -15
- data/lib/lamy_result/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c804f6269e93e5c6f4741bf8d4de4c0d1f67a3cad29022620f6c4b394aaf8460
|
|
4
|
+
data.tar.gz: 5128295c967871adec2d7d3251bf2a7b39fcc9a82c56c3ade5f04922f43fc3c9
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: eb41fa69656252be504bc8096072b221a2ce033e9dd39a915b430153d2862bd6bd92391a8418cd2e6e2c9d0fb479de04bab29d3b30abbf7d74a3e431c7aa3dd0
|
|
7
|
+
data.tar.gz: 35aad442edd5f6343db7d17f372582760077dc56f36f58ef13559ad89693aba50f55c8ffd61ceaececd3f465ca565b4225e021f7451bfbe5efbf8cad97aa1132
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -6,7 +6,37 @@ The project is inspired by Elixir and Erlang's tagged tuple and Rust's Result/Op
|
|
|
6
6
|
|
|
7
7
|
Rather than change the way you're doing things. Returning a single result is great for most cases. You'll know if and when you need something more.
|
|
8
8
|
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
Add this line to your application's Gemfile:
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
gem 'lamy_result'
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
And then execute:
|
|
18
|
+
|
|
19
|
+
$ bundle install
|
|
20
|
+
|
|
21
|
+
Or install it yourself as:
|
|
22
|
+
|
|
23
|
+
$ gem install lamy_result
|
|
24
|
+
|
|
25
|
+
[LamyResult](https://rubygems.org/gems/lamy_result) on RubyGems.org
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
A few pre-designed status labels are included: `succeeded` `ok` `failed` `error` `true` `false`
|
|
30
|
+
|
|
31
|
+
Providing a short-hand way to create a result, each statuc corresponds to a static method (or class method) on the Lamy class.
|
|
32
|
+
|
|
33
|
+
For example, `Lamy.failed('You did not think this through.')` will return a Lamy instance with the status set to `:failed` and the value to `You did not think this through.`
|
|
34
|
+
|
|
35
|
+
Here are some basic examples.
|
|
36
|
+
|
|
9
37
|
```ruby
|
|
38
|
+
require 'lamy_result'
|
|
39
|
+
|
|
10
40
|
include LamyResult
|
|
11
41
|
|
|
12
42
|
result = Lamy.ok('Lamy is awesome')
|
|
@@ -22,6 +52,12 @@ result.ok_then do |v|
|
|
|
22
52
|
do_something_cool v
|
|
23
53
|
end
|
|
24
54
|
|
|
55
|
+
# When either of the input statuses match the instance's status, the
|
|
56
|
+
# instance value will be yielded to the block.
|
|
57
|
+
result.any_then(:ok, :success) do |value|
|
|
58
|
+
value.upcase
|
|
59
|
+
end
|
|
60
|
+
|
|
25
61
|
result.to_a
|
|
26
62
|
# Output: [:ok, 'Lamy is awesome']
|
|
27
63
|
|
|
@@ -45,31 +81,6 @@ another_result.successful?
|
|
|
45
81
|
another_result.succeeded?
|
|
46
82
|
```
|
|
47
83
|
|
|
48
|
-
## Installation
|
|
49
|
-
|
|
50
|
-
Add this line to your application's Gemfile:
|
|
51
|
-
|
|
52
|
-
```ruby
|
|
53
|
-
gem 'lamy_result'
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
And then execute:
|
|
57
|
-
|
|
58
|
-
$ bundle install
|
|
59
|
-
|
|
60
|
-
Or install it yourself as:
|
|
61
|
-
|
|
62
|
-
$ gem install lamy_result
|
|
63
|
-
|
|
64
|
-
## Usage
|
|
65
|
-
|
|
66
|
-
A few pre-designed status labels are included: `success` `ok` `failed` `error` `true` `false`
|
|
67
|
-
|
|
68
|
-
As a short-hand way to create a result, each one corresponds to a static method (or class method) on the Lamy class.
|
|
69
|
-
|
|
70
|
-
For example, `Lamy.failed('You did not think this through.')` will instantize a Lamy instance with the status set to `:failed` and the value to `You did not think this through.`
|
|
71
|
-
|
|
72
|
-
|
|
73
84
|
## Motivation
|
|
74
85
|
|
|
75
86
|
The idea came from a function which would check if a file exists with either dashes or underscores. If I was looking for `./ecchi/ecchi-pic-7.jpg`, it should also check for `./ecchi/ecchi_pic_7.jpg`. Without going on a tangent, the function is meant to reconcile file name inconsistencies.
|
data/Rakefile
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
4
|
-
require
|
|
3
|
+
require 'bundler/gem_tasks'
|
|
4
|
+
require 'rspec/core/rake_task'
|
|
5
5
|
|
|
6
6
|
RSpec::Core::RakeTask.new(:spec)
|
|
7
7
|
|
|
8
|
-
require
|
|
8
|
+
require 'rubocop/rake_task'
|
|
9
9
|
|
|
10
10
|
RuboCop::RakeTask.new
|
|
11
11
|
|
data/lib/lamy_result/lamy.rb
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module LamyResult
|
|
4
|
-
|
|
5
4
|
# Creates and checks result instances.
|
|
6
5
|
#
|
|
7
6
|
# A quick reference for class and instance methods.
|
|
@@ -41,8 +40,24 @@ module LamyResult
|
|
|
41
40
|
statuses.any? {|status| status_is?(status) }
|
|
42
41
|
end
|
|
43
42
|
|
|
44
|
-
#
|
|
45
|
-
#
|
|
43
|
+
# Yields the instance value if its status matches either of the instance
|
|
44
|
+
# statuses. If no block is given, the value will be returned. If there is
|
|
45
|
+
# no match, false will be returned.
|
|
46
|
+
#
|
|
47
|
+
# Usage:
|
|
48
|
+
# status = Lamy.ok('Yukihana Lamy is awesome')
|
|
49
|
+
# status.any_then(:ok, :success, :good) {|v| v.upcase }
|
|
50
|
+
#
|
|
51
|
+
def any_then(*statuses)
|
|
52
|
+
if any?(*statuses)
|
|
53
|
+
return yield if block_given?
|
|
54
|
+
|
|
55
|
+
# Just return the value.
|
|
56
|
+
return @value
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
false
|
|
60
|
+
end
|
|
46
61
|
|
|
47
62
|
# Calls #status_is? on the `status_to_check` and if true, yields the value.
|
|
48
63
|
# Otherwise, returns nil. This is a low-level method meant to be
|
|
@@ -51,6 +66,7 @@ module LamyResult
|
|
|
51
66
|
if status_is?(status_to_check)
|
|
52
67
|
return yield @value if block_given?
|
|
53
68
|
|
|
69
|
+
# Just return the value.
|
|
54
70
|
return @value
|
|
55
71
|
end
|
|
56
72
|
|
|
@@ -100,13 +116,13 @@ module LamyResult
|
|
|
100
116
|
new_status, *new_status_aliases = Array(status)
|
|
101
117
|
|
|
102
118
|
# Defines the short-hand class method like .ok(input)
|
|
103
|
-
|
|
119
|
+
define_status_method(new_status, *new_status_aliases)
|
|
104
120
|
|
|
105
121
|
# Define status check instance methods like #ok?
|
|
106
|
-
|
|
122
|
+
define_status_check_method(new_status, *new_status_aliases)
|
|
107
123
|
|
|
108
124
|
# Define conditional instance methods like #ok_then
|
|
109
|
-
|
|
125
|
+
define_conditional_then_method(new_status, *new_status_aliases)
|
|
110
126
|
end
|
|
111
127
|
end
|
|
112
128
|
|
|
@@ -118,19 +134,19 @@ module LamyResult
|
|
|
118
134
|
end
|
|
119
135
|
|
|
120
136
|
def self.format_status_check_method(status)
|
|
121
|
-
|
|
137
|
+
format_status("#{status}?")
|
|
122
138
|
end
|
|
123
139
|
|
|
124
140
|
def self.format_conditional_method(status)
|
|
125
|
-
|
|
141
|
+
format_status("#{status}_then")
|
|
126
142
|
end
|
|
127
143
|
|
|
128
144
|
def self.define_status_method(status, *aliases)
|
|
129
145
|
self
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
146
|
+
.class
|
|
147
|
+
.send(:define_method, status) do |value|
|
|
148
|
+
Lamy.new(status: status, value: value)
|
|
149
|
+
end
|
|
134
150
|
|
|
135
151
|
# Define aliases for the new status if we got any.
|
|
136
152
|
define_class_aliases_for(status, *aliases)
|
|
@@ -151,7 +167,7 @@ module LamyResult
|
|
|
151
167
|
|
|
152
168
|
assert_new_instance_method! method_symbol
|
|
153
169
|
|
|
154
|
-
|
|
170
|
+
define_instance_aliases_for(method_symbol, *status_check_aliases)
|
|
155
171
|
end
|
|
156
172
|
|
|
157
173
|
def self.define_conditional_then_method(status, *aliases)
|
|
@@ -173,8 +189,8 @@ module LamyResult
|
|
|
173
189
|
# Checks if the method is an instance method, otherwise raises a
|
|
174
190
|
# StandardError.
|
|
175
191
|
def self.assert_new_instance_method!(method_symbol)
|
|
176
|
-
unless
|
|
177
|
-
raise StandardError
|
|
192
|
+
unless instance_methods.include?(method_symbol)
|
|
193
|
+
raise StandardError, "##{method_symbol} is not an instance method."
|
|
178
194
|
end
|
|
179
195
|
|
|
180
196
|
true
|
|
@@ -217,6 +233,7 @@ module LamyResult
|
|
|
217
233
|
alias == status_is?
|
|
218
234
|
alias eql? status_is?
|
|
219
235
|
alias either? any?
|
|
236
|
+
alias either_then any_then
|
|
220
237
|
|
|
221
238
|
class << self
|
|
222
239
|
# The singular form may be more comfortable for users wanting to define
|
data/lib/lamy_result/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: lamy_result
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jed Burke
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2022-
|
|
11
|
+
date: 2022-05-02 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: 'The project is inspired by Elixir and Erlang''s tagged tuple and Rust''s
|
|
14
14
|
Result/Option. Despite inspiration from other languages, Lamy Result aims to be
|