sinclair 1.6.2 → 1.6.3

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
  SHA256:
3
- metadata.gz: 15c8c49649134851cc9f1fb6a92d4ea19ff82cc2e68ce2d09339987b2623a5d4
4
- data.tar.gz: def1468dbdf88b1f700edc6fd172cc6dc06d56b9aba07171cb6d7c3a7bcdc32e
3
+ metadata.gz: e73aaf0289f39dd23b05bbd256c7bbd98de3d5a2a963bddd096315241ad81d6b
4
+ data.tar.gz: ed6e16a5b51ef93cc7bd083c6ae304121df91dfc67ac71b8804cc340c63a002a
5
5
  SHA512:
6
- metadata.gz: fcaf42f0c6ee8b2cdb4986e003b487f1f53184d9486c9479477fcfe4fb38550ff9deecc4b3029da563812179152ecee68c379b5673f60eedfb7df9a4f945b30c
7
- data.tar.gz: 622f77edaeb7d9b247a15c071ecb80981e82d915124dbeb57851c86153c1790bd301ea7be067b9020281dc9aeecd6cec54ec4403a74972bf7f8d9d89cf444020
6
+ metadata.gz: a38a73d5ddca24d0dfacb8c535255a83978f84b0913c940f7eda1e6a5f162fd14c314b063df67d35673c2b83d55fc0d42ed7a8aaa2fbfa7093f0e23e7bef5abc
7
+ data.tar.gz: 1bb0387bd0cc01250985b5b000fac058e34a39b37b352cf89a953ab36fef069fda576121ff59ef0d95ab3521cce2879e0940085d95d4ccd95feb1e169d06da47
data/README.md CHANGED
@@ -15,7 +15,7 @@ methods
15
15
 
16
16
  Yard Documentation
17
17
  -------------------
