config_module 0.1.0 → 1.0.0

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
  SHA1:
3
- metadata.gz: 4ff9be2b58fa4690a4341b05c51578ca4066810c
4
- data.tar.gz: 0c6aeb9814bf45f447b86091c869804954909a3b
3
+ metadata.gz: a93ee42bdd50f65f7ae6b14b8b76cb7ea3dfe3a7
4
+ data.tar.gz: 0052b145d6b42b8d7e6662a9d196e22907e94568
5
5
  SHA512:
6
- metadata.gz: 9308b74b9f72f8c1f58384cb6063ed8736349628b5e30d338fa7f42290f5aabfc2e87e0b76f1c9b8ca1c2685d9bff2c13525d93db915cffd6639d5560e0e159a
7
- data.tar.gz: 083a6b61d0e40931f2335ef76b02db1e07bbca73cb8523f0667247f1650b0938fb0dae2a00669c80a39f1577200f80bc56d74c733165ae554d5466469395d26f
6
+ metadata.gz: 0f347735ec0e70695643960023c16128f58ea943992ef3c5f71af2daf96df5854b221f1448d635f8544cde4b4555c061e8b70844d864f90066b7ad1e49b25a92
7
+ data.tar.gz: 5535e018b88f7112b83e3015bfe7f2b58d02e76cfad063d4f0309ce963756919eb72baf8f5b45bb7c5150765175475378b1297e893b4b2a1e4ccf96baada5cdc
data/.gitignore CHANGED
@@ -15,3 +15,5 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ tags
19
+ TAGS
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
5
+ - rbx-19mode
6
+ - ruby-head
7
+ script: bundle exec ruby spec/run_all_specs.rb
8
+ before_install:
9
+ - gem update bundler
10
+ before_script:
11
+ - bundle exec gem list
data/Gemfile CHANGED
@@ -2,5 +2,3 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in config_module.gemspec
4
4
  gemspec
5
-
6
- gem 'uspec', git: '../uspec'
data/README.markdown CHANGED
@@ -3,6 +3,9 @@ ConfigModule
3
3
 
4
4
  Load important configuration files into their own modules!
5
5
 
6
+ [![Code Climate](https://codeclimate.com/github/acook/config_module.png)](https://codeclimate.com/github/acook/config_module)
7
+ [![Dependency Status](https://gemnasium.com/acook/config_module.png)](https://gemnasium.com/acook/config_module)
8
+
6
9
  Installation
7
10
  ------------
8
11
 
@@ -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 # ConfigOption::NotFoundError.new(name, source)#, source
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
- return tree unless namespaced?
28
-
29
- Array(namespaces).inject(ConfigOption.wrap tree) do |subtree, ns|
30
- raise(InvalidNamespaceError.new(ns, subtree)) unless subtree.respond_to? ns
31
-
32
- subtree.send ns
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 namespaced?
37
- !(namespaces.nil? || namespaces.empty?)
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 ConfigOption::NotFoundError.new name, self
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
- super "invalid #{identifier} `#{name}' for #{object_info}"
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
@@ -1,3 +1,3 @@
1
1
  module ConfigModule
2
- VERSION = "0.1.0"
2
+ VERSION = '1.0.0'
3
3
  end
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.config.send key
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 = *new_namespace
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
@@ -1,16 +1,13 @@
1
1
  require_relative 'spec_helper'
2
2
 
3
- hash = {a: {b: 5}}
4
- opt = ConfigModule::ConfigOption.new hash
3
+ helper = ConfigModule::ConfigHelper.new
4
+ helper.config_file = './config/example.yml'
5
5
 
6
- spec 'ConfigOptions are Enumerable' do
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
- opt.to_ary
8
+ helper.method_missing_handler :nonexistant, caller(1)
13
9
  rescue NoMethodError => error
14
- error.class == ConfigModule::ConfigOption::NotFoundError
10
+ error.backtrace.to_s.include? "spec/config_helper_spec.rb:6:in"
15
11
  end
16
12
  end
13
+
@@ -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
+
@@ -0,0 +1,10 @@
1
+ require_relative 'spec_helper'
2
+ require_relative 'example_config'
3
+
4
+ spec 'method missing must handle multiple arguments gracefully' do
5
+ begin
6
+ ExampleConfig.nonexistant :foo, :bar
7
+ rescue NoMethodError => error
8
+ error.name == :nonexistant
9
+ end
10
+ end
@@ -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
@@ -0,0 +1,5 @@
1
+ require_relative 'spec_helper'
2
+
3
+ Dir['*_spec.rb'].each do |filename|
4
+ require_relative filename
5
+ 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: 0.1.0
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-02-27 00:00:00.000000000 Z
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