sinclair 1.6.5 → 1.6.6
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 +3 -1
- data/lib/sinclair/options.rb +10 -60
- data/lib/sinclair/options/class_methods.rb +99 -0
- data/lib/sinclair/version.rb +1 -1
- data/spec/integration/yard/sinclair/options_parser_spec.rb +9 -0
- data/spec/lib/sinclair/options/class_methods_spec.rb +255 -0
- data/spec/lib/sinclair/options_spec.rb +28 -237
- data/spec/support/models/builder_options.rb +7 -0
- data/spec/support/models/open_options.rb +7 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3c38de6f509924f2b5b98eff6492718504c2f83111360e4985c67e421a2cebc5
|
4
|
+
data.tar.gz: 9210a66214f8fa11a6c42496b00f1045bf2268adde7c049b1481d47b21aba6cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7536c4b9bc1198fd23777b4bd73d20479d0c94d6ef03444e48e4c05687623d0c4f2cdcdb8ad6bfef4a20832307f04021d14bdd628bd3ff2cbf6e1c611c5be7ad
|
7
|
+
data.tar.gz: f0c246aa85bd9989337240ded4aa36634b80dcf1042f8776663b05ebea81220dd13b60f45f8c12ee4aa95064be096094c68d9fe6619d517fa36a879ffe897011
|
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.
|
18
|
+
[https://www.rubydoc.info/gems/sinclair/1.6.6](https://www.rubydoc.info/gems/sinclair/1.6.6)
|
19
19
|
|
20
20
|
Installation
|
21
21
|
---------------
|
@@ -452,6 +452,8 @@ Options allows projects to have an easy to configure option object
|
|
452
452
|
```ruby
|
453
453
|
class ConnectionOptions < Sinclair::Options
|
454
454
|
with_options :timeout, :retries, port: 443, protocol: 'https'
|
455
|
+
|
456
|
+
# skip_validation if you dont want to validate intialization arguments
|
455
457
|
end
|
456
458
|
|
457
459
|
options = ConnectionOptions.new(
|
data/lib/sinclair/options.rb
CHANGED
@@ -20,64 +20,10 @@ class Sinclair
|
|
20
20
|
# options.port # returns 8080
|
21
21
|
# options.protocol # returns 'https'
|
22
22
|
class Options
|
23
|
-
autoload :Builder,
|
23
|
+
autoload :Builder, 'sinclair/options/builder'
|
24
|
+
autoload :ClassMethods, 'sinclair/options/class_methods'
|
24
25
|
|
25
|
-
|
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
|
-
# @api private
|
49
|
-
# @private
|
50
|
-
#
|
51
|
-
# Options allowed when initializing options
|
52
|
-
#
|
53
|
-
# @return [Set<Symbol>]
|
54
|
-
def allowed_options
|
55
|
-
@allowed_options ||= superclass.try(:allowed_options).dup || Set.new
|
56
|
-
end
|
57
|
-
|
58
|
-
private
|
59
|
-
|
60
|
-
# @api public
|
61
|
-
# @!visibility public
|
62
|
-
#
|
63
|
-
# Add available options
|
64
|
-
#
|
65
|
-
# @example (see Options)
|
66
|
-
#
|
67
|
-
# @return (see Sinclair#build)
|
68
|
-
#
|
69
|
-
# @overload with_options(*options)
|
70
|
-
# @param options [Array<Symbol>] list of accepted
|
71
|
-
# options
|
72
|
-
# @overload with_options(*options, **defaults)
|
73
|
-
# @param options [Array<Symbol>] list of accepted
|
74
|
-
# options
|
75
|
-
# @param defaults [Hash<Symbol,Object>] default options
|
76
|
-
# hash
|
77
|
-
def with_options(*options)
|
78
|
-
Builder.new(self, *options).build
|
79
|
-
end
|
80
|
-
end
|
26
|
+
extend ClassMethods
|
81
27
|
|
82
28
|
# @param options [Hash] hash with options (see {.options}, {.with_options})
|
83
29
|
# @example (see Options)
|
@@ -108,7 +54,7 @@ class Sinclair
|
|
108
54
|
# # protocol: 'https'
|
109
55
|
# # }
|
110
56
|
def to_h
|
111
|
-
|
57
|
+
allowed_options.inject({}) do |hash, option|
|
112
58
|
hash.merge(option => public_send(option))
|
113
59
|
end
|
114
60
|
end
|
@@ -121,13 +67,15 @@ class Sinclair
|
|
121
67
|
def ==(other)
|
122
68
|
return false unless self.class == other.class
|
123
69
|
|
124
|
-
|
70
|
+
allowed_options.all? do |name|
|
125
71
|
public_send(name) == other.public_send(name)
|
126
72
|
end
|
127
73
|
end
|
128
74
|
|
129
75
|
private
|
130
76
|
|
77
|
+
delegate :allowed_options, :skip_validation?, :invalid_options_in, to: :class
|
78
|
+
|
131
79
|
# @private
|
132
80
|
# @api private
|
133
81
|
#
|
@@ -137,7 +85,9 @@ class Sinclair
|
|
137
85
|
#
|
138
86
|
# @return [NilClass]
|
139
87
|
def check_options(options)
|
140
|
-
|
88
|
+
return if skip_validation?
|
89
|
+
|
90
|
+
invalid_keys = invalid_options_in(options.keys)
|
141
91
|
|
142
92
|
return if invalid_keys.empty?
|
143
93
|
|
@@ -0,0 +1,99 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'set'
|
4
|
+
|
5
|
+
class Sinclair
|
6
|
+
class Options
|
7
|
+
# Class Methods for {Sinclai::Options}
|
8
|
+
module ClassMethods
|
9
|
+
# @api private
|
10
|
+
#
|
11
|
+
# returns invalid options
|
12
|
+
#
|
13
|
+
# @return [Array<Symbol>]
|
14
|
+
def invalid_options_in(names)
|
15
|
+
names.map(&:to_sym) - allowed_options.to_a
|
16
|
+
end
|
17
|
+
|
18
|
+
# @api private
|
19
|
+
#
|
20
|
+
# Allow new option
|
21
|
+
#
|
22
|
+
# This does not create the method
|
23
|
+
#
|
24
|
+
# @param name [String,Symbol] options to be allowed
|
25
|
+
#
|
26
|
+
# @return [Set<Symbol>]
|
27
|
+
def allow(name)
|
28
|
+
allowed_options << name.to_sym
|
29
|
+
end
|
30
|
+
|
31
|
+
# @api private
|
32
|
+
# @private
|
33
|
+
#
|
34
|
+
# Options allowed when initializing options
|
35
|
+
#
|
36
|
+
# @return [Set<Symbol>]
|
37
|
+
def allowed_options
|
38
|
+
@allowed_options ||= superclass.try(:allowed_options).dup || Set.new
|
39
|
+
end
|
40
|
+
|
41
|
+
# @api private
|
42
|
+
#
|
43
|
+
# checks if class skips initialization validation
|
44
|
+
#
|
45
|
+
# @return [TrueClass,FalseClass]
|
46
|
+
def skip_validation?
|
47
|
+
@skip_validation ||= superclass.try(:skip_validation?) || false
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
# @api public
|
53
|
+
# @!visibility public
|
54
|
+
#
|
55
|
+
# Add available options
|
56
|
+
#
|
57
|
+
# @example (see Options)
|
58
|
+
#
|
59
|
+
# @return (see Sinclair#build)
|
60
|
+
#
|
61
|
+
# @overload with_options(*options)
|
62
|
+
# @param options [Array<Symbol>] list of accepted
|
63
|
+
# options
|
64
|
+
# @overload with_options(*options, **defaults)
|
65
|
+
# @param options [Array<Symbol>] list of accepted
|
66
|
+
# options
|
67
|
+
# @param defaults [Hash<Symbol,Object>] default options
|
68
|
+
# hash
|
69
|
+
def with_options(*options)
|
70
|
+
Builder.new(self, *options).build
|
71
|
+
end
|
72
|
+
|
73
|
+
# @api public
|
74
|
+
# @!visibility public
|
75
|
+
#
|
76
|
+
# Changes class to skip attributes validation
|
77
|
+
#
|
78
|
+
# when initializing options, options
|
79
|
+
# will accept any arguments when validation
|
80
|
+
# is skipped
|
81
|
+
#
|
82
|
+
# @return [TrueClass]
|
83
|
+
#
|
84
|
+
# @example
|
85
|
+
# class BuilderOptions < Sinclair::Options
|
86
|
+
# with_options :name
|
87
|
+
#
|
88
|
+
# skip_validation
|
89
|
+
# end
|
90
|
+
# options = BuilderOptions.new(name: 'Joe', age: 10)
|
91
|
+
#
|
92
|
+
# options.name # returns 'Joe'
|
93
|
+
# options.try(:age) # returns nil
|
94
|
+
def skip_validation
|
95
|
+
@skip_validation = true
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
data/lib/sinclair/version.rb
CHANGED
@@ -13,5 +13,14 @@ describe Sinclair::OptionsParser do
|
|
13
13
|
expect(model.the_method).to eq('The value is not 10 but 20')
|
14
14
|
end
|
15
15
|
end
|
16
|
+
|
17
|
+
describe '.skip_validation' do
|
18
|
+
it 'accepts options' do
|
19
|
+
options = BuilderOptions.new(name: 'Joe', age: 10)
|
20
|
+
|
21
|
+
expect(options.name).to eq('Joe')
|
22
|
+
expect(options.try(:age)).to be_nil
|
23
|
+
end
|
24
|
+
end
|
16
25
|
end
|
17
26
|
end
|
@@ -0,0 +1,255 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Sinclair::Options::ClassMethods do
|
6
|
+
subject(:options) { klass.new }
|
7
|
+
|
8
|
+
describe '.with_options' do
|
9
|
+
let(:klass) { Class.new(Sinclair::Options) }
|
10
|
+
let(:test_keys) { %i[timeout retries invalid] }
|
11
|
+
|
12
|
+
context 'when calling with keys' do
|
13
|
+
it 'add first method' do
|
14
|
+
expect { klass.send(:with_options, :timeout, 'retries') }
|
15
|
+
.to add_method(:timeout).to(klass)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'add second method' do
|
19
|
+
expect { klass.send(:with_options, :timeout, 'retries') }
|
20
|
+
.to add_method(:retries).to(klass)
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'adds options to allowed' do
|
24
|
+
expect { klass.send(:with_options, :timeout, 'retries') }
|
25
|
+
.to change { klass.invalid_options_in(test_keys) }
|
26
|
+
.from(%i[timeout retries invalid])
|
27
|
+
.to([:invalid])
|
28
|
+
end
|
29
|
+
|
30
|
+
it do
|
31
|
+
expect { klass.send(:with_options, :timeout, 'retries') }
|
32
|
+
.not_to change {
|
33
|
+
Sinclair::Options.invalid_options_in(%i[timeout retries invalid])
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'when when calling method after building' do
|
38
|
+
before { klass.send(:with_options, :timeout, 'retries') }
|
39
|
+
|
40
|
+
it { expect(options.timeout).to be_nil }
|
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
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'when calling with a hash' do
|
56
|
+
it 'adds method' do
|
57
|
+
expect { klass.send(:with_options, timeout_sec: 10, 'retries' => 20) }
|
58
|
+
.to add_method(:timeout_sec).to(klass)
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'when when calling method after building' do
|
62
|
+
before { klass.send(:with_options, timeout_sec: 10, 'retries' => 20) }
|
63
|
+
|
64
|
+
it 'returns default value' do
|
65
|
+
expect(options.retries).to eq(20)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
context 'when calling on subclass' do
|
71
|
+
let(:super_class) { Class.new(Sinclair::Options) }
|
72
|
+
let(:klass) { Class.new(super_class) }
|
73
|
+
|
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') }
|
79
|
+
|
80
|
+
it 'add first method' do
|
81
|
+
expect { klass.send(:with_options, :protocol, 'port' => 443) }
|
82
|
+
.to add_method(:protocol).to(klass)
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'add second method' do
|
86
|
+
expect { klass.send(:with_options, :protocol, 'port' => 443) }
|
87
|
+
.to add_method(:port).to(klass)
|
88
|
+
end
|
89
|
+
|
90
|
+
it do
|
91
|
+
expect { klass.send(:with_options, 'protocol', port: 443) }
|
92
|
+
.to change {
|
93
|
+
klass.invalid_options_in(test_keys)
|
94
|
+
}.from(%i[protocol port invalid])
|
95
|
+
.to([:invalid])
|
96
|
+
end
|
97
|
+
|
98
|
+
it do
|
99
|
+
expect { klass.send(:with_options, 'protocol', port: 443) }
|
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(Sinclair::Options) }
|
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(Sinclair::Options) }
|
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(Sinclair::Options) }
|
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(Sinclair::Options) }
|
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])
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
describe '.allowed_options' do
|
201
|
+
let(:klass) { Class.new(Sinclair::Options) }
|
202
|
+
|
203
|
+
context 'when class has not been changed' do
|
204
|
+
it { expect(klass.allowed_options).to be_a(Set) }
|
205
|
+
end
|
206
|
+
|
207
|
+
context 'when initializing with with options' do
|
208
|
+
let(:expected) do
|
209
|
+
Set.new %i[timeout retries port protocol]
|
210
|
+
end
|
211
|
+
|
212
|
+
before do
|
213
|
+
klass.send(
|
214
|
+
:with_options,
|
215
|
+
:timeout, :retries, port: 443, protocol: 'https'
|
216
|
+
)
|
217
|
+
end
|
218
|
+
|
219
|
+
it do
|
220
|
+
expect(klass.allowed_options)
|
221
|
+
.to eq(expected)
|
222
|
+
end
|
223
|
+
|
224
|
+
context 'when class is descendent' do
|
225
|
+
let(:descendent_class) { Class.new(klass) }
|
226
|
+
let(:expected) do
|
227
|
+
Set.new %i[timeout retries port protocol name]
|
228
|
+
end
|
229
|
+
|
230
|
+
before do
|
231
|
+
descendent_class.send(
|
232
|
+
:with_options,
|
233
|
+
:name
|
234
|
+
)
|
235
|
+
end
|
236
|
+
|
237
|
+
it do
|
238
|
+
expect(descendent_class.allowed_options)
|
239
|
+
.to eq(expected)
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
describe '.skip_validation' do
|
246
|
+
let(:klass) { Class.new(Sinclair::Options) }
|
247
|
+
|
248
|
+
it 'skip initialization validation' do
|
249
|
+
klass.send(:skip_validation)
|
250
|
+
|
251
|
+
expect { klass.new(invalid: 10) }
|
252
|
+
.not_to raise_error
|
253
|
+
end
|
254
|
+
end
|
255
|
+
end
|
@@ -5,243 +5,6 @@ require 'spec_helper'
|
|
5
5
|
describe Sinclair::Options do
|
6
6
|
subject(:options) { klass.new }
|
7
7
|
|
8
|
-
describe '.with_options' do
|
9
|
-
let(:klass) { Class.new(described_class) }
|
10
|
-
let(:test_keys) { %i[timeout retries invalid] }
|
11
|
-
|
12
|
-
context 'when calling with keys' do
|
13
|
-
it 'add first method' do
|
14
|
-
expect { klass.send(:with_options, :timeout, 'retries') }
|
15
|
-
.to add_method(:timeout).to(klass)
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'add second method' do
|
19
|
-
expect { klass.send(:with_options, :timeout, 'retries') }
|
20
|
-
.to add_method(:retries).to(klass)
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'adds options to allowed' do
|
24
|
-
expect { klass.send(:with_options, :timeout, 'retries') }
|
25
|
-
.to change { klass.invalid_options_in(test_keys) }
|
26
|
-
.from(%i[timeout retries invalid])
|
27
|
-
.to([:invalid])
|
28
|
-
end
|
29
|
-
|
30
|
-
it do
|
31
|
-
expect { klass.send(:with_options, :timeout, 'retries') }
|
32
|
-
.not_to change {
|
33
|
-
described_class.invalid_options_in(%i[timeout retries invalid])
|
34
|
-
}
|
35
|
-
end
|
36
|
-
|
37
|
-
context 'when when calling method after building' do
|
38
|
-
before { klass.send(:with_options, :timeout, 'retries') }
|
39
|
-
|
40
|
-
it { expect(options.timeout).to be_nil }
|
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
|
53
|
-
end
|
54
|
-
|
55
|
-
context 'when calling with a hash' do
|
56
|
-
it 'adds method' do
|
57
|
-
expect { klass.send(:with_options, timeout_sec: 10, 'retries' => 20) }
|
58
|
-
.to add_method(:timeout_sec).to(klass)
|
59
|
-
end
|
60
|
-
|
61
|
-
context 'when when calling method after building' do
|
62
|
-
before { klass.send(:with_options, timeout_sec: 10, 'retries' => 20) }
|
63
|
-
|
64
|
-
it 'returns default value' do
|
65
|
-
expect(options.retries).to eq(20)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
context 'when calling on subclass' do
|
71
|
-
let(:super_class) { Class.new(described_class) }
|
72
|
-
let(:klass) { Class.new(super_class) }
|
73
|
-
|
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') }
|
79
|
-
|
80
|
-
it 'add first method' do
|
81
|
-
expect { klass.send(:with_options, :protocol, 'port' => 443) }
|
82
|
-
.to add_method(:protocol).to(klass)
|
83
|
-
end
|
84
|
-
|
85
|
-
it 'add second method' do
|
86
|
-
expect { klass.send(:with_options, :protocol, 'port' => 443) }
|
87
|
-
.to add_method(:port).to(klass)
|
88
|
-
end
|
89
|
-
|
90
|
-
it do
|
91
|
-
expect { klass.send(:with_options, 'protocol', port: 443) }
|
92
|
-
.to change {
|
93
|
-
klass.invalid_options_in(test_keys)
|
94
|
-
}.from(%i[protocol port invalid])
|
95
|
-
.to([:invalid])
|
96
|
-
end
|
97
|
-
|
98
|
-
it do
|
99
|
-
expect { klass.send(:with_options, 'protocol', port: 443) }
|
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])
|
196
|
-
end
|
197
|
-
end
|
198
|
-
end
|
199
|
-
|
200
|
-
describe '.allowed_options' do
|
201
|
-
let(:klass) { Class.new(described_class) }
|
202
|
-
|
203
|
-
context 'when class has not been changed' do
|
204
|
-
it { expect(klass.allowed_options).to be_a(Set) }
|
205
|
-
end
|
206
|
-
|
207
|
-
context 'when initializing with with options' do
|
208
|
-
let(:expected) do
|
209
|
-
Set.new %i[timeout retries port protocol]
|
210
|
-
end
|
211
|
-
|
212
|
-
before do
|
213
|
-
klass.send(
|
214
|
-
:with_options,
|
215
|
-
:timeout, :retries, port: 443, protocol: 'https'
|
216
|
-
)
|
217
|
-
end
|
218
|
-
|
219
|
-
it do
|
220
|
-
expect(klass.allowed_options)
|
221
|
-
.to eq(expected)
|
222
|
-
end
|
223
|
-
|
224
|
-
context 'when class is descendent' do
|
225
|
-
let(:descendent_class) { Class.new(klass) }
|
226
|
-
let(:expected) do
|
227
|
-
Set.new %i[timeout retries port protocol name]
|
228
|
-
end
|
229
|
-
|
230
|
-
before do
|
231
|
-
descendent_class.send(
|
232
|
-
:with_options,
|
233
|
-
:name
|
234
|
-
)
|
235
|
-
end
|
236
|
-
|
237
|
-
it do
|
238
|
-
expect(descendent_class.allowed_options)
|
239
|
-
.to eq(expected)
|
240
|
-
end
|
241
|
-
end
|
242
|
-
end
|
243
|
-
end
|
244
|
-
|
245
8
|
describe '#initialize' do
|
246
9
|
let(:klass) { ConnectionOptions }
|
247
10
|
|
@@ -323,6 +86,34 @@ describe Sinclair::Options do
|
|
323
86
|
end
|
324
87
|
end
|
325
88
|
|
89
|
+
context 'when initializing with invalid args a class that skips validation' do
|
90
|
+
let(:klass) { OpenOptions }
|
91
|
+
|
92
|
+
it do
|
93
|
+
expect { klass.new(valid_option: 20, invalid: 10) }
|
94
|
+
.not_to raise_error
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'initialize option' do
|
98
|
+
expect(klass.new(valid_option: 20, invalid: 10).valid_option)
|
99
|
+
.to eq(20)
|
100
|
+
end
|
101
|
+
|
102
|
+
context 'when initializing a subclass' do
|
103
|
+
let(:klass) { Class.new(OpenOptions) }
|
104
|
+
|
105
|
+
it do
|
106
|
+
expect { klass.new(valid_option: 20, invalid: 10) }
|
107
|
+
.not_to raise_error
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'initialize option' do
|
111
|
+
expect(klass.new(valid_option: 20, invalid: 10).valid_option)
|
112
|
+
.to eq(20)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
326
117
|
context 'when initializing with string or symbol keys' do
|
327
118
|
it do
|
328
119
|
expect { klass.new('timeout' => 20, retries: 30) }
|
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.
|
4
|
+
version: 1.6.6
|
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-
|
11
|
+
date: 2020-05-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -300,6 +300,7 @@ files:
|
|
300
300
|
- lib/sinclair/method_definitions.rb
|
301
301
|
- lib/sinclair/options.rb
|
302
302
|
- lib/sinclair/options/builder.rb
|
303
|
+
- lib/sinclair/options/class_methods.rb
|
303
304
|
- lib/sinclair/options_parser.rb
|
304
305
|
- lib/sinclair/version.rb
|
305
306
|
- sinclair.gemspec
|
@@ -350,6 +351,7 @@ files:
|
|
350
351
|
- spec/lib/sinclair/method_definition_spec.rb
|
351
352
|
- spec/lib/sinclair/method_definitions_spec.rb
|
352
353
|
- spec/lib/sinclair/options/builder_spec.rb
|
354
|
+
- spec/lib/sinclair/options/class_methods_spec.rb
|
353
355
|
- spec/lib/sinclair/options_parser_spec.rb
|
354
356
|
- spec/lib/sinclair/options_spec.rb
|
355
357
|
- spec/lib/sinclair_spec.rb
|
@@ -357,6 +359,7 @@ files:
|
|
357
359
|
- spec/support/fixture_helpers.rb
|
358
360
|
- spec/support/models/app_client.rb
|
359
361
|
- spec/support/models/app_config.rb
|
362
|
+
- spec/support/models/builder_options.rb
|
360
363
|
- spec/support/models/client.rb
|
361
364
|
- spec/support/models/connection_options.rb
|
362
365
|
- spec/support/models/default_value.rb
|
@@ -381,6 +384,7 @@ files:
|
|
381
384
|
- spec/support/models/my_configurable.rb
|
382
385
|
- spec/support/models/my_model.rb
|
383
386
|
- spec/support/models/my_server_config.rb
|
387
|
+
- spec/support/models/open_options.rb
|
384
388
|
- spec/support/models/person.rb
|
385
389
|
- spec/support/models/purchase.rb
|
386
390
|
- spec/support/models/random_generator.rb
|