mu-result 1.1.0 → 1.2.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 +0 -2
- data/Gemfile.lock +1 -1
- data/README.md +20 -0
- data/lib/mu/result/base_result.rb +10 -0
- data/lib/mu/result/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7035a2ec94da66780f5ad89cc4cc1975a16633e2021b7bbc0366007ae5100773
|
4
|
+
data.tar.gz: 10c5f2b8f40fdfebe315663551f954b16049ad84a6327fa78e3e29ac44f84fe0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d4fbb1d0c9d670d2ae6073d293780de9ec232035732720779b205771f43239a0a3394673aa9f4317155ff6ec5eee8dc0f8fb142a8399363213b00da1b7ed4ee
|
7
|
+
data.tar.gz: 1ecc708bdd34a381a034613ae6d85def2a846e2542e136b5de1542205fa55538dee83387ca4796400e6819eaf512875ba734079c50c3b70c42d0968888a8373b
|
data/CHANGELOG.md
ADDED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -24,6 +24,7 @@ Or install it yourself as:
|
|
24
24
|
|
25
25
|
```ruby
|
26
26
|
include 'mu'
|
27
|
+
|
27
28
|
result = Result.success
|
28
29
|
result.success? # => true
|
29
30
|
result = Result.error
|
@@ -34,6 +35,25 @@ result = Result.success(name: 'Arthur Dent', answer: 42)
|
|
34
35
|
result.data # => { name: 'Arthur Dent', answer: 42 }
|
35
36
|
```
|
36
37
|
|
38
|
+
### Unwrap
|
39
|
+
|
40
|
+
When you want to access to a specific field of the result, you can _unwrap_ it:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
result = Result.success(user: 'olistik', role: :developer)
|
44
|
+
result.unwrap(:user) # => 'olistik'
|
45
|
+
result.unwrap(:role) # => :developer
|
46
|
+
result.unwrap(:name) # => raises `StandardError (The symbol 'name' is not included in the result data object.)`
|
47
|
+
```
|
48
|
+
|
49
|
+
This is a way to avoid getting unwanted nil values out of the result when passing the wrong field:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
result = Result.success(user: 'olistik', role: :developer)
|
53
|
+
value = result.data[:users] # => nil, this could lead to an annoying bug later on because we're not aware there's a typo in `:users`, yet.
|
54
|
+
value = result.unwrap(:users) # This raises an exception instead, making it evident that we're using the wrong name for the field.
|
55
|
+
```
|
56
|
+
|
37
57
|
## Development
|
38
58
|
|
39
59
|
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.
|
@@ -27,6 +27,16 @@ module Mu
|
|
27
27
|
def error?
|
28
28
|
!success?
|
29
29
|
end
|
30
|
+
|
31
|
+
def unwrap(symbol = nil)
|
32
|
+
return data if symbol.nil?
|
33
|
+
|
34
|
+
if !data.respond_to?(:include?) || !data.include?(symbol)
|
35
|
+
raise StandardError.new("The symbol '#{symbol}' is not included in the result data object.")
|
36
|
+
end
|
37
|
+
|
38
|
+
return data[symbol]
|
39
|
+
end
|
30
40
|
end
|
31
41
|
|
32
42
|
end
|
data/lib/mu/result/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mu-result
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- olistik
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-01-
|
11
|
+
date: 2020-01-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -62,6 +62,7 @@ files:
|
|
62
62
|
- ".gitignore"
|
63
63
|
- ".rspec"
|
64
64
|
- ".travis.yml"
|
65
|
+
- CHANGELOG.md
|
65
66
|
- CODE_OF_CONDUCT.md
|
66
67
|
- Gemfile
|
67
68
|
- Gemfile.lock
|