dry-behaviour 0.5.0 → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c27c0695413fb84924f27453989fd85836066d0f
4
- data.tar.gz: 48217fe055d56c59e4842e28f9eddbc597a48a80
3
+ metadata.gz: d3191f3934db794f8482d4abc4854e9fe20667e4
4
+ data.tar.gz: 68ce5932e3873146760b37b5a542a27b1dcb2579
5
5
  SHA512:
6
- metadata.gz: a9e4ca4ed497dd76ed2caf2797d1570f3e7184ae4fe54497b15e84f9a7a3199858c2bfff2cab124a8b3d8d6bbb10617dec3bd397a36ae2580da9b40927f35fb2
7
- data.tar.gz: 38f727f407f90ff7398798410962e08b8d32e95d99fcf1c3eabc6722c1dc6673c4a978a0bb502cf4cea1ec0e7c4960b9c94ba6b373e0df411e972e081268b661
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
- ## Declaration
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
- ## Usage
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.index { |_type, var| var == param }
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 && condition === args[idx] rescue false # FIXME: more accurate
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
@@ -1,5 +1,5 @@
1
1
  module Dry
2
2
  module Behaviour
3
- VERSION = '0.5.0'.freeze
3
+ VERSION = '0.5.1'.freeze
4
4
  end
5
5
  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.0
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-11 00:00:00.000000000 Z
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.4.8
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.