dry-behaviour 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +38 -2
- data/lib/dry/behaviour/cerberus.rb +5 -2
- data/lib/dry/behaviour/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d3191f3934db794f8482d4abc4854e9fe20667e4
|
4
|
+
data.tar.gz: 68ce5932e3873146760b37b5a542a27b1dcb2579
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ac7997b358b608b996fa5525eb46b1286455bf98fd8ec04c694420ca0d2a5324ea31d2a6115a2b09b57a91c02b907002015b15c4aff654e91049817e183f604c
|
7
|
+
data.tar.gz: 73868bad1e0b42419118f649c6a150259698e9d561ec77573fe8b62d5f0dc22f4b472d829bd86c2e11096497e5e7b29f3eb7ca93fa5861859b5222fb7378c595
|
data/README.md
CHANGED
@@ -4,7 +4,9 @@
|
|
4
4
|
|
5
5
|
**Tiny library inspired by Elixir [`protocol`](http://elixir-lang.org/getting-started/protocols.html) pattern.**
|
6
6
|
|
7
|
-
##
|
7
|
+
## Protocols
|
8
|
+
|
9
|
+
### Declaration
|
8
10
|
|
9
11
|
```ruby
|
10
12
|
require 'dry/behaviour'
|
@@ -45,7 +47,7 @@ module Protocols
|
|
45
47
|
end
|
46
48
|
```
|
47
49
|
|
48
|
-
|
50
|
+
### Usage
|
49
51
|
|
50
52
|
```ruby
|
51
53
|
expect(Protocols::Adder.add(5, 3)).to eq(8)
|
@@ -58,6 +60,40 @@ expect(Protocols::Adder.add(nil, 10)).to eq(10)
|
|
58
60
|
expect(Protocols::Adder.add_default(1)).to eq(6)
|
59
61
|
```
|
60
62
|
|
63
|
+
## Guards
|
64
|
+
|
65
|
+
Starting with `v0.5.0` we support multiple function clauses and guards.
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
class GuardTest
|
69
|
+
include Dry::Guards
|
70
|
+
|
71
|
+
def a(p, p2 = nil, *_a, when: { p: Integer, p2: String }, **_b, &cb); 1; end
|
72
|
+
def a(p, _p2 = nil, *_a, when: { p: Integer }, **_b, &cb); 2; end
|
73
|
+
def a(p, _p2 = nil, *_a, when: { p: Float }, **_b, &cb); 3; end
|
74
|
+
def a(p, _p2 = nil, *_a, when: { p: ->(v) { v < 42 } }, **_b, &cb); 4; end
|
75
|
+
def a(_p, _p2 = nil, *_a, when: { cb: ->(v) { !v.nil? } }, **_b, &cb); 5; end
|
76
|
+
def a(p1, p2, p3); 6; end
|
77
|
+
def a(p, _p2 = nil, *_a, **_b, &cb); 'ALL'; end
|
78
|
+
|
79
|
+
def b(p, &cb)
|
80
|
+
'NOT GUARDED'
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
gt = GuardTest.new
|
85
|
+
|
86
|
+
it 'performs routing to function clauses as by guards' do
|
87
|
+
expect(gt.a(42, 'Hello')).to eq(1)
|
88
|
+
expect(gt.a(42)).to eq(2)
|
89
|
+
expect(gt.a(3.14)).to eq(3)
|
90
|
+
expect(gt.a(3)).to eq(4)
|
91
|
+
# expect(gt.a('Hello', &-> { puts 0 })).to eq(5) NYI
|
92
|
+
expect(gt.a(*%w|1 2 3|)).to eq(6)
|
93
|
+
expect(gt.a('Hello')).to eq('ALL')
|
94
|
+
end
|
95
|
+
```
|
96
|
+
|
61
97
|
## Authors
|
62
98
|
|
63
99
|
@am-kantox, @saverio-kantox & @kantox
|
@@ -93,10 +93,13 @@ module Dry
|
|
93
93
|
end
|
94
94
|
next if hash.nil?
|
95
95
|
hash.all? do |param, condition|
|
96
|
-
idx = m.parameters.
|
96
|
+
type_var, idx = m.parameters.each_with_index.detect { |(_type, var), idx| var == param }
|
97
97
|
# rubocop:disable Style/CaseEquality
|
98
98
|
# rubocop:disable Style/RescueModifier
|
99
|
-
idx &&
|
99
|
+
idx && (case type_var.first
|
100
|
+
when :req, :opt then condition === args[idx] rescue false
|
101
|
+
when :block then condition === cb
|
102
|
+
end)
|
100
103
|
# rubocop:enable Style/RescueModifier
|
101
104
|
# rubocop:enable Style/CaseEquality
|
102
105
|
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.5.
|
4
|
+
version: 0.5.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: 2017-04-
|
13
|
+
date: 2017-04-12 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|
@@ -133,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
133
133
|
version: '0'
|
134
134
|
requirements: []
|
135
135
|
rubyforge_project:
|
136
|
-
rubygems_version: 2.
|
136
|
+
rubygems_version: 2.6.8
|
137
137
|
signing_key:
|
138
138
|
specification_version: 4
|
139
139
|
summary: Tiny library inspired by Elixir protocol pattern.
|