zx-result 0.0.2 → 0.0.4
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/.rubocop.yml +3 -0
- data/lib/zx/eager_load.rb +5 -0
- data/lib/zx/fmap.rb +14 -0
- data/lib/zx/reflect.rb +22 -0
- data/lib/zx/result.rb +14 -59
- data/lib/zx/version.rb +1 -1
- data/lib/zx.rb +25 -12
- metadata +6 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 210f397a810c2b3298e142cfa3de4b153bc9a4c4f0091bf3764afbe3a7eeb09c
|
|
4
|
+
data.tar.gz: a4e84f994cd018637395ffff524921d41b73e3e61e223192ee74eb2c7fa7e922
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: df7513abb2eba86992c5bf03be139b5baa4500bfda8d5d622a8f1315e0a412606317a0f4c7318d6ff049bffc55e91ecc68b3450f3681e48596660a21c9cc299d
|
|
7
|
+
data.tar.gz: a38aeefd2f63478878541adb7e171eba5de1636db1a8a526a6fef81dd3e13f9b3566ccb753e272ce2e3e8f19c0794c7fa4904b9c3584703a5871f6c4d05ce626
|
data/.rubocop.yml
CHANGED
data/lib/zx/fmap.rb
ADDED
data/lib/zx/reflect.rb
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Zx
|
|
4
|
+
class Reflect
|
|
5
|
+
attr_accessor :result, :tag
|
|
6
|
+
|
|
7
|
+
def initialize(result, tag)
|
|
8
|
+
self.result = result
|
|
9
|
+
self.tag = tag
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def apply(&block)
|
|
13
|
+
return block.call(result.value, [result.type, result.success?]) if tag.nil?
|
|
14
|
+
|
|
15
|
+
block.call(result.value, [result.type, result.success?]) if result.type == tag.to_sym
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.apply(result, tag, &block)
|
|
19
|
+
new(result, tag).apply(&block)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
data/lib/zx/result.rb
CHANGED
|
@@ -29,6 +29,10 @@ module Zx
|
|
|
29
29
|
@value || raise(FailureError)
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
+
def unwrap
|
|
33
|
+
@value
|
|
34
|
+
end
|
|
35
|
+
|
|
32
36
|
def deconstruct
|
|
33
37
|
[type, value]
|
|
34
38
|
end
|
|
@@ -38,13 +42,15 @@ module Zx
|
|
|
38
42
|
end
|
|
39
43
|
|
|
40
44
|
def on_unknown(&block)
|
|
41
|
-
|
|
45
|
+
Reflect.apply(self, nil, &block)
|
|
46
|
+
|
|
47
|
+
self
|
|
42
48
|
end
|
|
43
49
|
|
|
44
50
|
def on_success(tag = nil, &block)
|
|
45
51
|
return self if failure?
|
|
46
52
|
|
|
47
|
-
|
|
53
|
+
Reflect.apply(self, tag, &block)
|
|
48
54
|
|
|
49
55
|
self
|
|
50
56
|
end
|
|
@@ -52,7 +58,7 @@ module Zx
|
|
|
52
58
|
def on_failure(tag = nil, &block)
|
|
53
59
|
return self if success?
|
|
54
60
|
|
|
55
|
-
|
|
61
|
+
Reflect.apply(self, tag, &block)
|
|
56
62
|
|
|
57
63
|
self
|
|
58
64
|
end
|
|
@@ -61,6 +67,7 @@ module Zx
|
|
|
61
67
|
case ontype.to_sym
|
|
62
68
|
when :success then on_success(tag, &block)
|
|
63
69
|
when :failure then on_failure(tag, &block)
|
|
70
|
+
when :unknown then on_unknown(tag, &block)
|
|
64
71
|
end
|
|
65
72
|
end
|
|
66
73
|
alias >> on
|
|
@@ -68,21 +75,11 @@ module Zx
|
|
|
68
75
|
alias pipe on
|
|
69
76
|
|
|
70
77
|
def then(&block)
|
|
71
|
-
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
def step(&block)
|
|
75
|
-
fmap(&block)
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
def fmap(&block)
|
|
79
|
-
return self if failure?
|
|
80
|
-
|
|
81
|
-
new_value = block.call @value
|
|
82
|
-
@value = new_value
|
|
83
|
-
|
|
84
|
-
self
|
|
78
|
+
Fmap.call(self, &block)
|
|
85
79
|
end
|
|
80
|
+
alias and_then then
|
|
81
|
+
alias step then
|
|
82
|
+
alias fmap then
|
|
86
83
|
|
|
87
84
|
def check(&block)
|
|
88
85
|
return self if !!block.call(@value)
|
|
@@ -105,47 +102,5 @@ module Zx
|
|
|
105
102
|
|
|
106
103
|
self
|
|
107
104
|
end
|
|
108
|
-
|
|
109
|
-
def __execute__(tag = nil, &block)
|
|
110
|
-
return block.call(@value, [@type, @success]) if tag.nil?
|
|
111
|
-
|
|
112
|
-
block.call(@value, [@type, @success]) if @type == tag.to_sym
|
|
113
|
-
end
|
|
114
|
-
private :__execute__
|
|
115
|
-
|
|
116
|
-
def Success(value = nil, options = {})
|
|
117
|
-
success!(value, type: options.fetch(:type, :ok))
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
def Success!(value = nil, options = {})
|
|
121
|
-
success!(value, type: options.fetch(:type, :ok))
|
|
122
|
-
end
|
|
123
|
-
|
|
124
|
-
def Failure(value = nil, options = {})
|
|
125
|
-
failure!(value, type: options.fetch(:type, :error))
|
|
126
|
-
end
|
|
127
|
-
|
|
128
|
-
def Failure!(value = nil, options = {})
|
|
129
|
-
failure!(value, type: options.fetch(:type, :error))
|
|
130
|
-
end
|
|
131
|
-
|
|
132
|
-
def self.Success(value = nil, options = {})
|
|
133
|
-
new.success!(value, type: options.fetch(:type, :ok))
|
|
134
|
-
end
|
|
135
|
-
|
|
136
|
-
def self.Success!(...)
|
|
137
|
-
Success(...)
|
|
138
|
-
end
|
|
139
|
-
|
|
140
|
-
def self.Failure(value = nil, options = {})
|
|
141
|
-
new.failure!(value, type: options.fetch(:type, :error))
|
|
142
|
-
end
|
|
143
|
-
|
|
144
|
-
def self.Failure!(...)
|
|
145
|
-
Failure(...)
|
|
146
|
-
end
|
|
147
|
-
|
|
148
|
-
Success = ->(*kwargs) { Success(*kwargs) }
|
|
149
|
-
Failure = ->(*kwargs) { Failure(*kwargs) }
|
|
150
105
|
end
|
|
151
106
|
end
|
data/lib/zx/version.rb
CHANGED
data/lib/zx.rb
CHANGED
|
@@ -1,21 +1,34 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'zx/version'
|
|
4
|
+
require 'zx/fmap'
|
|
5
|
+
require 'zx/reflect'
|
|
3
6
|
require 'zx/result'
|
|
4
7
|
|
|
5
8
|
module Zx
|
|
6
|
-
module
|
|
7
|
-
Success = ->(
|
|
8
|
-
Failure = ->(
|
|
9
|
-
|
|
10
|
-
def Success(
|
|
11
|
-
Result.
|
|
9
|
+
module Methods
|
|
10
|
+
Success = ->(value = nil, options = {}) { Zx.Success(value, { type: :ok }.merge(options)) }
|
|
11
|
+
Failure = ->(value = nil, options = {}) { Zx.Failure(value, { type: :error }.merge(options)) }
|
|
12
|
+
|
|
13
|
+
def Success(value = nil, options = {})
|
|
14
|
+
Zx::Result.new.success!(value, type: options.fetch(:type, :ok))
|
|
12
15
|
end
|
|
13
|
-
|
|
14
|
-
def Failure(
|
|
15
|
-
Result.
|
|
16
|
+
|
|
17
|
+
def Failure(value = nil, options = {})
|
|
18
|
+
Zx::Result.new.failure!(value, type: options.fetch(:type, :error))
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def Try(default = nil, options = {})
|
|
22
|
+
Success[yield]
|
|
23
|
+
rescue StandardError => e
|
|
24
|
+
Failure[default || options.fetch(:or, nil)]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def Given(input)
|
|
28
|
+
Try { input }
|
|
16
29
|
end
|
|
17
30
|
end
|
|
18
|
-
|
|
19
|
-
include
|
|
20
|
-
extend
|
|
31
|
+
|
|
32
|
+
include Methods
|
|
33
|
+
extend Methods
|
|
21
34
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: zx-result
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Thadeu Esteves
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2024-05-07 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -88,6 +88,9 @@ files:
|
|
|
88
88
|
- bin/console
|
|
89
89
|
- bin/setup
|
|
90
90
|
- lib/zx.rb
|
|
91
|
+
- lib/zx/eager_load.rb
|
|
92
|
+
- lib/zx/fmap.rb
|
|
93
|
+
- lib/zx/reflect.rb
|
|
91
94
|
- lib/zx/result.rb
|
|
92
95
|
- lib/zx/version.rb
|
|
93
96
|
- zx-result.gemspec
|
|
@@ -110,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
110
113
|
- !ruby/object:Gem::Version
|
|
111
114
|
version: '0'
|
|
112
115
|
requirements: []
|
|
113
|
-
rubygems_version: 3.
|
|
116
|
+
rubygems_version: 3.4.19
|
|
114
117
|
signing_key:
|
|
115
118
|
specification_version: 4
|
|
116
119
|
summary: Functional result object for Ruby
|