18
- [https://www.rubydoc.info/gems/sinclair/1.6.2](https://www.rubydoc.info/gems/sinclair/1.6.2)
18
+ [https://www.rubydoc.info/gems/sinclair/1.6.3](https://www.rubydoc.info/gems/sinclair/1.6.3)
19
19
 
20
20
  Installation
21
21
  ---------------
@@ -463,6 +463,8 @@ Options allows projects to have an easy to configure option object
463
463
  options.retries # returns nil
464
464
  options.protocol # returns 'http'
465
465
  options.port # returns 443
466
+
467
+ ConnectionOptions.new(invalid: 10) # raises Sinclair::Exception::InvalidOptions
466
468
  ```
467
469
 
468
470
  RSspec matcher
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'set'
4
+
3
5
  class Sinclair
4
6
  # @api public
5
7
  # @author Darthjee
@@ -21,28 +23,56 @@ class Sinclair
21
23
  autoload :Builder, 'sinclair/options/builder'
22
24
 
23
25
  class << self
26
+ # @api private
27
+ #
28
+ # returns invalid options
29
+ #
30
+ # @return [Array<Symbol>]
31
+ def invalid_options_in(names)
32
+ names.map(&:to_sym) - allowed_options.to_a
33
+ end
34
+
35
+ # @api private
36
+ #
37
+ # Allow new option
38
+ #
39
+ # This does not create the method
40
+ #
41
+ # @param name [String,Symbol] options to be allowed
42
+ #
43
+ # @return [Set<Symbol>]
44
+ def allow(name)
45
+ allowed_options << name.to_sym
46
+ end
47
+
48
+ private
49
+
24
50
  # @api private
25
51
  # @private
26
52
  #
27
53
  # Options allowed when initializing options
28
54
  #
29
- # @return [Array<Symbol>]
55
+ # @return [Set<Symbol>]
30
56
  def allowed_options
31
- @allowed_options ||= (superclass.try(:allowed_options).dup || [])
57
+ @allowed_options ||= build_allowed_options
32
58
  end
33
59
 
34
60
  # @api private
35
61
  # @private
36
62
  #
37
- # returns invalid options
63
+ # Build set of allowed options
38
64
  #
39
- # @return [Array<Symbol>]
40
- def invalid_options_in(names)
41
- names.map(&:to_sym) - allowed_options
65
+ # When class is descendent of {Options}
66
+ # a duplication of it's parents allowed_options is
67
+ # returned
68
+ #
69
+ # @return (see allowed_options)
70
+ def build_allowed_options
71
+ superclass.send(:allowed_options).dup
72
+ rescue NoMethodError
73
+ Set.new
42
74
  end
43
75
 
44
- private
45
-
46
76
  # @api public
47
77
  # @!visibility public
48
78
  #
@@ -57,7 +57,7 @@ class Sinclair
57
57
  def add_all_methods
58
58
  attributes.each do |option, value|
59
59
  add_method(option, cached: :full) { value }
60
- klass.allowed_options.push(option.to_sym)
60
+ klass.allow(option)
61
61
  end
62
62
  end
63
63
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Sinclair
4
- VERSION = '1.6.2'
4
+ VERSION = '1.6.3'
5
5
  end
@@ -15,5 +15,13 @@ describe Sinclair::Options do
15
15
  expect(options.protocol).to eq('http')
16
16
  expect(options.port).to eq(443)
17
17
  end
18
+
19
+ context 'when initialized with invalid options' do
20
+ it do
21
+ expect do
22
+ ConnectionOptions.new(invalid: 10)
23
+ end.to raise_error(Sinclair::Exception::InvalidOptions)
24
+ end
25
+ end
18
26
  end
19
27
  end
@@ -11,6 +11,10 @@ describe Sinclair::Options::Builder do
11
11
  let(:klass) { Class.new(Sinclair::Options) }
12
12
  let(:options) { klass.new }
13
13
 
14
+ let(:test_keys) do
15
+ %i[timeout retries invalid]
16
+ end
17
+
14
18
  context 'when calling with keys' do
15
19
  let(:args) { [:timeout, 'retries'] }
16
20
 
@@ -26,14 +30,14 @@ describe Sinclair::Options::Builder do
26
30
 
27
31
  it do
28
32
  expect { builder.build }
29
- .to change(klass, :allowed_options)
30
- .from([])
31
- .to(%i[timeout retries])
33
+ .to change { klass.invalid_options_in(test_keys) }
34
+ .from(test_keys)
35
+ .to([:invalid])
32
36
  end
33
37
 
34
38
  it do
35
39
  expect { builder.build }
36
- .not_to change(Sinclair::Options, :allowed_options)
40
+ .not_to change { Sinclair::Options.invalid_options_in(test_keys) }
37
41
  end
38
42
 
39
43
  context 'when when calling method after building' do
@@ -67,6 +71,10 @@ describe Sinclair::Options::Builder do
67
71
  [:protocol, { 'port' => 443 }]
68
72
  end
69
73
 
74
+ let(:test_keys) do
75
+ %i[timeout retries protocol port invalid]
76
+ end
77
+
70
78
  let(:super_builder) do
71
79
  described_class.new(super_class, :timeout, 'retries')
72
80
  end
@@ -85,14 +93,14 @@ describe Sinclair::Options::Builder do
85
93
 
86
94
  it do
87
95
  expect { builder.build }
88
- .to change(klass, :allowed_options)
89
- .from(%i[timeout retries])
90
- .to(%i[timeout retries protocol port])
96
+ .to change { klass.invalid_options_in(test_keys) }
97
+ .from(%i[protocol port invalid])
98
+ .to(%i[invalid])
91
99
  end
92
100
 
93
101
  it do
94
102
  expect { builder.build }
95
- .not_to change(super_class, :allowed_options)
103
+ .not_to change { super_class.invalid_options_in(test_keys) }
96
104
  end
97
105
  end
98
106
  end
@@ -6,7 +6,8 @@ describe Sinclair::Options do
6
6
  subject(:options) { klass.new }
7
7
 
8
8
  describe '.with_options' do
9
- let(:klass) { Class.new(described_class) }
9
+ let(:klass) { Class.new(described_class) }
10
+ let(:test_keys) { %i[timeout retries invalid] }
10
11
 
11
12
  context 'when calling with keys' do
12
13
  it 'add first method' do
@@ -19,16 +20,18 @@ describe Sinclair::Options do
19
20
  .to add_method(:retries).to(klass)
20
21
  end
21
22
 
22
- it do
23
+ it 'adds options to allowed' do
23
24
  expect { klass.send(:with_options, :timeout, 'retries') }
24
- .to change(klass, :allowed_options)
25
- .from([])
26
- .to(%i[timeout retries])
25
+ .to change { klass.invalid_options_in(test_keys) }
26
+ .from(%i[timeout retries invalid])
27
+ .to([:invalid])
27
28
  end
28
29
 
29
30
  it do
30
31
  expect { klass.send(:with_options, :timeout, 'retries') }
31
- .not_to change(described_class, :allowed_options)
32
+ .not_to change {
33
+ described_class.invalid_options_in(%i[timeout retries invalid])
34
+ }
32
35
  end
33
36
 
34
37
  context 'when when calling method after building' do
@@ -36,6 +39,17 @@ describe Sinclair::Options do
36
39
 
37
40
  it { expect(options.timeout).to be_nil }
38
41
  end
42
+
43
+ context 'when calling method twice' do
44
+ before { klass.send(:with_options, :timeout, retries: 10) }
45
+
46
+ it do
47
+ expect { klass.send(:with_options, :timeout, :retries) }
48
+ .not_to change {
49
+ klass.invalid_options_in(%i[timeout retries invalid])
50
+ }
51
+ end
52
+ end
39
53
  end
40
54
 
41
55
  context 'when calling with a hash' do
@@ -57,7 +71,11 @@ describe Sinclair::Options do
57
71
  let(:super_class) { Class.new(described_class) }
58
72
  let(:klass) { Class.new(super_class) }
59
73
 
60
- before { super_class.send(:with_options, :timeout, 'retries') }
74
+ let(:test_keys) do
75
+ %i[timeout retries name protocol port invalid]
76
+ end
77
+
78
+ before { super_class.send(:with_options, :timeout, 'retries', name: 'My Connector') }
61
79
 
62
80
  it 'add first method' do
63
81
  expect { klass.send(:with_options, :protocol, 'port' => 443) }
@@ -71,14 +89,110 @@ describe Sinclair::Options do
71
89
 
72
90
  it do
73
91
  expect { klass.send(:with_options, 'protocol', port: 443) }
74
- .to change(klass, :allowed_options)
75
- .from(%i[timeout retries])
76
- .to(%i[timeout retries protocol port])
92
+ .to change {
93
+ klass.invalid_options_in(test_keys)
94
+ }.from(%i[protocol port invalid])
95
+ .to([:invalid])
77
96
  end
78
97
 
79
98
  it do
80
99
  expect { klass.send(:with_options, 'protocol', port: 443) }
81
- .not_to change(super_class, :allowed_options)
100
+ .not_to change {
101
+ super_class.invalid_options_in(%i[protocol port])
102
+ }
103
+ end
104
+
105
+ context 'when overriding a method' do
106
+ it do
107
+ expect { klass.send(:with_options, :name, timeout: 10) }
108
+ .not_to change { klass.invalid_options_in(%i[name timeout]) }
109
+ end
110
+
111
+ it 'change methods to return new default' do
112
+ expect { klass.send(:with_options, :name, timeout: 10) }
113
+ .to change { klass.new.timeout }
114
+ .from(nil).to(10)
115
+ end
116
+
117
+ it 'change methods to return without default' do
118
+ expect { klass.send(:with_options, :name, timeout: 10) }
119
+ .to change { klass.new.name }
120
+ .from('My Connector').to(nil)
121
+ end
122
+ end
123
+ end
124
+ end
125
+
126
+ describe '.invalid_options_in' do
127
+ let(:klass) { Class.new(described_class) }
128
+ let(:test_keys) { %i[timeout invalid] }
129
+
130
+ it 'returns alls keys as invalid' do
131
+ expect(klass.invalid_options_in(test_keys))
132
+ .to eq(test_keys)
133
+ end
134
+
135
+ context 'when allowed options was never set' do
136
+ before { klass.allow(:timeout) }
137
+
138
+ it 'returns keys that are not allowed by the input' do
139
+ expect(klass.invalid_options_in(test_keys))
140
+ .to eq([:invalid])
141
+ end
142
+ end
143
+
144
+ context 'when calling on subclass' do
145
+ let(:super_class) { Class.new(described_class) }
146
+ let(:klass) { Class.new(super_class) }
147
+ let(:test_keys) { %i[timeout invalid] }
148
+
149
+ before { super_class.allow(:timeout) }
150
+
151
+ context 'when not adding allowed options' do
152
+ it 'returns keys that are not allowed by the input' do
153
+ expect(klass.invalid_options_in(test_keys))
154
+ .to eq([:invalid])
155
+ end
156
+ end
157
+
158
+ context 'when adding keys' do
159
+ before { super_class.allow(:retries) }
160
+
161
+ it 'returns keys that are not allowed by the input' do
162
+ expect(klass.invalid_options_in(test_keys))
163
+ .to eq([:invalid])
164
+ end
165
+
166
+ it 'adds new key to accepted' do
167
+ expect(klass.invalid_options_in([:retries]))
168
+ .to be_empty
169
+ end
170
+ end
171
+ end
172
+ end
173
+
174
+ describe '.allow' do
175
+ let(:klass) { Class.new(described_class) }
176
+ let(:test_keys) { %i[timeout retries invalid] }
177
+
178
+ it 'adds options to allowed' do
179
+ expect { klass.allow(:timeout) }
180
+ .to change { klass.invalid_options_in(test_keys) }
181
+ .from(%i[timeout retries invalid])
182
+ .to(%i[retries invalid])
183
+ end
184
+
185
+ context 'when calling on subclass' do
186
+ let(:super_class) { Class.new(described_class) }
187
+ let(:klass) { Class.new(super_class) }
188
+
189
+ before { super_class.allow(:timeout) }
190
+
191
+ it 'adds options to allowed' do
192
+ expect { klass.allow(:retries) }
193
+ .to change { klass.invalid_options_in(test_keys) }
194
+ .from(%i[retries invalid])
195
+ .to(%i[invalid])
82
196
  end
83
197
  end
84
198
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinclair
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.2
4
+ version: 1.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - DarthJee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-02 00:00:00.000000000 Z
11
+ date: 2020-05-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport