sinclair 1.3.2 → 1.3.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: 5a99344876ea7ff46dc785f5d9a9daddedce4fa84fb1d33c1387f333ef2611f8
4
- data.tar.gz: 2687f7ebd7816a13275d9aede938dff46731edde1dba1b2e0a272f9e0967af18
3
+ metadata.gz: 46aae0be813637d70c6060f5ac9a14537d19eb2d5e4275a31e864625b396c038
4
+ data.tar.gz: '08f41f83267f7f017b036e4fd806e336a17932ae4f93eadae0e08dd4341c51cb'
5
5
  SHA512:
6
- metadata.gz: 8b6278abe2c8f3b0ec4f20cc9856508cf7f5726643229d532c075e5a142cca02141f5e3e8b8d99bea25859dd2a4b072b2fb457aa0b682f4a25949f132c2715e4
7
- data.tar.gz: d0e6cae7c4e3d1f87c99f737066292cdee42ee6b14c00ce7b1a6cb1eb20f05dd73a776e5cd75c3ac2d6ffb7ab1a91a67f57d5d0d00385238aa57ab84dbc6d7bb
6
+ metadata.gz: 61deac1e3f21b5f238f33fd900ffadacd478b48cdaa3387ac7c272c28edb6d810af1609ec0d76b670861339c9de13600c8c39538c5be0199d5175fe6ed899434
7
+ data.tar.gz: 3681a6b3f56c9380e6bcc19bde499c0f4afc593075d8e23144ecc3d0168f0c89bf5352f351b1b87930fc2ba937f313a94292498157e75e6e4e7d56b12bd2efac
data/README.md CHANGED
@@ -14,7 +14,7 @@ methods
14
14
 
15
15
  Yard Documentation
16
16
  -------------------
17
- https://www.rubydoc.info/gems/sinclair/1.3.2
17
+ https://www.rubydoc.info/gems/sinclair/1.3.3
18
18
 
19
19
  Installation
20
20
  ---------------
@@ -332,6 +332,38 @@ the `configurable#configure` method
332
332
  MyConfigurable.config.port # returns 80
333
333
  ```
334
334
 
335
+ Configurations can also be done through custom classes
336
+
337
+ ```ruby
338
+ class MyServerConfig
339
+ def url
340
+ if @port
341
+ "http://#{@host}:#{@port}"
342
+ else
343
+ "http://#{@host}"
344
+ end
345
+ end
346
+ end
347
+
348
+ class Client
349
+ extend Sinclair::Configurable
350
+
351
+ configurable_by MyServerConfig, with: %i[host port]
352
+ end
353
+
354
+ Client.configure do
355
+ host 'interstella.com'
356
+ end
357
+
358
+ Client.config.url # returns 'http://interstella.com'
359
+
360
+ Client.configure do |config|
361
+ config.port 8080
362
+ end
363
+
364
+ Client.config.url # returns 'http://interstella.com:8080'
365
+ ```
366
+
335
367
  RSspec matcher
336
368
  ---------------
337
369
 
@@ -25,6 +25,8 @@ class Sinclair
25
25
  @config_attributes = config_attributes.dup
26
26
  end
27
27
 
28
+ # @api public
29
+ #
28
30
  # Returns current instance of config
29
31
  #
30
32
  # the method returns the same instance until +reset_config+
@@ -41,6 +43,8 @@ class Sinclair
41
43
  @config ||= config_class.new
42
44
  end
43
45
 
46
+ # @api public
47
+ #
44
48
  # Cleans the current config instance
45
49
  #
46
50
  # After cleaning it, {#config} will generate a new
@@ -87,6 +91,8 @@ class Sinclair
87
91
  config_attributes.concat(builder.config_names.map(&:to_sym))
88
92
  end
89
93
 
94
+ # @api public
95
+ #
90
96
  # Set the values in the config
91
97
  #
92
98
  # The block given is evaluated by the {ConfigBuilder}
@@ -8,37 +8,14 @@ class Sinclair
8
8
  # By extending Configurable, class receives the methods public
9
9
  # {ConfigFactory#config .config}, {ConfigFactory#reset_config .reset_config}
10
10
  # and {ConfigFactory#configure .configure}
11
- # and the private {#configurable_with .configurable_with}
11
+ # and the private methods {#configurable_with .configurable_with}
12
+ # and {#configurable_by .configurable_by}
12
13
  #
13
14
  # @see ConfigFactory
14
15
  # @see ConfigBuilder
15
16
  #
16
- # @example
17
- # class MyConfigurable
18
- # extend Sinclair::Configurable
19
- #
20
- # # port is defaulted to 80
21
- # configurable_with :host, port: 80
22
- # end
23
- #
24
- # MyConfigurable.configure do
25
- # host 'interstella.com'
26
- # port 5555
27
- # end
28
- #
29
- # MyConfigurable.config.host
30
- # # returns 'interstella.com'
31
- #
32
- # MyConfigurable.config.port
33
- # # returns 5555
34
- #
35
- # MyConfigurable.reset_config
36
- #
37
- # MyConfigurable.config.host
38
- # # returns nil
39
- #
40
- # MyConfigurable.config.port
41
- # # returns 80
17
+ # @example (see #configurable_with)
18
+ # @example (see #configurable_by)
42
19
  module Configurable
43
20
  # (see ConfigFactory#config)
44
21
  # @see ConfigFactory#config
@@ -93,9 +70,84 @@ class Sinclair
93
70
  #
94
71
  # @see ConfigFactory#add_configs
95
72
  #
96
- # @example (see Configurable)
73
+ # @example Configuring with common {Sinclair::Config} class
74
+ # class MyConfigurable
75
+ # extend Sinclair::Configurable
76
+ #
77
+ # # port is defaulted to 80
78
+ # configurable_with :host, port: 80
79
+ # end
80
+ #
81
+ # MyConfigurable.configure do
82
+ # host 'interstella.com'
83
+ # port 5555
84
+ # end
85
+ #
86
+ # MyConfigurable.config.host
87
+ # # returns 'interstella.com'
88
+ #
89
+ # MyConfigurable.config.port
90
+ # # returns 5555
91
+ #
92
+ # MyConfigurable.reset_config
93
+ #
94
+ # MyConfigurable.config.host
95
+ # # returns nil
96
+ #
97
+ # MyConfigurable.config.port
98
+ # # returns 80
97
99
  def configurable_with(*attributes)
98
100
  config_factory.add_configs(*attributes)
99
101
  end
102
+
103
+ # @!visibility public
104
+ #
105
+ # Allows configuration to happen through custom config class
106
+ #
107
+ # @param config_class [Class] custom configuration class
108
+ # @param with [Array<Symbol,String>] List of all
109
+ # configuration attributes expected to be found on
110
+ # +config_class+
111
+ #
112
+ # configurable_with does not add methods to config_class.
113
+ # If needed, those can be added by a subsequent call to
114
+ # {#configurable_with}
115
+ #
116
+ # @return [ConfigFactory]
117
+ #
118
+ # @example Configured by custom config class
119
+ # class MyServerConfig
120
+ # def url
121
+ # if @port
122
+ # "http://#{@host}:#{@port}"
123
+ # else
124
+ # "http://#{@host}"
125
+ # end
126
+ # end
127
+ # end
128
+ #
129
+ # class Client
130
+ # extend Sinclair::Configurable
131
+ #
132
+ # configurable_by MyServerConfig, with: %i[host port]
133
+ # end
134
+ #
135
+ # Client.configure do
136
+ # host 'interstella.com'
137
+ # end
138
+ #
139
+ # Client.config.url # returns 'http://interstella.com'
140
+ #
141
+ # Client.configure do |config|
142
+ # config.port 8080
143
+ # end
144
+ #
145
+ # Client.config.url # returns 'http://interstella.com:8080'
146
+ def configurable_by(config_class, with: [])
147
+ @config_factory = ConfigFactory.new(
148
+ config_class: config_class,
149
+ config_attributes: with.map(&:to_sym)
150
+ )
151
+ end
100
152
  end
101
153
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Sinclair
4
- VERSION = '1.3.2'
4
+ VERSION = '1.3.3'
5
5
  end
@@ -4,7 +4,7 @@ DIFF_LIST=$(git diff --name-only $CIRCLE_SHA1 $(git merge-base $CIRCLE_SHA1 orig
4
4
 
5
5
  if [ ! -z "$DIFF_LIST" ]; then
6
6
  mkdir -p tmp/rubycritic/compare
7
- bundle exec rubycritic --format console --branch origin/master -t 0 $DIFF_LIST
7
+ bundle exec rubycritic --format console --branch origin/master -t 0 --maximum-decrease 1 $DIFF_LIST
8
8
  else
9
9
  echo "No changes detected. Skipping rubycritic..."
10
10
  fi
@@ -3,40 +3,72 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe Sinclair::Configurable do
6
- context 'when it is configured' do
7
- before do
8
- MyConfigurable.configure do |config|
9
- config.host 'interstella.art'
10
- config.port 5555
6
+ describe 'README' do
7
+ describe 'Configured with' do
8
+ before do
9
+ MyConfigurable.configure do |config|
10
+ config.host 'interstella.art'
11
+ config.port 5555
12
+ end
11
13
  end
12
- end
13
14
 
14
- after do
15
- MyConfigurable.reset_config
16
- end
15
+ after do
16
+ MyConfigurable.reset_config
17
+ end
17
18
 
18
- it 'sets configuration host' do
19
- expect(MyConfigurable.config.host)
20
- .to eq('interstella.art')
21
- end
19
+ it 'sets configuration host' do
20
+ expect(MyConfigurable.config.host)
21
+ .to eq('interstella.art')
22
+ end
23
+
24
+ it 'sets configuration port' do
25
+ expect(MyConfigurable.config.port)
26
+ .to eq(5555)
27
+ end
28
+
29
+ context 'when #rest_config is called' do
30
+ before do
31
+ MyConfigurable.reset_config
32
+ end
33
+
34
+ it 'resets configuration host' do
35
+ expect(MyConfigurable.config.host)
36
+ .to be_nil
37
+ end
22
38
 
23
- it 'sets configuration port' do
24
- expect(MyConfigurable.config.port)
25
- .to eq(5555)
39
+ it 'resets configuration port' do
40
+ expect(MyConfigurable.config.port).to eq(80)
41
+ end
42
+ end
26
43
  end
27
44
 
28
- context 'when #rest_config is called' do
45
+ describe 'Configured by' do
29
46
  before do
30
- MyConfigurable.reset_config
47
+ Client.configure do
48
+ host 'interstella.com'
49
+ end
31
50
  end
32
51
 
33
- it 'resets configuration host' do
34
- expect(MyConfigurable.config.host)
35
- .to be_nil
52
+ after do
53
+ Client.reset_config
54
+ end
55
+
56
+ it 'sets host' do
57
+ expect(Client.config.url)
58
+ .to eq('http://interstella.com')
36
59
  end
37
60
 
38
- it 'resets configuration port' do
39
- expect(MyConfigurable.config.port).to eq(80)
61
+ context 'when setting the port' do
62
+ before do
63
+ Client.configure do |config|
64
+ config.port 8080
65
+ end
66
+ end
67
+
68
+ it 'sets host and port' do
69
+ expect(Client.config.url)
70
+ .to eq('http://interstella.com:8080')
71
+ end
40
72
  end
41
73
  end
42
74
  end
@@ -1,37 +1,71 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'spec_helper'
4
+
3
5
  describe Sinclair::Configurable do
4
6
  describe '#yard' do
5
- before do
6
- MyConfigurable.configure do
7
- host 'interstella.com'
8
- port 5555
7
+ describe '#configurable_with' do
8
+ before do
9
+ MyConfigurable.configure do
10
+ host 'interstella.com'
11
+ port 5555
12
+ end
9
13
  end
10
- end
11
14
 
12
- after do
13
- MyConfigurable.reset_config
14
- end
15
+ after do
16
+ MyConfigurable.reset_config
17
+ end
15
18
 
16
- it 'sets right value for config host' do
17
- expect(MyConfigurable.config.host)
18
- .to eq('interstella.com')
19
- end
19
+ it 'sets right value for config host' do
20
+ expect(MyConfigurable.config.host)
21
+ .to eq('interstella.com')
22
+ end
23
+
24
+ it 'sets right value for config port' do
25
+ expect(MyConfigurable.config.port)
26
+ .to eq(5555)
27
+ end
20
28
 
21
- it 'sets right value for config port' do
22
- expect(MyConfigurable.config.port)
23
- .to eq(5555)
29
+ context 'when reset_config is called' do
30
+ before { MyConfigurable.reset_config }
31
+
32
+ it 'returns initial value for host' do
33
+ expect(MyConfigurable.config.host).to be_nil
34
+ end
35
+
36
+ it 'returns initial value for port' do
37
+ expect(MyConfigurable.config.port).to eq(80)
38
+ end
39
+ end
24
40
  end
25
41
 
26
- context 'when reset_config is called' do
27
- before { MyConfigurable.reset_config }
42
+ describe '#configurable_by' do
43
+ before do
44
+ Client.configure do
45
+ host 'interstella.com'
46
+ end
47
+ end
28
48
 
29
- it 'returns initial value for host' do
30
- expect(MyConfigurable.config.host).to be_nil
49
+ after do
50
+ Client.reset_config
31
51
  end
32
52
 
33
- it 'returns initial value for port' do
34
- expect(MyConfigurable.config.port).to eq(80)
53
+ it 'sets host' do
54
+ expect(Client.config.url)
55
+ .to eq('http://interstella.com')
56
+ end
57
+
58
+ context 'when setting the port' do
59
+ before do
60
+ Client.configure do |config|
61
+ config.port 8080
62
+ end
63
+ end
64
+
65
+ it 'sets host and port' do
66
+ expect(Client.config.url)
67
+ .to eq('http://interstella.com:8080')
68
+ end
35
69
  end
36
70
  end
37
71
  end
@@ -42,6 +42,11 @@ describe Sinclair::Configurable do
42
42
  end
43
43
 
44
44
  describe '.configurable_with' do
45
+ it do
46
+ expect(configurable.send(:configurable_with, :name, 'host'))
47
+ .to all(be_a(Symbol))
48
+ end
49
+
45
50
  it 'adds reader to config' do
46
51
  expect { configurable.send(:configurable_with, :name) }
47
52
  .to add_method(:name).to(configurable.config)
@@ -70,6 +75,64 @@ describe Sinclair::Configurable do
70
75
  end
71
76
  end
72
77
 
78
+ describe '#configurable_by' do
79
+ let(:config_class) { ServerConfig }
80
+
81
+ it do
82
+ expect(configurable.send(:configurable_by, config_class))
83
+ .to be_a(Sinclair::ConfigFactory)
84
+ end
85
+
86
+ it 'changes config class' do
87
+ expect { configurable.send(:configurable_by, config_class) }
88
+ .to change { configurable.config.class }
89
+ .to(ServerConfig)
90
+ end
91
+
92
+ context 'when config attributes are not given' do
93
+ it 'raises error with any configuration' do
94
+ expect { configurable.configure { host 'myhost' } }
95
+ .to raise_error(NoMethodError)
96
+ end
97
+ end
98
+
99
+ context 'when config attributes are given' do
100
+ let(:attributes) { [:host, 'port'] }
101
+
102
+ let(:block) do
103
+ proc do
104
+ configurable.send(
105
+ :configurable_by, config_class, with: attributes
106
+ )
107
+ end
108
+ end
109
+
110
+ it 'does not add symbol methods config object' do
111
+ expect(&block)
112
+ .not_to add_method(:host).to(configurable.config)
113
+ end
114
+
115
+ it 'does not add string methods config object' do
116
+ expect(&block)
117
+ .not_to add_method(:port).to(configurable.config)
118
+ end
119
+
120
+ it 'does not raises error on configuration of given symbol attributes' do
121
+ block.call
122
+
123
+ expect { configurable.configure { host 'myhost' } }
124
+ .not_to raise_error
125
+ end
126
+
127
+ it 'does not raises error on configuration of given string attributes' do
128
+ block.call
129
+
130
+ expect { configurable.configure { port 90 } }
131
+ .not_to raise_error
132
+ end
133
+ end
134
+ end
135
+
73
136
  describe '.configure' do
74
137
  let(:config) { configurable.config }
75
138
 
@@ -93,5 +156,43 @@ describe Sinclair::Configurable do
93
156
  .to raise_error(NoMethodError)
94
157
  end
95
158
  end
159
+
160
+ context 'when it was configured by custom class' do
161
+ let(:config_class) { ServerConfig }
162
+
163
+ before do
164
+ configurable.send(
165
+ :configurable_by, config_class, with: [:host, 'port']
166
+ )
167
+ end
168
+
169
+ context 'when config class has the methods' do
170
+ it do
171
+ expect { configurable.configure { |c| c.host 'myhost' } }
172
+ .not_to raise_error
173
+ end
174
+
175
+ it 'sets variable' do
176
+ expect { configurable.configure { |c| c.host 'myhost' } }
177
+ .to change(config, :host)
178
+ .from(nil).to('myhost')
179
+ end
180
+ end
181
+
182
+ context 'when config class does not have the methods' do
183
+ let(:config_class) { Class.new(Sinclair::Config) }
184
+
185
+ it do
186
+ expect { configurable.configure { |c| c.host 'myhost' } }
187
+ .not_to raise_error
188
+ end
189
+
190
+ it 'sets variable' do
191
+ expect { configurable.configure { |c| c.host 'myhost' } }
192
+ .to change { config.instance_variable_get(:@host) }
193
+ .from(nil).to('myhost')
194
+ end
195
+ end
196
+ end
96
197
  end
97
198
  end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require './spec/support/models/my_server_config'
4
+
5
+ class Client
6
+ extend Sinclair::Configurable
7
+
8
+ configurable_by MyServerConfig, with: %i[host port]
9
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class MyServerConfig
4
+ def url
5
+ if @port
6
+ "http://#{@host}:#{@port}"
7
+ else
8
+ "http://#{@host}"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ServerConfig
4
+ attr_reader :host, :port
5
+ 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.3.2
4
+ version: 1.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - DarthJee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-19 00:00:00.000000000 Z
11
+ date: 2019-06-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -234,6 +234,7 @@ files:
234
234
  - spec/lib/sinclair_spec.rb
235
235
  - spec/spec_helper.rb
236
236
  - spec/support/fixture_helpers.rb
237
+ - spec/support/models/client.rb
237
238
  - spec/support/models/default_value.rb
238
239
  - spec/support/models/default_value_builder.rb
239
240
  - spec/support/models/default_valueable.rb
@@ -250,9 +251,11 @@ files:
250
251
  - spec/support/models/my_config.rb
251
252
  - spec/support/models/my_configurable.rb
252
253
  - spec/support/models/my_model.rb
254
+ - spec/support/models/my_server_config.rb
253
255
  - spec/support/models/person.rb
254
256
  - spec/support/models/purchase.rb
255
257
  - spec/support/models/server.rb
258
+ - spec/support/models/server_config.rb
256
259
  - spec/support/models/validator_builder.rb
257
260
  - spec/support/shared_examples/config_factory.rb
258
261
  - spec/support/shared_examples/method_definition.rb