zx-monads 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/zx/version.rb +1 -1
- data/zx-monads.gemspec +1 -1
- metadata +2 -4
- data/lib/either.rb +0 -80
- data/lib/zx/either.rb +0 -29
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e9acee35f37cd6cec08557b8ecf438ac66cda53c81737def1667d912ac916002
|
4
|
+
data.tar.gz: 7aa82e0795492ba409b8644a3013298af1d4003af0bf8004a71b35423711bf77
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 869629640531acfc443af35b60a76e6696e6b10c2804de327d5054eae310244675f17e55e1964308909bd93c3960cc64cc5cd6ee9959d85c1b637eaed554424c
|
7
|
+
data.tar.gz: 850944a1da552252a4434a47ccfd3615733b23789d677a52303b4352f5a26656eaceaafd57ee1a6b396333c8a5f3179280fb3a1cc05d66b6f487286b4b5ca0d9
|
data/lib/zx/version.rb
CHANGED
data/zx-monads.gemspec
CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
|
|
11
11
|
spec.authors = ['Thadeu Esteves']
|
12
12
|
spec.email = ['tadeuu@gmail.com']
|
13
13
|
spec.summary = 'FP Monads for Ruby'
|
14
|
-
spec.description = 'Use
|
14
|
+
spec.description = 'Use Maybe to handle errors in your code'
|
15
15
|
spec.homepage = 'https://github.com/thadeu/zx-monads'
|
16
16
|
spec.license = 'MIT'
|
17
17
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zx-monads
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thadeu Esteves
|
@@ -66,7 +66,7 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0.70'
|
69
|
-
description: Use
|
69
|
+
description: Use Maybe to handle errors in your code
|
70
70
|
email:
|
71
71
|
- tadeuu@gmail.com
|
72
72
|
executables: []
|
@@ -85,10 +85,8 @@ files:
|
|
85
85
|
- Rakefile
|
86
86
|
- bin/console
|
87
87
|
- bin/setup
|
88
|
-
- lib/either.rb
|
89
88
|
- lib/maybe.rb
|
90
89
|
- lib/zx.rb
|
91
|
-
- lib/zx/either.rb
|
92
90
|
- lib/zx/maybe.rb
|
93
91
|
- lib/zx/steps.rb
|
94
92
|
- lib/zx/version.rb
|
data/lib/either.rb
DELETED
@@ -1,80 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Either
|
4
|
-
class ArgumentError < ArgumentError
|
5
|
-
end
|
6
|
-
|
7
|
-
class ValueError < ArgumentError
|
8
|
-
end
|
9
|
-
|
10
|
-
def self.[](...)
|
11
|
-
new(...)
|
12
|
-
end
|
13
|
-
|
14
|
-
def success?
|
15
|
-
raise NotImplementedError
|
16
|
-
end
|
17
|
-
|
18
|
-
def failure?
|
19
|
-
!success?
|
20
|
-
end
|
21
|
-
|
22
|
-
def value!
|
23
|
-
@value || raise(ValueError, 'value is empty')
|
24
|
-
end
|
25
|
-
|
26
|
-
def on_success(&block)
|
27
|
-
return self if failure?
|
28
|
-
|
29
|
-
block.call(@value)
|
30
|
-
|
31
|
-
self
|
32
|
-
end
|
33
|
-
|
34
|
-
def on_failure(&block)
|
35
|
-
return self if success?
|
36
|
-
|
37
|
-
block.call(@value)
|
38
|
-
|
39
|
-
self
|
40
|
-
end
|
41
|
-
|
42
|
-
def match(right:, left:)
|
43
|
-
case self
|
44
|
-
in Right then right.call(@value)
|
45
|
-
else left.call(@error)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
class Left < Either
|
50
|
-
attr_reader :error
|
51
|
-
|
52
|
-
def initialize(error)
|
53
|
-
@error = error
|
54
|
-
end
|
55
|
-
|
56
|
-
def deconstruct
|
57
|
-
[@error]
|
58
|
-
end
|
59
|
-
|
60
|
-
def success?
|
61
|
-
false
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
class Right < Either
|
66
|
-
attr_reader :value
|
67
|
-
|
68
|
-
def initialize(value)
|
69
|
-
@value = value
|
70
|
-
end
|
71
|
-
|
72
|
-
def deconstruct
|
73
|
-
[@value]
|
74
|
-
end
|
75
|
-
|
76
|
-
def success?
|
77
|
-
true
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
data/lib/zx/either.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
module Zx
|
3
|
-
module Either
|
4
|
-
module Eitherable
|
5
|
-
Left = ->(*kwargs) { ::Either::Left.new(*kwargs) }
|
6
|
-
Right = ->(*kwargs) { ::Either::Right.new(*kwargs) }
|
7
|
-
|
8
|
-
def Right(*kwargs)
|
9
|
-
::Either::Right.new(*kwargs)
|
10
|
-
end
|
11
|
-
|
12
|
-
def Left(*kwargs)
|
13
|
-
::Either::Left.new(*kwargs)
|
14
|
-
end
|
15
|
-
|
16
|
-
def Try(default = nil, options = {})
|
17
|
-
Right yield
|
18
|
-
rescue StandardError => e
|
19
|
-
Left default || options.fetch(:or, nil)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def self.included(klass)
|
24
|
-
klass.include(Eitherable)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
include Either
|
29
|
-
end
|