config-factory 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES.md +5 -0
- data/README.md +45 -0
- data/lib/config/factory/abstract_factory.rb +9 -3
- data/lib/config/factory/environment.rb +1 -0
- data/lib/config/factory/module_info.rb +1 -1
- data/spec/data/db-config.yml +18 -0
- data/spec/unit/config/factory/abstract_factory_spec.rb +14 -0
- data/spec/unit/config/factory/environment_spec.rb +10 -2
- data/spec/unit/config/factory/environments_spec.rb +15 -2
- data/spec/unit/config/factory/fixtures.rb +10 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a1f210944b382bc0260157f164e45025db904802
|
4
|
+
data.tar.gz: 9d0d655fbb1a219b3bb22ce320b2fd0480dde335
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2bd33c114f0cbcb09f70f8aacf9b9ffff28757a20a5d8039e0888d0f8575887d9d2c282c34d703fa39d77aa786c9d01b5a76d623004ce55cf2588231f0364f94
|
7
|
+
data.tar.gz: 7526b42e798652f771d0c094ee574ecab591811bfadd26b5446d333fcd5e62f7e22107cb50c672270ba754a590954d65fe20a9340703cb3f6ebb380359064c76
|
data/CHANGES.md
CHANGED
data/README.md
CHANGED
@@ -11,6 +11,16 @@ pattern, with run-time configuration provided by hashes or YAML files.
|
|
11
11
|
|
12
12
|
## Example
|
13
13
|
|
14
|
+
The abstract configuration factory declares a `key`, which is used to look up the concrete
|
15
|
+
config class for a given configuration. Concrete implementations register themselves with a
|
16
|
+
DSL method named after the `key` value.
|
17
|
+
|
18
|
+
In the example below, the `SourceConfig` abstract factory declares the key `:protocol`; the
|
19
|
+
concrete classes `OAISourceConfig` and `ResyncSourceConfig` register themselves with
|
20
|
+
`protocol: 'OAI'` and `protocol: 'Resync'`, respectively. `SourceConfig.for_environment()`
|
21
|
+
will then look for a `protocol:` line in the configuration file to determine which
|
22
|
+
registered concrete class to instantiate.
|
23
|
+
|
14
24
|
```ruby
|
15
25
|
class SourceConfig
|
16
26
|
include Config::Factory
|
@@ -83,3 +93,38 @@ test_env = environments[:test]
|
|
83
93
|
source_config = SourceConfig.for_environment(test_env, :source)
|
84
94
|
# => #<ResyncSourceConfig:0x007fe8d48180c0 @capability_list_url="http://localhost:8888/capabilitylist.xml">
|
85
95
|
```
|
96
|
+
|
97
|
+
## Config classes with only one implementation
|
98
|
+
|
99
|
+
`config-factory` also supports instantiating concrete configuration classes directly.
|
100
|
+
In this case, we simply don't declare a `key` for the class, and the configuration hash
|
101
|
+
will be passed directly to the initializer of the concrete class.
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
class DBConfig
|
105
|
+
include Config::Factory
|
106
|
+
|
107
|
+
def initialize(connection_info)
|
108
|
+
@connection_info = connection_info
|
109
|
+
end
|
110
|
+
end
|
111
|
+
```
|
112
|
+
|
113
|
+
```YAML
|
114
|
+
test:
|
115
|
+
db:
|
116
|
+
adapter: sqlite3
|
117
|
+
database: ':memory:'
|
118
|
+
pool: 5
|
119
|
+
timeout: 5000
|
120
|
+
|
121
|
+
production:
|
122
|
+
db:
|
123
|
+
adapter: mysql2
|
124
|
+
host: mydb.example.org
|
125
|
+
database: myapp
|
126
|
+
username: myuser
|
127
|
+
password: blank
|
128
|
+
port: 3306
|
129
|
+
encoding: utf8
|
130
|
+
```
|
@@ -29,15 +29,21 @@ module Config
|
|
29
29
|
def build_from(arg_hash)
|
30
30
|
fail ArgumentError, "nil argument hash passed to #{self}.build_from" unless arg_hash
|
31
31
|
args = deep_symbolize_keys(arg_hash)
|
32
|
+
product_class = find_product_class(args)
|
33
|
+
product_class.new(args)
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def find_product_class(args)
|
39
|
+
return self unless product_key
|
32
40
|
fail ArgumentError, "product key #{product_key} not found in argument hash #{args}" unless args.key?(product_key)
|
33
41
|
key_value = args.delete(product_key)
|
34
42
|
product_class = products[key_value]
|
35
43
|
fail ArgumentError, "No #{name} product class found for #{product_key}: #{key_value}" unless product_class
|
36
|
-
product_class
|
44
|
+
product_class
|
37
45
|
end
|
38
46
|
|
39
|
-
private
|
40
|
-
|
41
47
|
def deep_symbolize_keys(val)
|
42
48
|
return val unless val.is_a?(Hash)
|
43
49
|
val.map do |k, v|
|
@@ -0,0 +1,18 @@
|
|
1
|
+
development:
|
2
|
+
adapter: sqlite3
|
3
|
+
database: db/development.sqlite3
|
4
|
+
pool: 5
|
5
|
+
timeout: 5000
|
6
|
+
|
7
|
+
production:
|
8
|
+
adapter: sqlite3
|
9
|
+
database: db/production.sqlite3
|
10
|
+
pool: 5
|
11
|
+
timeout: 5000
|
12
|
+
|
13
|
+
test: &test
|
14
|
+
adapter: sqlite3
|
15
|
+
# database: db/test.sqlite3
|
16
|
+
database: ':memory:'
|
17
|
+
pool: 5
|
18
|
+
timeout: 5000
|
@@ -19,6 +19,20 @@ module Config
|
|
19
19
|
config_hash = { protocol: 'Elvis' }
|
20
20
|
expect { SourceConfig.build_from(config_hash) }.to raise_error(ArgumentError, /SourceConfig.*protocol.*Elvis/)
|
21
21
|
end
|
22
|
+
|
23
|
+
it 'supports concrete factories without product keys' do
|
24
|
+
config_hash = {
|
25
|
+
adapter: 'mysql2',
|
26
|
+
encoding: 'utf8',
|
27
|
+
pool: 5,
|
28
|
+
database: 'example_pord',
|
29
|
+
host: 'mysql-dev.example.org',
|
30
|
+
port: 3306
|
31
|
+
}
|
32
|
+
product = DBConfig.build_from(config_hash)
|
33
|
+
expect(product).to be_a(DBConfig)
|
34
|
+
expect(product.connection_info).to eq(config_hash)
|
35
|
+
end
|
22
36
|
end
|
23
37
|
|
24
38
|
describe '#for_environment' do
|
@@ -28,6 +28,15 @@ module Config
|
|
28
28
|
it 'requires configs to be hash-like' do
|
29
29
|
expect { Environment.new(name: :name, configs: false) }.to raise_error(ArgumentError)
|
30
30
|
end
|
31
|
+
it 'reads a standard ActiveRecord db config' do
|
32
|
+
yaml_hash = {
|
33
|
+
'adapter' => 'sqlite3',
|
34
|
+
'database' => 'db/development.sqlite3',
|
35
|
+
'pool' => 5,
|
36
|
+
'timeout' => 5000
|
37
|
+
}
|
38
|
+
Environment.new(name: :test, configs: yaml_hash)
|
39
|
+
end
|
31
40
|
end
|
32
41
|
|
33
42
|
describe '#load_file' do
|
@@ -57,8 +66,7 @@ module Config
|
|
57
66
|
|
58
67
|
it 'includes the hash' do
|
59
68
|
h = {
|
60
|
-
'db' =>
|
61
|
-
{
|
69
|
+
'db' => {
|
62
70
|
'adapter' => 'mysql2',
|
63
71
|
'encoding' => 'utf8',
|
64
72
|
'pool' => 5,
|
@@ -7,8 +7,21 @@ module Config
|
|
7
7
|
it 'loads a multi-environment config file' do
|
8
8
|
envs = Environments.load_file('spec/data/multiple-environments.yml')
|
9
9
|
expect(envs).to be_a(Hash)
|
10
|
-
|
11
|
-
|
10
|
+
expected = [:defaults, :development, :test, :production]
|
11
|
+
expect(envs.size).to eq(expected.size)
|
12
|
+
expected.each do |env_name|
|
13
|
+
env = envs[env_name]
|
14
|
+
expect(env).to be_an(Environment)
|
15
|
+
expect(env.name).to eq(env_name)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'reads a standard ActiveRecord DB config' do
|
20
|
+
envs = Environments.load_file('spec/data/db-config.yml')
|
21
|
+
expect(envs).to be_a(Hash)
|
22
|
+
expected = [:development, :test, :production]
|
23
|
+
expect(envs.size).to eq(expected.size)
|
24
|
+
expected.each do |env_name|
|
12
25
|
env = envs[env_name]
|
13
26
|
expect(env).to be_an(Environment)
|
14
27
|
expect(env.name).to eq(env_name)
|
@@ -41,6 +41,16 @@ class ResyncSourceConfig < SourceConfig
|
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
+
class DBConfig
|
45
|
+
include Config::Factory
|
46
|
+
|
47
|
+
attr_reader :connection_info
|
48
|
+
|
49
|
+
def initialize(connection_info)
|
50
|
+
@connection_info = connection_info
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
44
54
|
# IndexConfig
|
45
55
|
|
46
56
|
class IndexConfig
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: config-factory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Moles
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -120,6 +120,7 @@ files:
|
|
120
120
|
- lib/config/factory/log.rb
|
121
121
|
- lib/config/factory/module_info.rb
|
122
122
|
- spec/.rubocop.yml
|
123
|
+
- spec/data/db-config.yml
|
123
124
|
- spec/data/multiple-environments.yml
|
124
125
|
- spec/data/single-environment.yml
|
125
126
|
- spec/spec_helper.rb
|
@@ -154,6 +155,7 @@ specification_version: 4
|
|
154
155
|
summary: A gem for creating configuration classes using the Abstract Factory pattern.
|
155
156
|
test_files:
|
156
157
|
- spec/.rubocop.yml
|
158
|
+
- spec/data/db-config.yml
|
157
159
|
- spec/data/multiple-environments.yml
|
158
160
|
- spec/data/single-environment.yml
|
159
161
|
- spec/spec_helper.rb
|