zx-monads 0.0.3 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5813fb9b40773da4e1b045030b8cd3086ac78b19792860d78d7a62b139892d06
4
- data.tar.gz: 0a8923287676072ccec22427a2ea564a318c1ad895bdce66d1c2a1557f8804cb
3
+ metadata.gz: 92ddc57d4126dfd37ebaeeb3695eeb9bd32dfedd4a1a973b1eefe6cc9f082f88
4
+ data.tar.gz: 1baafdd45d22c064368e9a8bd789fed4b23cc4cffb493d903441442e26e47502
5
5
  SHA512:
6
- metadata.gz: 21598b32f355806f176afd8e6fdab613d3d999410943ef570cbfdda8500bd443f28e00e7b35073bdb75dc46fd44d888efe6b3fe8bac82bbb9bec97d873d13e77
7
- data.tar.gz: 64d5dac9dfaabb44fc9227f01fdcec0c7ddfb18a2f8e01c307266eb235438a1ce34af4e078c8f2675fb441b7544ce148a898a89dc281f5206761e0b6c8f6609e
6
+ metadata.gz: 9969b07e28b5a4ca988e416bddcde3360e51c6f572b0712c247aedb7f78717e23e9821c2c83a4fafa4bea660e7c20ada2a43c75d07ec6159c09984f27941d471
7
+ data.tar.gz: 3fffca4170917b53e657cb010727cbdc3fd94f4f752893f01f61022714b5e9fda604b9dac1de3ec11dd642d451ccc6a41427e410894e8105dab63cc05e4c6cb9
@@ -10,7 +10,7 @@ jobs:
10
10
  runs-on: ubuntu-latest
11
11
  strategy:
12
12
  matrix:
13
- ruby-version: ['2.7', '3.0', '3.1', '3.2']
13
+ ruby-version: ['2.7', '3.0', '3.1', 'head']
14
14
 
15
15
  steps:
16
16
  - uses: actions/checkout@v3
data/.rubocop.yml CHANGED
@@ -7,7 +7,7 @@ Style/Documentation:
7
7
  Enabled: false
8
8
 
9
9
  Layout:
10
- Enabled: false
10
+ Enabled: true
11
11
 
12
12
  Style/FrozenStringLiteralComment:
13
13
  Enabled: true
data/Gemfile CHANGED
@@ -1,14 +1,12 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'base64'
4
- gem 'json'
5
3
  gem 'zeitwerk'
6
4
 
7
5
  group :development, :test do
6
+ gem 'byebug'
8
7
  gem 'rubocop', github: 'rubocop/rubocop', require: false
9
8
  end
10
9
 
11
10
  group :test do
12
- gem 'byebug'
13
11
  gem 'rspec'
14
12
  end
data/README.md CHANGED
@@ -96,6 +96,7 @@ end
96
96
  #math(some:, none:) -> Receive callables and associate them
97
97
  #on_success{} -> Only when Some
98
98
  #on_failure{} -> Only then None
99
+ #on(:success|:failure){}
99
100
  ```
100
101
 
101
102
  ### ZX::Maybe
@@ -404,6 +405,15 @@ order.call
404
405
  .on_failure { |none| expect(none.or(0)).to eq(0) }
405
406
  ```
406
407
 
408
+ ```rb
409
+ order = OrderStep.new(20)
410
+
411
+ order.call
412
+ .map { |n| n + 1 }
413
+ .on(:success) { |some| expect(some.unwrap).to eq(10) }
414
+ .on(:failure) { |none| expect(none.or(0)).to eq(0) }
415
+ ```
416
+
407
417
  ```rb
408
418
  order = OrderStep.new(-1)
409
419
 
data/lib/maybe.rb CHANGED
@@ -1,13 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Maybe
4
+ attr_reader :value
5
+
4
6
  IsBlank = ->(value) { value.nil? || value.to_s.strip&.empty? || !value }
5
7
 
6
8
  def self.of(...)
7
9
  new.of(...)
8
10
  end
9
11
 
10
- def self.[](...)
12
+ def self.[](...)
11
13
  of(...)
12
14
  end
13
15
 
@@ -18,7 +20,7 @@ class Maybe
18
20
  rescue StandardError
19
21
  None.new
20
22
  end
21
-
23
+
22
24
  def type
23
25
  to_s.downcase.to_sym
24
26
  end
@@ -32,7 +34,7 @@ class Maybe
32
34
  end
33
35
 
34
36
  def unwrap
35
- @value
37
+ @value
36
38
  end
37
39
 
38
40
  def or(value)
@@ -42,6 +44,7 @@ class Maybe
42
44
  def >>(other)
43
45
  self > other
44
46
  end
47
+ alias | >>
45
48
 
46
49
  def fmap(_)
47
50
  self
@@ -58,7 +61,7 @@ class Maybe
58
61
  case arg
59
62
  in None then self
60
63
  in Symbol | String then dig(arg)
61
- end
64
+ end
62
65
  rescue StandardError => e
63
66
  None.new(e.message)
64
67
  end
@@ -77,7 +80,7 @@ class Maybe
77
80
  def dig(...)
78
81
  Maybe[@value&.dig(...)]
79
82
  end
80
-
83
+
81
84
  def dig!(...)
82
85
  dig(...).unwrap
83
86
  end
@@ -105,6 +108,13 @@ class Maybe
105
108
  self
106
109
  end
107
110
 
111
+ def on(ontype, &block)
112
+ case ontype.to_sym
113
+ when :success then on_success(&block)
114
+ when :failure then on_failure(&block)
115
+ end
116
+ end
117
+
108
118
  class Some < Maybe
109
119
  def self.[](...)
110
120
  new(...)
@@ -114,7 +124,7 @@ class Maybe
114
124
  @value = value
115
125
  end
116
126
 
117
- def deconstruct
127
+ def deconstruct
118
128
  [@value]
119
129
  end
120
130
 
@@ -123,7 +133,7 @@ class Maybe
123
133
  end
124
134
 
125
135
  def to_s
126
- 'Some'
136
+ 'Some'
127
137
  end
128
138
 
129
139
  def >(other)
@@ -136,7 +146,7 @@ class Maybe
136
146
  end
137
147
 
138
148
  class None < Maybe
139
- def self.[](...)
149
+ def self.[](...)
140
150
  new(...)
141
151
  end
142
152
 
@@ -160,4 +170,4 @@ class Maybe
160
170
  self
161
171
  end
162
172
  end
163
- end
173
+ end
data/lib/zx/maybe.rb CHANGED
@@ -1,19 +1,20 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Zx
3
4
  module Maybe
4
5
  module Maybeable
5
6
  None = ->(*kwargs) { ::Maybe::None.new(*kwargs) }
6
7
  Some = ->(*kwargs) { ::Maybe::Some.new(*kwargs) }
7
8
  Maybe = ->(*kwargs) { ::Maybe.of(*kwargs) }
8
-
9
+
9
10
  def Maybe(*kwargs)
10
11
  ::Maybe.of(*kwargs)
11
12
  end
12
-
13
+
13
14
  def Some(*kwargs)
14
15
  ::Maybe::Some.new(*kwargs)
15
16
  end
16
-
17
+
17
18
  def None(*kwargs)
18
19
  ::Maybe::None.new(*kwargs)
19
20
  end
@@ -30,4 +31,4 @@ module Zx
30
31
  klass.extend(Maybeable)
31
32
  end
32
33
  end
33
- end
34
+ end
data/lib/zx/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zx
4
- VERSION = '0.0.3'
4
+ VERSION = '0.0.5'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zx-monads
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thadeu Esteves
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-09-25 00:00:00.000000000 Z
11
+ date: 2023-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler