config-factory 0.0.8 → 0.0.9

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
  SHA1:
3
- metadata.gz: 6a2b88cc897a3961acfe0506efeae2215de462b8
4
- data.tar.gz: 0478ac34202073124995fd797b082bbcfa609551
3
+ metadata.gz: c44c851822d0e71704e0892c28b76f68cfed5772
4
+ data.tar.gz: 6b6fc389e2b25d2b3d2344990761ccf768c0fb0a
5
5
  SHA512:
6
- metadata.gz: ac4f9910a57c0cdb78c9052f773cbef06ca811f881af87418103a2861ad5c9c17db8687f9cba42cdc465cb3092e713443ee039cae30705860e0f487692660d94
7
- data.tar.gz: 971e76011a0ff96b3607392261bb88f48e708e0745406c7a28067932e8553bbd354690b396177e46a3040be0aa35837c20c96867d4322491f07305d59676a666
6
+ metadata.gz: f94eecfabc84b6e17d0e40538dd65dff3b299c0920acc7a5e07964552c2a521bf15e564c2c23efbf31e805d666defc7e128f8cd06981e7cb04c332e4b12de17c
7
+ data.tar.gz: 517762975319826c5101bf3ba0ed599735d4ab593b3c4a8bd971a3ec36fbf62886287f3b2f4f18db58e68e7eb11a6a5eede8fb9d71f157a1e840bfac7a64ec9c
data/CHANGES.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.0.9 (9 August 2016)
2
+
3
+ - Add `AbstractFactory#env_name` and inject the environment name, if available, on construction,
4
+ in case factories need to know their environments. The default value is `Environments::DEFAULT_ENVIRONMENT`.
5
+
1
6
  ## 0.0.8 (4 April 2016)
2
7
 
3
8
  - Support looking up implementation classes based on an argument
@@ -1,9 +1,16 @@
1
+ require 'config/factory/environments'
2
+
1
3
  module Config
2
4
  module Factory
5
+
3
6
  def self.included(base)
4
7
  base.extend(AbstractFactory)
5
8
  end
6
9
 
10
+ def env_name
11
+ @env_name ||= Environments::DEFAULT_ENVIRONMENT
12
+ end
13
+
7
14
  module AbstractFactory
8
15
  attr_reader :impl_key
9
16
 
@@ -26,7 +33,7 @@ module Config
26
33
  def for_environment(env, config_name)
27
34
  arg_hash = env.args_for(config_name)
28
35
  fail ArgumentError, "no #{self} arguments found for config #{config_name} in environment #{env}" unless arg_hash
29
- build_from(arg_hash)
36
+ build_from(arg_hash, nil, env.name)
30
37
  end
31
38
 
32
39
  def from_file(path, config_name)
@@ -34,12 +41,16 @@ module Config
34
41
  for_environment(env, config_name)
35
42
  end
36
43
 
37
- def build_from(arg_hash, section_name = nil)
44
+ def build_from(arg_hash, section_name = nil, env_name = nil)
38
45
  fail ArgumentError, "nil argument hash passed to #{self}.build_from" unless arg_hash
39
46
  args = deep_symbolize_keys(arg_hash)
40
47
  args = args[section_name] if section_name
41
48
  impl_class = find_impl_class(args)
42
- impl_class.new(args)
49
+ begin
50
+ return create_impl(impl_class, args, env_name)
51
+ rescue => e
52
+ raise ArgumentError, "Error instantiating #{impl_class} with arguments #{args}: #{e}"
53
+ end
43
54
  end
44
55
 
45
56
  private
@@ -54,7 +65,7 @@ module Config
54
65
  end
55
66
 
56
67
  def impl_for_key(key_sym, args)
57
- fail ArgumentError, "implementation key #{key_sym} not found in argument hash #{args}" unless args.key?(key_sym)
68
+ fail ArgumentError, "implementation key #{key_sym} not found in argument hash #{args || 'nil'}" unless args && args.key?(key_sym)
58
69
  key_value = args.delete(key_sym)
59
70
  impl_class = impls_by_key[key_value]
60
71
  fail ArgumentError, "No #{name} implementation found for #{key_sym}: #{key_value}" unless impl_class
@@ -72,6 +83,11 @@ module Config
72
83
  @impls_by_key ||= {}
73
84
  end
74
85
 
86
+ def create_impl(impl_class, args, env_name)
87
+ factory_impl = impl_class.new(args)
88
+ factory_impl.instance_variable_set(:@env_name, env_name)
89
+ factory_impl
90
+ end
75
91
  end
76
92
  end
77
93
  end
@@ -1,5 +1,5 @@
1
1
  require 'yaml'
2
- require_relative 'environment'
2
+ require 'config/factory/environment'
3
3
 
4
4
  module Config
5
5
  module Factory
@@ -4,7 +4,7 @@ module Config
4
4
  NAME = 'config-factory'
5
5
 
6
6
  # The version of this gem
7
- VERSION = '0.0.8'
7
+ VERSION = '0.0.9'
8
8
 
9
9
  # The copyright notice for this gem
10
10
  COPYRIGHT = 'Copyright (c) 2016 The Regents of the University of California'
@@ -15,6 +15,12 @@ module Config
15
15
  end
16
16
  end
17
17
 
18
+ it 'defaults to Environments::DEFAULT_ENVIRONMENT' do
19
+ config_hash = { protocol: 'OAI', oai_base_url: 'http://oai.example.org/oai', metadata_prefix: 'some_prefix', set: 'some_set', seconds_granularity: true }
20
+ impl = SourceConfig.build_from(config_hash)
21
+ expect(impl.env_name).to eq(Environments::DEFAULT_ENVIRONMENT)
22
+ end
23
+
18
24
  it 'raises a sensible exception if no impl found for the key' do
19
25
  config_hash = { protocol: 'Elvis' }
20
26
  expect { SourceConfig.build_from(config_hash) }.to raise_error(ArgumentError, /SourceConfig.*protocol.*Elvis/)
@@ -32,6 +38,7 @@ module Config
32
38
  impl = MysqlConfig.build_from(config_hash)
33
39
  expect(impl).to be_a(MysqlConfig)
34
40
  expect(impl.connection_info).to eq(config_hash)
41
+ expect(impl.env_name).to eq(Environments::DEFAULT_ENVIRONMENT)
35
42
  end
36
43
 
37
44
  it "can build a class without a declared key, so long as it's registered" do
@@ -46,6 +53,7 @@ module Config
46
53
  impl = PersistenceConfig.build_from(config_hash)
47
54
  expect(impl).to be_a(DBConfig)
48
55
  expect(impl.connection_info).to eq(config_hash)
56
+ expect(impl.env_name).to eq(Environments::DEFAULT_ENVIRONMENT)
49
57
  end
50
58
 
51
59
  it 'works with section headers' do
@@ -60,29 +68,49 @@ module Config
60
68
  impl = PersistenceConfig.build_from(hash, :persistence)
61
69
  expect(impl).to be_a(DBConfig)
62
70
  expect(impl.connection_info).to eq(expected_info)
71
+ expect(impl.env_name).to eq(Environments::DEFAULT_ENVIRONMENT)
63
72
  end
64
73
  end
65
74
 
66
75
  describe '#for_environment' do
67
76
  it 'builds the correct class with the correct config' do
68
- config_hash = { protocol: 'OAI', oai_base_url: 'http://oai.example.org/oai', metadata_prefix: 'some_prefix', set: 'some_set', seconds_granularity: true }
69
77
  env = instance_double(Environment)
78
+
79
+ config_hash = { protocol: 'OAI', oai_base_url: 'http://oai.example.org/oai', metadata_prefix: 'some_prefix', set: 'some_set', seconds_granularity: true }
70
80
  expect(env).to receive(:args_for).with(:source) { config_hash }
71
81
 
82
+ env_name = :elvis
83
+ expect(env).to receive(:name) { env_name }
84
+
72
85
  impl = SourceConfig.for_environment(env, :source)
73
86
  expect(impl).to be_an(OAISourceConfig)
74
87
  args = { oai_base_url: URI('http://oai.example.org/oai'), metadata_prefix: 'some_prefix', set: 'some_set', seconds_granularity: true }
75
88
  args.each do |k, v|
76
89
  expect(impl.send(k)).to eq(v)
77
90
  end
91
+ expect(impl.env_name).to eq(env_name)
78
92
  end
79
93
 
80
94
  it 'raises a sensible exception if no impl found for the key' do
81
95
  config_hash = { protocol: 'Elvis' }
82
96
  env = instance_double(Environment)
83
97
  expect(env).to receive(:args_for).with(:source) { config_hash }
98
+ expect(env).to receive(:name) { 'elvis' }
84
99
  expect { SourceConfig.for_environment(env, :source) }.to raise_error(ArgumentError, /SourceConfig.*protocol.*Elvis/)
85
100
  end
101
+
102
+ it 'raises a sensible exception if initializer fails' do
103
+ bad_uri = 'I am not a URI'
104
+ config_hash = {
105
+ protocol: 'OAI',
106
+ oai_base_url: bad_uri,
107
+ metadata_prefix: 'some_prefix'
108
+ }
109
+ env = instance_double(Environment)
110
+ expect(env).to receive(:args_for).with(:source) { config_hash }
111
+ expect(env).to receive(:name) { 'elvis' }
112
+ expect { SourceConfig.for_environment(env, :source) }.to raise_error(ArgumentError, /OAISourceConfig.*#{bad_uri}/)
113
+ end
86
114
  end
87
115
 
88
116
  describe '#from_file' do
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.8
4
+ version: 0.0.9
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-04-04 00:00:00.000000000 Z
11
+ date: 2016-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler