dry-monads 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +5 -5
- data/CHANGELOG.md +23 -0
- data/lib/dry/monads/do.rb +18 -17
- data/lib/dry/monads/do/all.rb +13 -6
- data/lib/dry/monads/maybe.rb +4 -4
- data/lib/dry/monads/result.rb +5 -4
- data/lib/dry/monads/result/fixed.rb +3 -2
- data/lib/dry/monads/unit.rb +16 -0
- data/lib/dry/monads/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c819d8b433c150703fa74c233a2079c524e2841ddeb4acf073fef3e16ef54225
|
4
|
+
data.tar.gz: b2da0bd96075be4863bb2073b434489ecae1374ebf0cf5cf55ed11a89c07263f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bbb8cb8fc13031cc1b75e9f830381b29cc89ae4652c58a927c45c474ae93bd13364a190cf0af792586fe25f5a9c3d6611e7dbdce01e63e4347d1fe56774201a4
|
7
|
+
data.tar.gz: 5a116f3c547e15ad14aa7d72894c4cbafb90f893033fa98c141e4e20a2644a48b741e1fcee762e4e417c4c9437803802e441b2ce7d07b4b7791f73f0e4457ca4
|
data/.travis.yml
CHANGED
@@ -10,11 +10,11 @@ before_install:
|
|
10
10
|
after_success:
|
11
11
|
- '[ -d coverage ] && bundle exec codeclimate-test-reporter'
|
12
12
|
rvm:
|
13
|
-
- 2.2.
|
14
|
-
- 2.3.
|
15
|
-
- 2.4.
|
16
|
-
- 2.5.
|
17
|
-
- jruby-9.
|
13
|
+
- 2.2.10
|
14
|
+
- 2.3.7
|
15
|
+
- 2.4.4
|
16
|
+
- 2.5.1
|
17
|
+
- jruby-9.2.0.0
|
18
18
|
- ruby-head
|
19
19
|
env:
|
20
20
|
global:
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,26 @@
|
|
1
|
+
# v1.1.0 2018-10-16
|
2
|
+
|
3
|
+
## Fixed
|
4
|
+
|
5
|
+
* Do notation was made to work nicely with inheritance. This shouldn't break any existing code but if it does please report (flash-gordon)
|
6
|
+
|
7
|
+
## Added
|
8
|
+
|
9
|
+
* `Success()`, `Failure()`, and `Some()` now have `Unit` as a default argument:
|
10
|
+
```ruby
|
11
|
+
include Dry::Monads::Result::Mixin
|
12
|
+
include Dry::Monads::Do
|
13
|
+
|
14
|
+
def call
|
15
|
+
yield do_1
|
16
|
+
yield do_2
|
17
|
+
|
18
|
+
Success() # returns Success(Unit)
|
19
|
+
end
|
20
|
+
```
|
21
|
+
|
22
|
+
[Compare v1.0.1...v1.1.0](https://github.com/dry-rb/dry-monads/compare/v1.0.1...v1.1.0)
|
23
|
+
|
1
24
|
# v1.0.1 2018-08-11
|
2
25
|
|
3
26
|
## Fixed
|
data/lib/dry/monads/do.rb
CHANGED
@@ -76,7 +76,7 @@ module Dry
|
|
76
76
|
# @return [Module]
|
77
77
|
def self.for(*methods)
|
78
78
|
mod = Module.new do
|
79
|
-
methods.each { |
|
79
|
+
methods.each { |method_name| Do.wrap_method(self, method_name) }
|
80
80
|
end
|
81
81
|
|
82
82
|
Module.new do
|
@@ -103,34 +103,35 @@ module Dry
|
|
103
103
|
end
|
104
104
|
|
105
105
|
# @private
|
106
|
-
def self.coerce_to_monad(
|
107
|
-
return
|
106
|
+
def self.coerce_to_monad(monads)
|
107
|
+
return monads if monads.size != 1
|
108
108
|
|
109
|
-
|
109
|
+
first = monads[0]
|
110
110
|
|
111
|
-
case
|
112
|
-
when Array
|
113
|
-
|
114
|
-
|
111
|
+
case first
|
112
|
+
when Array
|
113
|
+
[List.coerce(first).traverse]
|
114
|
+
when List
|
115
|
+
[first.traverse]
|
115
116
|
else
|
116
|
-
|
117
|
+
monads
|
117
118
|
end
|
118
119
|
end
|
119
120
|
|
120
121
|
# @private
|
121
|
-
def self.wrap_method(target,
|
122
|
+
def self.wrap_method(target, method_name)
|
122
123
|
target.module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
|
123
|
-
def #{
|
124
|
+
def #{ method_name }(*)
|
124
125
|
if block_given?
|
125
126
|
super
|
126
127
|
else
|
127
|
-
super do |*
|
128
|
-
|
129
|
-
unwrapped =
|
130
|
-
|
131
|
-
|
128
|
+
super do |*monads|
|
129
|
+
monads = Do.coerce_to_monad(monads)
|
130
|
+
unwrapped = monads.map { |result|
|
131
|
+
monad = result.to_monad
|
132
|
+
monad.or { Do.halt(monad) }.value!
|
132
133
|
}
|
133
|
-
|
134
|
+
monads.size == 1 ? unwrapped[0] : unwrapped
|
134
135
|
end
|
135
136
|
end
|
136
137
|
rescue Halt => e
|
data/lib/dry/monads/do/all.rb
CHANGED
@@ -69,18 +69,24 @@ module Dry
|
|
69
69
|
define_method(:method_added) do |method|
|
70
70
|
super(method)
|
71
71
|
|
72
|
-
tracker.wrap_method(method)
|
72
|
+
tracker.wrap_method(self, method)
|
73
|
+
end
|
74
|
+
|
75
|
+
define_method(:inherited) do |base|
|
76
|
+
super(base)
|
77
|
+
|
78
|
+
base.prepend(wrappers[base])
|
73
79
|
end
|
74
80
|
end
|
75
81
|
end
|
76
82
|
|
77
83
|
def extend_object(target)
|
78
84
|
super
|
79
|
-
target.prepend(wrappers)
|
85
|
+
target.prepend(wrappers[target])
|
80
86
|
end
|
81
87
|
|
82
|
-
def wrap_method(method)
|
83
|
-
Do.wrap_method(wrappers, method)
|
88
|
+
def wrap_method(target, method)
|
89
|
+
Do.wrap_method(wrappers[target], method)
|
84
90
|
end
|
85
91
|
end
|
86
92
|
|
@@ -88,9 +94,10 @@ module Dry
|
|
88
94
|
def self.included(base)
|
89
95
|
super
|
90
96
|
|
91
|
-
|
97
|
+
wrappers = Hash.new { |h, k| h[k] = Module.new }
|
98
|
+
tracker = MethodTracker.new(wrappers)
|
92
99
|
base.extend(tracker)
|
93
|
-
base.instance_methods(false).each { |m| tracker.wrap_method(m) }
|
100
|
+
base.instance_methods(false).each { |m| tracker.wrap_method(base, m) }
|
94
101
|
end
|
95
102
|
end
|
96
103
|
end
|
data/lib/dry/monads/maybe.rb
CHANGED
@@ -4,6 +4,7 @@ require 'dry/core/deprecations'
|
|
4
4
|
|
5
5
|
require 'dry/monads/right_biased'
|
6
6
|
require 'dry/monads/transformer'
|
7
|
+
require 'dry/monads/unit'
|
7
8
|
|
8
9
|
module Dry
|
9
10
|
module Monads
|
@@ -87,9 +88,9 @@ module Dry
|
|
87
88
|
include Dry::Equalizer(:value!)
|
88
89
|
include RightBiased::Right
|
89
90
|
|
90
|
-
def initialize(value)
|
91
|
+
def initialize(value = Undefined)
|
91
92
|
raise ArgumentError, 'nil cannot be some' if value.nil?
|
92
|
-
@value = value
|
93
|
+
@value = Undefined.default(value, Unit)
|
93
94
|
end
|
94
95
|
|
95
96
|
# Does the same thing as #bind except it also wraps the value
|
@@ -209,8 +210,7 @@ module Dry
|
|
209
210
|
# @return [Maybe::Some]
|
210
211
|
#
|
211
212
|
def Some(value = Undefined, &block)
|
212
|
-
v = Undefined.default(value, block)
|
213
|
-
raise ArgumentError, 'No value given' if !value.nil? && v.nil?
|
213
|
+
v = Undefined.default(value, block || Unit)
|
214
214
|
Some.new(v)
|
215
215
|
end
|
216
216
|
|
data/lib/dry/monads/result.rb
CHANGED
@@ -4,6 +4,7 @@ require 'dry/monads/undefined'
|
|
4
4
|
require 'dry/monads/right_biased'
|
5
5
|
require 'dry/monads/transformer'
|
6
6
|
require 'dry/monads/conversion_stubs'
|
7
|
+
require 'dry/monads/unit'
|
7
8
|
|
8
9
|
module Dry
|
9
10
|
module Monads
|
@@ -248,8 +249,7 @@ module Dry
|
|
248
249
|
# @return [Result::Success]
|
249
250
|
#
|
250
251
|
def Success(value = Undefined, &block)
|
251
|
-
v = Undefined.default(value, block)
|
252
|
-
raise ArgumentError, 'No value given' if !value.nil? && v.nil?
|
252
|
+
v = Undefined.default(value, block || Unit)
|
253
253
|
Success.new(v)
|
254
254
|
end
|
255
255
|
|
@@ -264,8 +264,7 @@ module Dry
|
|
264
264
|
# @return [Result::Failure]
|
265
265
|
#
|
266
266
|
def Failure(value = Undefined, &block)
|
267
|
-
v = Undefined.default(value, block)
|
268
|
-
raise ArgumentError, 'No value given' if !value.nil? && v.nil?
|
267
|
+
v = Undefined.default(value, block || Unit)
|
269
268
|
Failure.new(v, RightBiased::Left.trace_caller)
|
270
269
|
end
|
271
270
|
end
|
@@ -299,6 +298,8 @@ module Dry
|
|
299
298
|
# Types.Value(:user_not_found) |
|
300
299
|
# Types.Value(:account_not_found)
|
301
300
|
#
|
301
|
+
# include Dry::Monads::Result(Error)
|
302
|
+
#
|
302
303
|
# def find_account(id)
|
303
304
|
# account = acount_repo.find(id)
|
304
305
|
#
|
data/lib/dry/monads/unit.rb
CHANGED
@@ -2,6 +2,22 @@
|
|
2
2
|
|
3
3
|
module Dry
|
4
4
|
module Monads
|
5
|
+
# Unit is a special object you can use whenever your computations don't
|
6
|
+
# return any payload. Previously, if your function ran a side-effect
|
7
|
+
# and returned no meaningful value, you had to return things like
|
8
|
+
# Success(nil), Success([]), Success({}), Maybe(""), Success(true) and
|
9
|
+
# so forth.
|
10
|
+
#
|
11
|
+
# You should use Unit if you wish to return an empty monad.
|
12
|
+
#
|
13
|
+
# @example with Result
|
14
|
+
# Success(Unit)
|
15
|
+
# Failure(Unit)
|
16
|
+
#
|
17
|
+
# @example with Maybe
|
18
|
+
# Maybe(Unit)
|
19
|
+
# => Some(Unit)
|
20
|
+
#
|
5
21
|
Unit = Object.new.tap do |unit|
|
6
22
|
def unit.to_s
|
7
23
|
'Unit'
|
data/lib/dry/monads/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry-monads
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nikita Shilnikov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-10-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dry-equalizer
|