config_module 0.0.2 → 0.0.3

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.
data/lib/config_module.rb CHANGED
@@ -8,38 +8,64 @@ module ConfigModule
8
8
  end
9
9
 
10
10
  def config
11
- @config ||= load_config
11
+ @config ||= ConfigOption.wrap load_config
12
12
  end
13
13
 
14
+ protected
15
+
14
16
  def config_file file
15
17
  @config_file = file
16
18
  end
17
19
 
18
- def namespace name
19
- @namespace = name
20
+ def namespace *name
21
+ @namespace = Array name
20
22
  end
21
23
 
22
- protected
24
+ private
23
25
 
24
- def wrap data
25
- if data.is_a? Hash then
26
- ConfigOption.new data
27
- else
28
- data
29
- end
26
+ def namespaced?
27
+ !(@namespace.nil? || @namespace.empty?)
30
28
  end
31
29
 
32
30
  def load_config
33
31
  file = YAML.load_file(@config_file)
34
- wrap @namespace ? (file[@namespace] || file[@namespace.to_sym]) : file
32
+
33
+ if namespaced? then
34
+ @namespace.inject(file) do |file, ns|
35
+ file.include?(ns) && file[ns] || file.include?(ns.to_sym) && file[ns.to_sym]
36
+ end
37
+ else
38
+ file
39
+ end
35
40
  end
36
41
 
37
- def method_missing name, *args, &block
38
- wrap config.send name, *args, &block
39
- rescue
40
- raise NoMethodError, "undefined method `#{name}' for #{self}", caller(1)
42
+ def method_missing name
43
+ ConfigOption.wrap config.get name
44
+ rescue NoMethodError => error
45
+ if error.name == name then
46
+ raise NoMethodError, "undefined method `#{name}' for #{self}", caller(1)
47
+ else
48
+ raise
49
+ end
41
50
  end
42
51
 
43
52
  class ConfigOption < OpenStruct
53
+ def self.wrap data
54
+ if data.is_a? Hash then
55
+ ConfigOption.new data
56
+ else
57
+ data
58
+ end
59
+ end
60
+
61
+ def get name
62
+ if @table.include? name then
63
+ self.class.wrap @table[name]
64
+ else
65
+ raise ConfigOption::NotFound
66
+ end
67
+ end
68
+
69
+ class NotFound < RangeError; end
44
70
  end
45
71
  end
@@ -1,3 +1,3 @@
1
1
  module ConfigModule
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/test/test.rb CHANGED
@@ -39,3 +39,21 @@ end
39
39
  spec 'nil values are preserved' do
40
40
  FalseNil.n == nil
41
41
  end
42
+
43
+ spec 'missing keys raise exception when called as methods' do
44
+ begin
45
+ FalseNil.nonexistant
46
+ rescue ConfigModule::ConfigOption::NotFound
47
+ true
48
+ end
49
+ end
50
+
51
+ module MultipleExample
52
+ extend ConfigModule
53
+ config_file './config/example.yml'
54
+ namespace :production, :dictionary
55
+ end
56
+
57
+ spec 'multiple namespaces can be set' do
58
+ MultipleExample.configuration == ExampleConfig.dictionary.configuration
59
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: config_module
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-15 00:00:00.000000000 Z
12
+ date: 2013-02-27 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Wrap a configuration file in a module for easy use throughout your application.
15
15
  Inspired by Rails.
@@ -61,3 +61,4 @@ test_files:
61
61
  - test/example_config.rb
62
62
  - test/mutest.rb
63
63
  - test/test.rb
64
+ has_rdoc: