dry-behaviour 0.11.2 → 0.12.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 +5 -5
- data/.github/workflows/ci.yml +4 -4
- data/.github/workflows/gempush.yml +3 -3
- data/.tool-versions +1 -0
- data/README.md +39 -0
- data/lib/dry/behaviour/black_tie.rb +19 -7
- data/lib/dry/behaviour/version.rb +1 -1
- metadata +5 -4
- data/.travis.yml +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: eca195e7f40df7dbd0ff8719938bbca1c717c82c
|
4
|
+
data.tar.gz: 820195b2a867f6e5c1a350b991108259b47c5888
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f106a7ad2a2a4548d17aeeb0053cd8875ce8126330b5cd3ae4d51219e73b564a2a6fe29ab9841e31ac3e506e1746013a96dde1d40ed094ecf18fe09e5fa7e4e4
|
7
|
+
data.tar.gz: bb91d87c3442fd30ea5887a33ca604000d1789f9a60907d0a9b3d2f29ab37966a3ff8b9574a22414307c22309d1ea2d2b0e15f4eb8ec606717d2b24ca1dc496c
|
data/.github/workflows/ci.yml
CHANGED
@@ -9,12 +9,12 @@ jobs:
|
|
9
9
|
|
10
10
|
steps:
|
11
11
|
- uses: actions/checkout@v1
|
12
|
-
- name: Set up Ruby 2.
|
13
|
-
uses:
|
12
|
+
- name: Set up Ruby 2.3.8
|
13
|
+
uses: ruby/setup-ruby@v1
|
14
14
|
with:
|
15
|
-
ruby-version: 2.
|
15
|
+
ruby-version: 2.3.8
|
16
16
|
- name: Build and test with Rake
|
17
17
|
run: |
|
18
18
|
gem install bundler
|
19
19
|
bundle install --jobs 4 --retry 3
|
20
|
-
bundle exec
|
20
|
+
bundle exec rspec
|
@@ -15,10 +15,10 @@ jobs:
|
|
15
15
|
|
16
16
|
steps:
|
17
17
|
- uses: actions/checkout@master
|
18
|
-
- name: Set up Ruby 2.
|
19
|
-
uses:
|
18
|
+
- name: Set up Ruby 2.3.8
|
19
|
+
uses: ruby/setup-ruby@v1
|
20
20
|
with:
|
21
|
-
version: 2.
|
21
|
+
version: 2.3.8
|
22
22
|
|
23
23
|
- name: Publish to GPR
|
24
24
|
run: |
|
data/.tool-versions
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby 2.3.8
|
data/README.md
CHANGED
@@ -60,6 +60,45 @@ expect(Protocols::Adder.add(nil, 10)).to eq(10)
|
|
60
60
|
expect(Protocols::Adder.add_default(1)).to eq(6)
|
61
61
|
```
|
62
62
|
|
63
|
+
### Arguments types
|
64
|
+
|
65
|
+
Normally, one would use a simple notation to declare a method. It includes `:this` receiver
|
66
|
+
in te very first position and some optional required arguments afterward.
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
defmethod :add, :this, :addend
|
70
|
+
...
|
71
|
+
defimpl ... do
|
72
|
+
def add(this, addend); this + addend; end
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
If the argument is not generic (optional, or splatted, or keyword,) its type must be explicitly
|
77
|
+
specified in the protocol definition as shown below.
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
defprotocol implicit_inheritance: true do
|
81
|
+
defmethod :with_def_argument, :this, [:foo_opt, :opt]
|
82
|
+
defmethod :with_def_keyword_argument, :this, [:foo_key, :key]
|
83
|
+
defmethod :with_req_keyword_argument, :this, [:foo_key, :keyreq]
|
84
|
+
|
85
|
+
def with_def_argument(this, foo_opt = :super); foo_opt; end
|
86
|
+
def with_def_keyword_argument(this, foo_key: :super); foo_key; end
|
87
|
+
def with_req_keyword_argument(this, foo_key:); foo_key; end
|
88
|
+
...
|
89
|
+
```
|
90
|
+
|
91
|
+
That said, `:addend` argument declaration is a syntactic sugar for `[:addend, :req]`.
|
92
|
+
Possible values for the type are:
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
PARAM_TYPES = %i[req opt rest key keyrest keyreq block]
|
96
|
+
```
|
97
|
+
|
98
|
+
Please note, that the signature of the method and its implementation must exactly match.
|
99
|
+
One cannot declare a method to have a `keyreq` argument and then make it defaulted in the
|
100
|
+
implementation. That is done by design.
|
101
|
+
|
63
102
|
## Guards
|
64
103
|
|
65
104
|
Starting with `v0.5.0` we support multiple function clauses and guards.
|
@@ -54,15 +54,22 @@ module Dry
|
|
54
54
|
end.reject(&:nil?).first
|
55
55
|
end
|
56
56
|
|
57
|
-
BlackTie.protocols[self].each do |method, *
|
58
|
-
singleton_class.send :define_method, method do |receiver
|
57
|
+
BlackTie.protocols[self].each do |method, *_m_args, **_m_kwargs| # FIXME: CHECK ARITY HERE
|
58
|
+
singleton_class.send :define_method, method do |receiver, *args, **kwargs|
|
59
59
|
impl = implementation_for(receiver)
|
60
|
+
|
60
61
|
raise Dry::Protocol::NotImplemented.new(
|
61
62
|
:protocol, inspect,
|
62
63
|
method: method, receiver: receiver, args: args, self: self
|
63
64
|
) unless impl
|
65
|
+
|
64
66
|
begin
|
65
|
-
|
67
|
+
# [AM] [v1] [FIXME] for modern rubies `if` is redundant
|
68
|
+
if kwargs.empty?
|
69
|
+
impl[method].(*args.unshift(receiver))
|
70
|
+
else
|
71
|
+
impl[method].(*args.unshift(receiver), **kwargs)
|
72
|
+
end
|
66
73
|
rescue => e
|
67
74
|
raise Dry::Protocol::NotImplemented.new(
|
68
75
|
:nested, inspect,
|
@@ -89,7 +96,7 @@ module Dry
|
|
89
96
|
BlackTie.Logger.warn(UNKNOWN_TYPE_DECLARATION % [Dry::BlackTie.proto_caller, type, self.inspect, name])
|
90
97
|
type = nil
|
91
98
|
end
|
92
|
-
[type || PARAM_TYPES.include?(p) ? p : :req, p]
|
99
|
+
[type || (PARAM_TYPES.include?(p) ? p : :req), p]
|
93
100
|
end
|
94
101
|
BlackTie.protocols[self][name] = params
|
95
102
|
end
|
@@ -109,8 +116,13 @@ module Dry
|
|
109
116
|
(NORMALIZE_KEYS.(protocol) - meths).each_with_object(meths) do |m, acc|
|
110
117
|
if BlackTie.protocols[protocol][:__implicit_inheritance__]
|
111
118
|
mod.singleton_class.class_eval do
|
112
|
-
define_method m do |this, *♿_args, &♿_λ|
|
113
|
-
|
119
|
+
define_method m do |this, *♿_args, **♿_kwargs, &♿_λ|
|
120
|
+
# [AM] [v1] [FIXME] for modern rubies `if` is redundant
|
121
|
+
if ♿_kwargs.empty?
|
122
|
+
super(this, *♿_args, &♿_λ)
|
123
|
+
else
|
124
|
+
super(this, *♿_args, **♿_kwargs, &♿_λ)
|
125
|
+
end
|
114
126
|
end
|
115
127
|
end
|
116
128
|
else
|
@@ -143,7 +155,7 @@ module Dry
|
|
143
155
|
end
|
144
156
|
module_function :defimpl
|
145
157
|
|
146
|
-
PARAM_TYPES = %i[req opt rest keyrest block]
|
158
|
+
PARAM_TYPES = %i[req opt rest key keyrest keyreq block]
|
147
159
|
|
148
160
|
DELEGATE_METHOD = lambda do |klazz, (source, target)|
|
149
161
|
klazz.class_eval do
|
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.
|
4
|
+
version: 0.12.0
|
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: 2022-07
|
13
|
+
date: 2022-12-07 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|
@@ -111,7 +111,7 @@ files:
|
|
111
111
|
- ".rspec"
|
112
112
|
- ".rubocop.yml"
|
113
113
|
- ".rubocop_todo.yml"
|
114
|
-
- ".
|
114
|
+
- ".tool-versions"
|
115
115
|
- CODE_OF_CONDUCT.md
|
116
116
|
- Gemfile
|
117
117
|
- LICENSE.txt
|
@@ -149,7 +149,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
149
149
|
- !ruby/object:Gem::Version
|
150
150
|
version: '0'
|
151
151
|
requirements: []
|
152
|
-
|
152
|
+
rubyforge_project:
|
153
|
+
rubygems_version: 2.5.2.3
|
153
154
|
signing_key:
|
154
155
|
specification_version: 4
|
155
156
|
summary: Tiny library inspired by Elixir protocol pattern.
|