zx-result 0.0.3 → 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 +12 -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,7 +42,7 @@ module Zx
|
|
38
42
|
end
|
39
43
|
|
40
44
|
def on_unknown(&block)
|
41
|
-
|
45
|
+
Reflect.apply(self, nil, &block)
|
42
46
|
|
43
47
|
self
|
44
48
|
end
|
@@ -46,7 +50,7 @@ module Zx
|
|
46
50
|
def on_success(tag = nil, &block)
|
47
51
|
return self if failure?
|
48
52
|
|
49
|
-
|
53
|
+
Reflect.apply(self, tag, &block)
|
50
54
|
|
51
55
|
self
|
52
56
|
end
|
@@ -54,7 +58,7 @@ module Zx
|
|
54
58
|
def on_failure(tag = nil, &block)
|
55
59
|
return self if success?
|
56
60
|
|
57
|
-
|
61
|
+
Reflect.apply(self, tag, &block)
|
58
62
|
|
59
63
|
self
|
60
64
|
end
|
@@ -63,6 +67,7 @@ module Zx
|
|
63
67
|
case ontype.to_sym
|
64
68
|
when :success then on_success(tag, &block)
|
65
69
|
when :failure then on_failure(tag, &block)
|
70
|
+
when :unknown then on_unknown(tag, &block)
|
66
71
|
end
|
67
72
|
end
|
68
73
|
alias >> on
|
@@ -70,21 +75,11 @@ module Zx
|
|
70
75
|
alias pipe on
|
71
76
|
|
72
77
|
def then(&block)
|
73
|
-
|
74
|
-
end
|
75
|
-
|
76
|
-
def step(&block)
|
77
|
-
fmap(&block)
|
78
|
-
end
|
79
|
-
|
80
|
-
def fmap(&block)
|
81
|
-
return self if failure?
|
82
|
-
|
83
|
-
new_value = block.call @value
|
84
|
-
@value = new_value
|
85
|
-
|
86
|
-
self
|
78
|
+
Fmap.call(self, &block)
|
87
79
|
end
|
80
|
+
alias and_then then
|
81
|
+
alias step then
|
82
|
+
alias fmap then
|
88
83
|
|
89
84
|
def check(&block)
|
90
85
|
return self if !!block.call(@value)
|
@@ -107,47 +102,5 @@ module Zx
|
|
107
102
|
|
108
103
|
self
|
109
104
|
end
|
110
|
-
|
111
|
-
def __execute__(tag = nil, &block)
|
112
|
-
return block.call(@value, [@type, @success]) if tag.nil?
|
113
|
-
|
114
|
-
block.call(@value, [@type, @success]) if @type == tag.to_sym
|
115
|
-
end
|
116
|
-
private :__execute__
|
117
|
-
|
118
|
-
def Success(value = nil, options = {})
|
119
|
-
success!(value, type: options.fetch(:type, :ok))
|
120
|
-
end
|
121
|
-
|
122
|
-
def Success!(value = nil, options = {})
|
123
|
-
success!(value, type: options.fetch(:type, :ok))
|
124
|
-
end
|
125
|
-
|
126
|
-
def Failure(value = nil, options = {})
|
127
|
-
failure!(value, type: options.fetch(:type, :error))
|
128
|
-
end
|
129
|
-
|
130
|
-
def Failure!(value = nil, options = {})
|
131
|
-
failure!(value, type: options.fetch(:type, :error))
|
132
|
-
end
|
133
|
-
|
134
|
-
def self.Success(value = nil, options = {})
|
135
|
-
new.success!(value, type: options.fetch(:type, :ok))
|
136
|
-
end
|
137
|
-
|
138
|
-
def self.Success!(...)
|
139
|
-
Success(...)
|
140
|
-
end
|
141
|
-
|
142
|
-
def self.Failure(value = nil, options = {})
|
143
|
-
new.failure!(value, type: options.fetch(:type, :error))
|
144
|
-
end
|
145
|
-
|
146
|
-
def self.Failure!(...)
|
147
|
-
Failure(...)
|
148
|
-
end
|
149
|
-
|
150
|
-
Success = ->(*kwargs) { Success(*kwargs) }
|
151
|
-
Failure = ->(*kwargs) { Failure(*kwargs) }
|
152
105
|
end
|
153
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
|