config_module 0.1.0 → 1.0.0
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/.gitignore +2 -0
- data/.travis.yml +11 -0
- data/Gemfile +0 -2
- data/README.markdown +3 -0
- data/config_module.gemspec +1 -0
- data/lib/config_module/config_helper.rb +18 -11
- data/lib/config_module/config_option.rb +14 -1
- data/lib/config_module/exceptions.rb +7 -4
- data/lib/config_module/version.rb +1 -1
- data/lib/config_module.rb +5 -5
- data/spec/config_helper_spec.rb +6 -9
- data/spec/config_module_spec.rb +12 -0
- data/spec/config_option_spec.rb +25 -0
- data/spec/method_missing_spec.rb +10 -0
- data/spec/namespace_spec.rb +14 -1
- data/spec/run_all_specs.rb +5 -0
- metadata +23 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a93ee42bdd50f65f7ae6b14b8b76cb7ea3dfe3a7
|
4
|
+
data.tar.gz: 0052b145d6b42b8d7e6662a9d196e22907e94568
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f347735ec0e70695643960023c16128f58ea943992ef3c5f71af2daf96df5854b221f1448d635f8544cde4b4555c061e8b70844d864f90066b7ad1e49b25a92
|
7
|
+
data.tar.gz: 5535e018b88f7112b83e3015bfe7f2b58d02e76cfad063d4f0309ce963756919eb72baf8f5b45bb7c5150765175475378b1297e893b4b2a1e4ccf96baada5cdc
|
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/README.markdown
CHANGED
@@ -3,6 +3,9 @@ ConfigModule
|
|
3
3
|
|
4
4
|
Load important configuration files into their own modules!
|
5
5
|
|
6
|
+
[](https://codeclimate.com/github/acook/config_module)
|
7
|
+
[](https://gemnasium.com/acook/config_module)
|
8
|
+
|
6
9
|
Installation
|
7
10
|
------------
|
8
11
|
|
data/config_module.gemspec
CHANGED
@@ -17,6 +17,7 @@ Gem::Specification.new do |gem|
|
|
17
17
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
18
|
gem.require_paths = ["lib"]
|
19
19
|
|
20
|
+
gem.add_development_dependency 'uspec'
|
20
21
|
gem.add_development_dependency 'pry'
|
21
22
|
gem.add_development_dependency 'pry-theme'
|
22
23
|
end
|
@@ -7,15 +7,19 @@ module ConfigModule
|
|
7
7
|
@config ||= ConfigOption.wrap load_config
|
8
8
|
end
|
9
9
|
|
10
|
-
def method_missing_handler name, source
|
11
|
-
config.send name
|
10
|
+
def method_missing_handler name, source, *args, &block
|
11
|
+
config.send name, *args, &block
|
12
12
|
rescue NoMethodError => error
|
13
13
|
if error.name == name then
|
14
|
-
raise
|
14
|
+
raise(
|
15
|
+
ConfigOption::NotFoundError.new(name, self, error),
|
16
|
+
error.message, source
|
17
|
+
)
|
15
18
|
else
|
16
19
|
raise
|
17
20
|
end
|
18
21
|
end
|
22
|
+
alias_method :field_lookup_handler, :method_missing_handler
|
19
23
|
|
20
24
|
def load_config
|
21
25
|
@raw_config = YAML.load_file config_file
|
@@ -24,17 +28,20 @@ module ConfigModule
|
|
24
28
|
end
|
25
29
|
|
26
30
|
def load_namespaces_from tree
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
31
|
+
namespaces.inject(ConfigOption.wrap tree) do |subtree, ns|
|
32
|
+
if subtree.respond_to? ns then
|
33
|
+
subtree.send ns
|
34
|
+
else
|
35
|
+
raise(
|
36
|
+
InvalidNamespaceError.new(ns, subtree, caller),
|
37
|
+
"No subkey with name: #{ns.inspect}", caller(6)
|
38
|
+
)
|
39
|
+
end
|
33
40
|
end
|
34
41
|
end
|
35
42
|
|
36
|
-
def
|
37
|
-
|
43
|
+
def namespaces
|
44
|
+
@namespaces ||= Array.new
|
38
45
|
end
|
39
46
|
end
|
40
47
|
end
|
@@ -15,14 +15,27 @@ module ConfigModule
|
|
15
15
|
@table.each_pair{|p| yield p}
|
16
16
|
end
|
17
17
|
|
18
|
+
def [] name
|
19
|
+
@table[name.to_sym]
|
20
|
+
end
|
21
|
+
|
18
22
|
def method_missing name, *args, &block
|
19
23
|
result = super
|
20
24
|
|
21
25
|
if result || @table.include?(name) then
|
22
26
|
self.class.wrap result
|
23
27
|
else
|
24
|
-
raise
|
28
|
+
raise(
|
29
|
+
ConfigOption::NotFoundError.new(name, self, caller),
|
30
|
+
"Key not found: #{name}", caller(3)
|
31
|
+
)
|
25
32
|
end
|
33
|
+
|
34
|
+
rescue NoMethodError => error
|
35
|
+
raise(
|
36
|
+
ConfigOption::NotFoundError.new(name, self, error),
|
37
|
+
error.message, caller(3)
|
38
|
+
)
|
26
39
|
end
|
27
40
|
|
28
41
|
def new_ostruct_member name
|
@@ -1,10 +1,13 @@
|
|
1
1
|
module ConfigModule
|
2
2
|
class ConfigError < NoMethodError
|
3
|
-
def initialize name, object
|
4
|
-
@name, @object = name, object
|
5
|
-
|
3
|
+
def initialize name, object, details = nil
|
4
|
+
@name, @object, @details = name, object, details
|
5
|
+
@custom_message = "invalid #{identifier} `#{name}' for #{object_info}"
|
6
6
|
end
|
7
|
-
attr :name, :object
|
7
|
+
attr :name, :object, :custom_message, :details
|
8
|
+
|
9
|
+
alias_method :super_message, :message
|
10
|
+
alias_method :message, :custom_message
|
8
11
|
|
9
12
|
def object_info
|
10
13
|
if object.is_a?(Class) then
|
data/lib/config_module.rb
CHANGED
@@ -8,8 +8,8 @@ require_relative 'config_module/config_option'
|
|
8
8
|
require_relative 'config_module/config_helper'
|
9
9
|
|
10
10
|
module ConfigModule
|
11
|
-
def [] key
|
12
|
-
__config_module_helper.
|
11
|
+
def [] key, *args
|
12
|
+
__config_module_helper.field_lookup_handler key, caller(1), *args
|
13
13
|
end
|
14
14
|
|
15
15
|
def config
|
@@ -23,7 +23,7 @@ protected
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def namespace *new_namespace
|
26
|
-
__config_module_helper.namespaces =
|
26
|
+
__config_module_helper.namespaces = new_namespace.flatten
|
27
27
|
end
|
28
28
|
|
29
29
|
private
|
@@ -32,7 +32,7 @@ private
|
|
32
32
|
@__config_module_helper ||= ConfigHelper.new
|
33
33
|
end
|
34
34
|
|
35
|
-
def method_missing name
|
36
|
-
__config_module_helper.method_missing_handler name, caller(1)
|
35
|
+
def method_missing name, *args, &block
|
36
|
+
__config_module_helper.method_missing_handler name, caller(1), *args
|
37
37
|
end
|
38
38
|
end
|
data/spec/config_helper_spec.rb
CHANGED
@@ -1,16 +1,13 @@
|
|
1
1
|
require_relative 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
helper = ConfigModule::ConfigHelper.new
|
4
|
+
helper.config_file = './config/example.yml'
|
5
5
|
|
6
|
-
spec '
|
7
|
-
opt.map{|k,v| v[:b]} == [5]
|
8
|
-
end
|
9
|
-
|
10
|
-
spec 'to_ary' do
|
6
|
+
spec 'method_missing_handler traces back to the caller' do
|
11
7
|
begin
|
12
|
-
|
8
|
+
helper.method_missing_handler :nonexistant, caller(1)
|
13
9
|
rescue NoMethodError => error
|
14
|
-
error.
|
10
|
+
error.backtrace.to_s.include? "spec/config_helper_spec.rb:6:in"
|
15
11
|
end
|
16
12
|
end
|
13
|
+
|
data/spec/config_module_spec.rb
CHANGED
@@ -13,6 +13,10 @@ spec 'config modules have [] methods' do
|
|
13
13
|
ExampleConfig.respond_to? :[]
|
14
14
|
end
|
15
15
|
|
16
|
+
spec 'config modules return correct data using []' do
|
17
|
+
ExampleConfig[:foo] == 'bar'
|
18
|
+
end
|
19
|
+
|
16
20
|
spec 'nested hash values are properly wrapped' do
|
17
21
|
ExampleConfig.dictionary.class == ConfigModule::ConfigOption
|
18
22
|
end
|
@@ -21,6 +25,14 @@ spec 'subkeys are accessible with methods' do
|
|
21
25
|
ExampleConfig.dictionary.configuration == 'An arrangement of elements in a particular form, figure, or combination.'
|
22
26
|
end
|
23
27
|
|
28
|
+
spec 'subkeys work with .each' do
|
29
|
+
text = String.new
|
30
|
+
ExampleConfig.dictionary.each do |entry|
|
31
|
+
text << entry.to_s
|
32
|
+
end
|
33
|
+
text == "[:configuration, \"An arrangement of elements in a particular form, figure, or combination.\"]"
|
34
|
+
end
|
35
|
+
|
24
36
|
module FalseNil
|
25
37
|
extend ConfigModule
|
26
38
|
config_file './config/false_nil.yml'
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
hash = {a: {b: 5}}
|
4
|
+
opt = ConfigModule::ConfigOption.new hash
|
5
|
+
|
6
|
+
spec 'responds to #[]' do
|
7
|
+
opt.respond_to? :[]
|
8
|
+
end
|
9
|
+
|
10
|
+
spec '#[] returns value associated with key' do
|
11
|
+
opt[:a] == hash[:a]
|
12
|
+
end
|
13
|
+
|
14
|
+
spec 'ConfigOptions are Enumerable' do
|
15
|
+
opt.map{|k,v| v[:b]} == [5]
|
16
|
+
end
|
17
|
+
|
18
|
+
spec 'to_ary' do
|
19
|
+
begin
|
20
|
+
opt.to_ary
|
21
|
+
rescue NoMethodError => error
|
22
|
+
error.class == ConfigModule::ConfigOption::NotFoundError
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
data/spec/namespace_spec.rb
CHANGED
@@ -5,7 +5,7 @@ def self.helper
|
|
5
5
|
else
|
6
6
|
@helper = ConfigModule::ConfigHelper.new
|
7
7
|
@helper.config_file = './config/example.yml'
|
8
|
-
@helper.namespaces = 'production'
|
8
|
+
@helper.namespaces = ['production']
|
9
9
|
@helper.config
|
10
10
|
@helper
|
11
11
|
end
|
@@ -21,3 +21,16 @@ spec 'specifying a namespace forces #config to return that subtree the first tim
|
|
21
21
|
symbolized_keys_from_full_config = helper.raw_config['production'].keys.map(&:to_sym)
|
22
22
|
symbolized_keys_from_full_config == table.keys
|
23
23
|
end
|
24
|
+
|
25
|
+
spec 'misconfigured namespaces provide useful errors' do
|
26
|
+
helper = ConfigModule::ConfigHelper.new
|
27
|
+
helper.config_file = './config/example.yml'
|
28
|
+
helper.namespaces = ['nonexistant']
|
29
|
+
|
30
|
+
begin
|
31
|
+
helper.config
|
32
|
+
rescue ConfigModule::InvalidNamespaceError => error
|
33
|
+
error.super_message == 'No subkey with name: "nonexistant"' &&
|
34
|
+
error.message == "invalid namespace `nonexistant' for instance of `ConfigModule::ConfigOption < OpenStruct'"
|
35
|
+
end
|
36
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: config_module
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anthony Cook
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-03-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: uspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: pry
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -48,6 +62,7 @@ extra_rdoc_files: []
|
|
48
62
|
files:
|
49
63
|
- .gitignore
|
50
64
|
- .rvmrc
|
65
|
+
- .travis.yml
|
51
66
|
- Gemfile
|
52
67
|
- README.markdown
|
53
68
|
- Rakefile
|
@@ -61,8 +76,11 @@ files:
|
|
61
76
|
- spec/config/false_nil.yml
|
62
77
|
- spec/config_helper_spec.rb
|
63
78
|
- spec/config_module_spec.rb
|
79
|
+
- spec/config_option_spec.rb
|
64
80
|
- spec/example_config.rb
|
81
|
+
- spec/method_missing_spec.rb
|
65
82
|
- spec/namespace_spec.rb
|
83
|
+
- spec/run_all_specs.rb
|
66
84
|
- spec/spec_helper.rb
|
67
85
|
homepage: http://github.com/acook/config_module
|
68
86
|
licenses: []
|
@@ -92,6 +110,9 @@ test_files:
|
|
92
110
|
- spec/config/false_nil.yml
|
93
111
|
- spec/config_helper_spec.rb
|
94
112
|
- spec/config_module_spec.rb
|
113
|
+
- spec/config_option_spec.rb
|
95
114
|
- spec/example_config.rb
|
115
|
+
- spec/method_missing_spec.rb
|
96
116
|
- spec/namespace_spec.rb
|
117
|
+
- spec/run_all_specs.rb
|
97
118
|
- spec/spec_helper.rb
|