sinclair 1.4.0 → 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4826b55d94281092e5a0ba4d397366c39ec3494ec6f84743480c9e6ef69a030e
4
- data.tar.gz: fbd63c4a98eacaf569a56c95411e71043f5b400bd7f98b593c73fba1a68d5bd6
3
+ metadata.gz: cf2a52c6d33753fa9a52ad79fc4657d9d8573526a471ffb8c4fad386b1063e63
4
+ data.tar.gz: e219d16240478c36417001ac957437c4bcd2b29b241d51dde3802a0f9114dd32
5
5
  SHA512:
6
- metadata.gz: 531c6f6db55bed97a3bfcf8f8c6001ae35cd5226aa5e386b0bb9442afcea7984bdf77ef4b16774e6e7c44c4b51c8ec15eac74431042f26a12d069909a2fa6a1f
7
- data.tar.gz: 5820b843eaddf7838d430c4713645fbad5a8bf46850bc9cce5dbc1cc9f7d0005dc949969c06ba6ed3ca088a9790fdf32121e2e795dcc09935092636594809ed2
6
+ metadata.gz: 1d8348d94ceebf9dc9a303f7f9e394ee3e8d42351a5c9b78a503c0882fb65a6afa6054a124cae96d7cc77380c7c239e10774a6dc0b7bb4091ff2de5fb4cc9df6
7
+ data.tar.gz: 8c6177428b8184b3b949bfa493a17b23b4a401b5767ebd131ffbdf125c03f061e20be249f079194557dcaf429c91f58517ab76b1cea9f7e2635bea55df76276b
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.4.0
17
+ https://www.rubydoc.info/gems/sinclair/1.4.1
18
18
 
19
19
  Installation
20
20
  ---------------
@@ -308,7 +308,8 @@ Configurable is a module that, when used, can add configurations
308
308
  to your classes/modules.
309
309
 
310
310
  Configurations are read-only objects that can only be set using
311
- the `configurable#configure` method
311
+ the `configurable#configure` method which accepts a block or
312
+ hash
312
313
 
313
314
  ```ruby
314
315
  module MyConfigurable
@@ -318,9 +319,8 @@ the `configurable#configure` method
318
319
  configurable_with :host, port: 80
319
320
  end
320
321
 
321
- MyConfigurable.configure do |config|
322
+ MyConfigurable.configure(port: 5555) do |config|
322
323
  config.host 'interstella.art'
323
- config.port 5555
324
324
  end
325
325
 
326
326
  MyConfigurable.config.host # returns 'interstella.art'
@@ -32,45 +32,6 @@ class Sinclair
32
32
  warn CONFIG_CLASS_WARNING
33
33
  end
34
34
 
35
- # @api public
36
- #
37
- # Returns current instance of config
38
- #
39
- # the method returns the same instance until +reset_config+
40
- # is called
41
- #
42
- # @return [Config,Object] the instance of given
43
- # config_class. by default, this returns
44
- # +Class.new(Config).new+
45
- #
46
- # @see #reset_config
47
- #
48
- # @example (see ConfigFactory)
49
- def config
50
- @config ||= config_class.new
51
- end
52
-
53
- # @api public
54
- #
55
- # Cleans the current config instance
56
- #
57
- # After cleaning it, {#config} will generate a new
58
- # instance
59
- #
60
- # @return [NilClass]
61
- #
62
- # @example
63
- # factory = Sinclair::ConfigFactory.new
64
- #
65
- # config = factory.config
66
- #
67
- # factory.reset_config
68
- #
69
- # factory.config == config # returns false
70
- def reset_config
71
- @config = nil
72
- end
73
-
74
35
  # Adds possible configurations
75
36
  #
76
37
  # It change the configuration class adding methods
@@ -102,39 +63,59 @@ class Sinclair
102
63
  config_attributes.concat(builder.config_names.map(&:to_sym))
103
64
  end
104
65
 
105
- # @api public
66
+ # (see Configurable#config)
106
67
  #
107
- # Set the values in the config
68
+ # @see #reset_config
108
69
  #
109
- # The block given is evaluated by the {ConfigBuilder}
110
- # where each method missed will be used to set a variable
111
- # in the config
70
+ # @example (see ConfigFactory)
71
+ def config
72
+ @config ||= config_class.new
73
+ end
74
+
75
+ # (see Configurable#reset_config)
112
76
  #
113
- # @yield [ConfigBuilder] methods called in the block
114
- # that are not present in {ConfigBuilder} are
115
- # then set as instance variables of the config
77
+ # @example
78
+ # factory = Sinclair::ConfigFactory.new
116
79
  #
117
- # @return [Object] the result of the block
80
+ # config = factory.config
81
+ #
82
+ # factory.reset_config
83
+ #
84
+ # factory.config == config # returns false
85
+ def reset_config
86
+ @config = nil
87
+ end
88
+
89
+ # (see Configurable#configure)
118
90
  #
119
91
  # @example Setting name on config
120
92
  # class MyConfig
121
93
  # extend Sinclair::ConfigClass
122
94
  #
123
- # attr_reader :name
95
+ # attr_reader :name, :email
124
96
  # end
125
97
  #
126
98
  # factory = Sinclair::ConfigFactory.new(
127
99
  # config_class: MyConfig,
128
- # config_attributes: [:name]
100
+ # config_attributes: %i[name email]
129
101
  # )
130
102
  #
131
103
  # config = factory.config
132
104
  #
133
- # factory.configure { name 'John' }
105
+ # factory.configure(email: 'john@server.com') do
106
+ # name 'John'
107
+ # end
134
108
  #
135
- # config.name # returns 'John'
136
- def configure(&block)
137
- config_builder.instance_eval(&block)
109
+ # config.name # returns 'John'
110
+ # config.email # returns 'john@server.com'
111
+ def configure(config_hash = {}, &block)
112
+ config_builder.instance_eval(&block) if block
113
+
114
+ config_builder.instance_eval do
115
+ config_hash.each do |key, value|
116
+ public_send(key, value)
117
+ end
118
+ end
138
119
  end
139
120
 
140
121
  # Returns a new instance of ConfigFactory
@@ -25,23 +25,57 @@ class Sinclair
25
25
  "configurable_with.\n" \
26
26
  "In future releases this will be enforced.\n" \
27
27
  'see more on https://github.com/darthjee/sinclair/blob/master/WARNINGS.md#usage-of-custom-config-classes'
28
- # (see ConfigFactory#config)
28
+
29
+ # @method config
30
+ #
31
+ # @api public
32
+ #
33
+ # Returns current instance of config
34
+ #
35
+ # the method returns the same instance until +reset_config+
36
+ # is called
37
+ #
38
+ # @return [Config,Object] the instance of given
39
+ # config_class. by default, this returns
40
+ # +Class.new(Config).new+
41
+ #
42
+ # @see #reset_config
29
43
  # @see ConfigFactory#config
30
- def config
31
- config_factory.config
32
- end
33
44
 
34
- # (see ConfigFactory#reset_config)
45
+ # @method reset_config
46
+ #
47
+ # @api public
48
+ #
49
+ # Cleans the current config instance
50
+ #
51
+ # After cleaning it, {#config} will generate a new
52
+ # instance
53
+ #
54
+ # @return [NilClass]
55
+ #
35
56
  # @see ConfigFactory#reset_config
36
- def reset_config
37
- config_factory.reset_config
38
- end
39
57
 
40
- # (see ConfigFactory#configure)
58
+ # @method configure(config_hash = {}, &block)
59
+ #
60
+ # @api public
61
+ #
62
+ # Set the values in the config
63
+ #
64
+ # The block given is evaluated by the {ConfigBuilder}
65
+ # where each method missed will be used to set a variable
66
+ # in the config
67
+ #
68
+ # @param config_hash [Hash] hash with keys and values for
69
+ # the configuration
70
+ #
71
+ # @yield [ConfigBuilder] methods called in the block
72
+ # that are not present in {ConfigBuilder} are
73
+ # then set as instance variables of the config
74
+ #
75
+ # @return [Object] the result of the block
76
+ #
41
77
  # @see ConfigFactory#configure
42
- def configure(&block)
43
- config_factory.configure(&block)
44
- end
78
+ delegate :config, :reset_config, :configure, to: :config_factory
45
79
 
46
80
  protected
47
81
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Sinclair
4
- VERSION = '1.4.0'
4
+ VERSION = '1.4.1'
5
5
  end
@@ -6,9 +6,8 @@ describe Sinclair::Configurable do
6
6
  describe 'README' do
7
7
  describe 'Configured with' do
8
8
  before do
9
- MyConfigurable.configure do |config|
9
+ MyConfigurable.configure(port: 5555) do |config|
10
10
  config.host 'interstella.art'
11
- config.port 5555
12
11
  end
13
12
  end
14
13
 
@@ -68,12 +68,22 @@ describe Sinclair::ConfigFactory do
68
68
  end
69
69
 
70
70
  describe '#configure' do
71
- let(:config_class) { MyConfig }
71
+ let(:config_class) { MyConfig }
72
+ let(:email) { 'john@server.com' }
73
+ let(:config_attributes) { %i[name email] }
72
74
 
73
- it 'sets variable on config' do
74
- expect { factory.configure { name 'John' } }
75
- .to change(config, :name)
76
- .from(nil).to('John')
75
+ describe 'Setting name with hash and block' do
76
+ it 'sets name on config' do
77
+ expect { factory.configure(email: email) { name 'John' } }
78
+ .to change(config, :name)
79
+ .from(nil).to('John')
80
+ end
81
+
82
+ it 'sets email on config' do
83
+ expect { factory.configure(email: email) { name 'John' } }
84
+ .to change(config, :email)
85
+ .from(nil).to(email)
86
+ end
77
87
  end
78
88
  end
79
89
  end
@@ -165,24 +165,49 @@ describe Sinclair::Configurable do
165
165
  describe '.configure' do
166
166
  let(:config) { configurable.config }
167
167
 
168
- it do
169
- expect { configurable.configure { |c| c.user 'Bob' } }
170
- .to change(config, :user)
171
- .from(nil).to('Bob')
172
- end
173
-
174
- context 'when it was defined using string' do
168
+ context 'when configuring using a block' do
175
169
  it do
176
- expect { configurable.configure { |c| c.password '123456' } }
177
- .to change(config, :password)
178
- .from(nil).to('123456')
170
+ expect { configurable.configure { |c| c.user 'Bob' } }
171
+ .to change(config, :user)
172
+ .from(nil).to('Bob')
173
+ end
174
+
175
+ context 'when it was defined using string' do
176
+ it do
177
+ expect { configurable.configure { |c| c.password '123456' } }
178
+ .to change(config, :password)
179
+ .from(nil).to('123456')
180
+ end
181
+ end
182
+
183
+ context 'when calling a method that was not defined' do
184
+ it do
185
+ expect { configurable.configure { |c| c.nope '123456' } }
186
+ .to raise_error(NoMethodError)
187
+ end
179
188
  end
180
189
  end
181
190
 
182
- context 'when calling a method that was not defined' do
191
+ context 'when configuring using a hash' do
183
192
  it do
184
- expect { configurable.configure { |c| c.nope '123456' } }
185
- .to raise_error(NoMethodError)
193
+ expect { configurable.configure(user: 'Bob') }
194
+ .to change(config, :user)
195
+ .from(nil).to('Bob')
196
+ end
197
+
198
+ context 'when it was defined using string' do
199
+ it do
200
+ expect { configurable.configure(password: '123456') }
201
+ .to change(config, :password)
202
+ .from(nil).to('123456')
203
+ end
204
+ end
205
+
206
+ context 'when calling a method that was not defined' do
207
+ it do
208
+ expect { configurable.configure(nope: '123456') }
209
+ .to raise_error(NoMethodError)
210
+ end
186
211
  end
187
212
  end
188
213
 
@@ -3,5 +3,5 @@
3
3
  class MyConfig
4
4
  extend Sinclair::ConfigClass
5
5
 
6
- attr_reader :name, :config
6
+ attr_reader :name, :email, :config
7
7
  end
@@ -66,32 +66,71 @@ shared_examples 'a config factory adding config' do
66
66
  end
67
67
 
68
68
  shared_examples 'configure a config' do
69
- it 'sets value on config' do
70
- expect { factory.configure { |c| c.user 'Bob' } }
71
- .to change(config, :user).to('Bob')
72
- end
69
+ context 'when a block is given' do
70
+ it 'sets value on config' do
71
+ expect { factory.configure { |c| c.user 'Bob' } }
72
+ .to change(config, :user).to('Bob')
73
+ end
73
74
 
74
- context 'when re-seting the value to nil' do
75
- before { factory.configure { |c| c.user 'Bob' } }
75
+ context 'when re-seting the value to nil' do
76
+ before { factory.configure { |c| c.user 'Bob' } }
77
+
78
+ it 'sets nil value on config' do
79
+ expect { factory.configure { |c| c.user nil } }
80
+ .to change(config, :user).to(nil)
81
+ end
82
+ end
76
83
 
77
- it 'sets nil value on config' do
78
- expect { factory.configure { |c| c.user nil } }
79
- .to change(config, :user).to(nil)
84
+ context 'when calling a method that was not defined' do
85
+ it do
86
+ expect { factory.configure { |c| c.nope '123456' } }
87
+ .to raise_error(NoMethodError)
88
+ end
89
+ end
90
+
91
+ context 'when it was defined using string' do
92
+ it do
93
+ expect { factory.configure { |c| c.password '123456' } }
94
+ .to change(config, :password)
95
+ .to('123456')
96
+ end
80
97
  end
81
98
  end
82
99
 
83
- context 'when calling a method that was not defined' do
100
+ context 'when no block nor hash is given' do
84
101
  it do
85
- expect { factory.configure { |c| c.nope '123456' } }
86
- .to raise_error(NoMethodError)
102
+ expect { factory.configure }.not_to raise_error
87
103
  end
88
104
  end
89
105
 
90
- context 'when it was defined using string' do
91
- it do
92
- expect { factory.configure { |c| c.password '123456' } }
93
- .to change(config, :password)
94
- .to('123456')
106
+ context 'when a hash is given' do
107
+ it 'sets value on config' do
108
+ expect { factory.configure(user: 'Bob') }
109
+ .to change(config, :user).to('Bob')
110
+ end
111
+
112
+ context 'when re-seting the value to nil' do
113
+ before { factory.configure(user: 'Bob') }
114
+
115
+ it 'sets nil value on config' do
116
+ expect { factory.configure(user: nil) }
117
+ .to change(config, :user).to(nil)
118
+ end
119
+ end
120
+
121
+ context 'when setting a variable that was not defined' do
122
+ it do
123
+ expect { factory.configure(nope: '123456') }
124
+ .to raise_error(NoMethodError)
125
+ end
126
+ end
127
+
128
+ context 'when it was defined using string' do
129
+ it do
130
+ expect { factory.configure(password: '123456') }
131
+ .to change(config, :password)
132
+ .to('123456')
133
+ end
95
134
  end
96
135
  end
97
136
  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.4.0
4
+ version: 1.4.1
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-30 00:00:00.000000000 Z
11
+ date: 2019-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport