dry-behaviour 0.1.0 → 0.1.1
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/README.md +12 -8
- data/lib/dry/behaviour/black_tie.rb +49 -10
- data/lib/dry/behaviour/version.rb +1 -1
- data/lib/dry/behaviour.rb +8 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 080e9972673cdf92dbb1ebe72b097fe8faa05fca
|
4
|
+
data.tar.gz: 7f62264785dda9bf3423bf5bc48176724d70a4db
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c0e50166e9843097a4147cd70924fcb3bae533cd3e38f8265795d4d1ee5087ebbaa363bf1df64f6f9ad81a1e0a5e4832d85cf05599463aea83f9ce85eba69aa5
|
7
|
+
data.tar.gz: 6c39fcdcb28bb5b82648d993b9985ea9402505e72cf745d352270f53b11aa4405163afef5be68aefafd2bf85267270c7c1ce3e84a3c4785211ae7adce26ea08a
|
data/README.md
CHANGED
@@ -39,14 +39,8 @@ module Protocols
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
|
43
|
-
|
44
|
-
this + other
|
45
|
-
end
|
46
|
-
def subtract(this, other)
|
47
|
-
this - other
|
48
|
-
end
|
49
|
-
end
|
42
|
+
# delegate `to_s` as is, map `add` and `subtract` to `:+` and `:-` respectively
|
43
|
+
defimpl target: Integer, delegate: :to_s, map: { add: :+, subtract: :- }
|
50
44
|
end
|
51
45
|
end
|
52
46
|
```
|
@@ -68,6 +62,16 @@ expect(Protocols::Adder.add_default(1)).to eq(6)
|
|
68
62
|
|
69
63
|
@am-kantox, @saverio-kantox & @kantox
|
70
64
|
|
65
|
+
## Changelog
|
66
|
+
|
67
|
+
### `0.1.1` :: delegate and map methods to receiver
|
68
|
+
|
69
|
+
`defimpl` now accepts `delegate` and `map`:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
defimpl MyProto, target: MyClass, delegate: :to_s, map: { add: :+, subtract: :- }
|
73
|
+
```
|
74
|
+
|
71
75
|
## Installation
|
72
76
|
|
73
77
|
Add this line to your application's Gemfile:
|
@@ -1,4 +1,11 @@
|
|
1
1
|
module Dry
|
2
|
+
# rubocop:disable Style/VariableName
|
3
|
+
# rubocop:disable Style/AsciiIdentifiers
|
4
|
+
# rubocop:disable Style/MultilineBlockChain
|
5
|
+
# rubocop:disable Style/EmptyCaseCondition
|
6
|
+
# rubocop:disable Metrics/PerceivedComplexity
|
7
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
8
|
+
# rubocop:disable Metrics/AbcSize
|
2
9
|
module BlackTie
|
3
10
|
class << self
|
4
11
|
def protocols
|
@@ -10,17 +17,15 @@ module Dry
|
|
10
17
|
end
|
11
18
|
end
|
12
19
|
|
13
|
-
def defprotocol
|
20
|
+
def defprotocol
|
14
21
|
raise if BlackTie.protocols.key?(self) # DUPLICATE DEF
|
15
|
-
raise unless block_given? || !delegate.empty?
|
16
|
-
# FIXME IMPLEMENT DELEGATES!
|
17
22
|
raise unless block_given?
|
18
23
|
|
19
24
|
ims = instance_methods(false)
|
20
25
|
class_eval(&Proc.new)
|
21
26
|
(instance_methods(false) - ims).each { |m| class_eval { module_function m } }
|
22
27
|
|
23
|
-
BlackTie.protocols[self].each do |method, *_| # FIXME CHECK PARAMS CORRESPONDENCE HERE
|
28
|
+
BlackTie.protocols[self].each do |method, *_| # FIXME: CHECK PARAMS CORRESPONDENCE HERE
|
24
29
|
# receiver, *args = *args
|
25
30
|
singleton_class.send :define_method, method do |receiver, *args|
|
26
31
|
receiver.class.ancestors.lazy.map do |c|
|
@@ -30,19 +35,53 @@ module Dry
|
|
30
35
|
end
|
31
36
|
end
|
32
37
|
|
33
|
-
def defmethod
|
38
|
+
def defmethod(name, *params)
|
34
39
|
BlackTie.protocols[self][name] = params
|
35
40
|
end
|
36
41
|
|
37
|
-
def defimpl
|
38
|
-
raise
|
39
|
-
raise if params[:for].nil?
|
42
|
+
def defimpl(protocol = nil, target: nil, delegate: [], map: {})
|
43
|
+
raise if target.nil? || !block_given? && delegate.empty? && map.empty?
|
40
44
|
|
41
|
-
|
45
|
+
mds = normalize_map_delegates(delegate, map)
|
46
|
+
Module.new do
|
47
|
+
mds.each do |k, v|
|
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
|
+
singleton_class.class_eval(&Proc.new) if block_given? # block takes precedence
|
60
|
+
end.tap do |mod|
|
42
61
|
mod.methods(false).each do |m|
|
43
|
-
BlackTie.implementations[protocol || self][
|
62
|
+
BlackTie.implementations[protocol || self][target][m] = mod.method(m).to_proc
|
44
63
|
end
|
45
64
|
end
|
46
65
|
end
|
66
|
+
module_function :defimpl
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def normalize_map_delegates(delegate, map)
|
71
|
+
[*delegate, *map].map do |e|
|
72
|
+
case e
|
73
|
+
when Symbol, String then [e.to_sym] * 2
|
74
|
+
when Array then e.map(&:to_sym) if e.size == 2
|
75
|
+
end
|
76
|
+
end.compact
|
77
|
+
end
|
78
|
+
module_function :normalize_map_delegates
|
47
79
|
end
|
80
|
+
# rubocop:enable Metrics/AbcSize
|
81
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
82
|
+
# rubocop:enable Metrics/PerceivedComplexity
|
83
|
+
# rubocop:enable Style/EmptyCaseCondition
|
84
|
+
# rubocop:enable Style/MultilineBlockChain
|
85
|
+
# rubocop:enable Style/AsciiIdentifiers
|
86
|
+
# rubocop:enable Style/VariableName
|
48
87
|
end
|
data/lib/dry/behaviour.rb
CHANGED
@@ -2,13 +2,17 @@ require 'dry/behaviour/version'
|
|
2
2
|
require 'dry/behaviour/black_tie'
|
3
3
|
|
4
4
|
module Dry
|
5
|
-
module Behaviour
|
6
|
-
# Your code goes here...
|
7
|
-
end
|
8
|
-
|
9
5
|
module Protocol
|
10
6
|
def self.included(base)
|
11
7
|
base.singleton_class.prepend(Dry::BlackTie)
|
12
8
|
end
|
9
|
+
|
10
|
+
class << self
|
11
|
+
# rubocop:disable Style/AsciiIdentifiers
|
12
|
+
def defimpl(protocol = nil, target: nil, delegate: [], map: {}, &λ)
|
13
|
+
Dry::BlackTie.defimpl(protocol, target: target, delegate: delegate, map: map, &λ)
|
14
|
+
end
|
15
|
+
# rubocop:enable Style/AsciiIdentifiers
|
16
|
+
end
|
13
17
|
end
|
14
18
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry-behaviour
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aleksei Matiushkin
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2016-10-
|
13
|
+
date: 2016-10-24 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|