dry-behaviour 0.1.1 → 0.2.0
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/lib/dry/behaviour/black_tie.rb +31 -16
- data/lib/dry/behaviour/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 822ccca7591e84e8f437fb9942ed3e2b0857ebbe
|
4
|
+
data.tar.gz: 80cc1b146e8caf3a3292c3ffc55cf0f88d64b9a6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8fdff2c37197e6ea799e23618a33cb06f8fce388a67ad0a24f79b5e1cf2afe89ee6ab7d7fcfbb2cbab15789740e3ac7cdbc6ccf463644e6df7b85e6f8f8239c6
|
7
|
+
data.tar.gz: b52c34a6dc9d690f6807b6b810bb6b336b278b8ac039e23352a200f02fa526e093e118888bb0c8452098ffddbdbac01309b5768281e1c19b39e0de4780e964c1
|
@@ -1,5 +1,4 @@
|
|
1
1
|
module Dry
|
2
|
-
# rubocop:disable Style/VariableName
|
3
2
|
# rubocop:disable Style/AsciiIdentifiers
|
4
3
|
# rubocop:disable Style/MultilineBlockChain
|
5
4
|
# rubocop:disable Style/EmptyCaseCondition
|
@@ -30,7 +29,7 @@ module Dry
|
|
30
29
|
singleton_class.send :define_method, method do |receiver, *args|
|
31
30
|
receiver.class.ancestors.lazy.map do |c|
|
32
31
|
BlackTie.implementations[self].fetch(c, nil)
|
33
|
-
end.reject(&:nil?).first[method].(
|
32
|
+
end.reject(&:nil?).first[method].(*args.unshift(receiver))
|
34
33
|
end
|
35
34
|
end
|
36
35
|
end
|
@@ -39,26 +38,34 @@ module Dry
|
|
39
38
|
BlackTie.protocols[self][name] = params
|
40
39
|
end
|
41
40
|
|
41
|
+
DELEGATE_METHOD = lambda do |klazz, (source, target)|
|
42
|
+
klazz.class_eval do
|
43
|
+
define_method source do |this, *args, **params, &λ|
|
44
|
+
case
|
45
|
+
when !args.empty? && !params.empty? then this.send(target, *args, **params, &λ)
|
46
|
+
when !args.empty? then this.send(target, *args, &λ)
|
47
|
+
when !params.empty? then this.send(target, **params, &λ)
|
48
|
+
else this.send(target, &λ)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
42
54
|
def defimpl(protocol = nil, target: nil, delegate: [], map: {})
|
43
55
|
raise if target.nil? || !block_given? && delegate.empty? && map.empty?
|
44
56
|
|
45
57
|
mds = normalize_map_delegates(delegate, map)
|
46
58
|
Module.new do
|
47
|
-
mds.each
|
48
|
-
singleton_class.class_eval do
|
49
|
-
define_method k do |this, *args, **params, &λ|
|
50
|
-
case
|
51
|
-
when !args.empty? && !params.empty? then this.send(v, *args, **params, &λ)
|
52
|
-
when !args.empty? then this.send(v, *args, &λ)
|
53
|
-
when !params.empty? then this.send(v, **params, &λ)
|
54
|
-
else this.send(v, &λ)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
+
mds.each(&DELEGATE_METHOD.curry[singleton_class])
|
59
60
|
singleton_class.class_eval(&Proc.new) if block_given? # block takes precedence
|
60
61
|
end.tap do |mod|
|
61
|
-
mod.methods(false).
|
62
|
+
mod.methods(false).tap do |meths|
|
63
|
+
(BlackTie.protocols[protocol || self].keys - meths).each_with_object(meths) do |m, acc|
|
64
|
+
safe_logger.warn("Implicit delegate #{(protocol || self).inspect}##{m} to #{target}")
|
65
|
+
DELEGATE_METHOD.(mod.singleton_class, [m] * 2)
|
66
|
+
acc << m
|
67
|
+
end
|
68
|
+
end.each do |m|
|
62
69
|
BlackTie.implementations[protocol || self][target][m] = mod.method(m).to_proc
|
63
70
|
end
|
64
71
|
end
|
@@ -67,6 +74,15 @@ module Dry
|
|
67
74
|
|
68
75
|
private
|
69
76
|
|
77
|
+
def safe_logger
|
78
|
+
if Kernel.const_defined?('::Rails')
|
79
|
+
Rails.logger
|
80
|
+
else
|
81
|
+
require 'logger'
|
82
|
+
Logger.new($stdout)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
70
86
|
def normalize_map_delegates(delegate, map)
|
71
87
|
[*delegate, *map].map do |e|
|
72
88
|
case e
|
@@ -83,5 +99,4 @@ module Dry
|
|
83
99
|
# rubocop:enable Style/EmptyCaseCondition
|
84
100
|
# rubocop:enable Style/MultilineBlockChain
|
85
101
|
# rubocop:enable Style/AsciiIdentifiers
|
86
|
-
# rubocop:enable Style/VariableName
|
87
102
|
end
|