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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dae639cb8c6a7201b6486ed746ea0a07cdd34d39fb3df6565e84a76f4d135693
4
- data.tar.gz: afd6150d91c54a90b8b69d3495e4f2408ab250a370b77e066a098eee24c1c100
3
+ metadata.gz: c819d8b433c150703fa74c233a2079c524e2841ddeb4acf073fef3e16ef54225
4
+ data.tar.gz: b2da0bd96075be4863bb2073b434489ecae1374ebf0cf5cf55ed11a89c07263f
5
5
  SHA512:
6
- metadata.gz: f4eaad835abb5e00b562651d8393a27310e2c6808c3109785d7352123758a7f2e3d54ac293d99c6f1e5ff928b38fc352f0ab6b7637e1399be5444073078528cc
7
- data.tar.gz: e2ef0c0ed1a9c759293a09fe640cee08601b0a97f0b8e282f9edd71ce28987e895cd84acf6e2f65e4de58909a6121037378f936ad1f6e68e361df8563c9210d0
6
+ metadata.gz: bbb8cb8fc13031cc1b75e9f830381b29cc89ae4652c58a927c45c474ae93bd13364a190cf0af792586fe25f5a9c3d6611e7dbdce01e63e4347d1fe56774201a4
7
+ data.tar.gz: 5a116f3c547e15ad14aa7d72894c4cbafb90f893033fa98c141e4e20a2644a48b741e1fcee762e4e417c4c9437803802e441b2ce7d07b4b7791f73f0e4457ca4
@@ -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.9
14
- - 2.3.6
15
- - 2.4.3
16
- - 2.5.0
17
- - jruby-9.1.15.0
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:
@@ -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
@@ -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 { |m| Do.wrap_method(self, m) }
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(ms)
107
- return ms if ms.size != 1
106
+ def self.coerce_to_monad(monads)
107
+ return monads if monads.size != 1
108
108
 
109
- fst = ms[0]
109
+ first = monads[0]
110
110
 
111
- case fst
112
- when Array, List
113
- list = fst.is_a?(Array) ? List.coerce(fst) : fst
114
- [list.traverse]
111
+ case first
112
+ when Array
113
+ [List.coerce(first).traverse]
114
+ when List
115
+ [first.traverse]
115
116
  else
116
- ms
117
+ monads
117
118
  end
118
119
  end
119
120
 
120
121
  # @private
121
- def self.wrap_method(target, method)
122
+ def self.wrap_method(target, method_name)
122
123
  target.module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
123
- def #{ method }(*)
124
+ def #{ method_name }(*)
124
125
  if block_given?
125
126
  super
126
127
  else
127
- super do |*ms|
128
- ms = Do.coerce_to_monad(ms)
129
- unwrapped = ms.map { |r|
130
- m = r.to_monad
131
- m.or { Do.halt(m) }.value!
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
- ms.size == 1 ? unwrapped[0] : unwrapped
134
+ monads.size == 1 ? unwrapped[0] : unwrapped
134
135
  end
135
136
  end
136
137
  rescue Halt => e
@@ -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
- tracker = MethodTracker.new(Module.new)
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
@@ -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
 
@@ -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
  #
@@ -17,8 +17,9 @@ module Dry::Monads
17
17
  end
18
18
  end
19
19
 
20
- def Success(value)
21
- Success.new(value)
20
+ def Success(value = Undefined, &block)
21
+ v = Undefined.default(value, block || Unit)
22
+ Success.new(v)
22
23
  end
23
24
  end
24
25
  end
@@ -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'
@@ -1,6 +1,6 @@
1
1
  module Dry
2
2
  module Monads
3
3
  # @private
4
- VERSION = '1.0.1'.freeze
4
+ VERSION = '1.1.0'.freeze
5
5
  end
6
6
  end
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.1
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-08-11 00:00:00.000000000 Z
11
+ date: 2018-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-equalizer