config_context 0.7.2 → 0.7.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.
@@ -16,33 +16,43 @@ module ConfigContext
16
16
  send(new_method, *arguments, &block)
17
17
  end
18
18
  end
19
-
19
+
20
+ def init
21
+ @config ||= Hash.new
22
+ end
20
23
 
21
24
  public
22
25
  def method_missing(method, *arguments, &block)
23
26
 
24
- @config ||= { }
27
+ @config || init
25
28
 
26
29
  case method.to_s
27
- when /(.+)=$/ then @config[method.to_s.delete("=").to_sym] = arguments.length == 1 ? arguments[0] : arguments
28
- when /(.+)\?$/ then @config.keys.include?(method.to_s.delete("=?").to_sym)
30
+ when /(.+)=$/ then
31
+ property_key = method.to_s.delete('=').to_sym
32
+
33
+ @config[property_key] = (arguments.size == 1) ? arguments[0] : arguments
34
+
35
+ when /(.+)\?$/ then
36
+ property_key = method.to_s.delete('?').to_sym
37
+
38
+ @config.keys.include?(property_key) #true/false
29
39
  else
30
40
 
31
- if @config.keys.include?(method)
41
+ if @config.keys.include?(method) # any type
32
42
  @config[method]
33
- else
43
+ else
34
44
  super
35
45
  end
36
46
  end
37
47
  end
38
48
 
39
49
  def erase!()
40
- @config = { }
50
+ @config = Hash.new
41
51
  end
42
52
 
43
53
  def configure(source=nil, options={}, &block)
44
54
 
45
- @config ||= { }
55
+ @config || init
46
56
 
47
57
  options = {:source=>nil, :context=>:root}.merge(options)
48
58
 
@@ -57,12 +67,13 @@ module ConfigContext
57
67
  end
58
68
  else
59
69
 
60
- @config[options[:context]] ||= { }
70
+ context = options[:context]
71
+ @config[context] ||= { } # New context
61
72
 
62
73
  case source
63
- when /\.(yml|yaml)/i then @config[options[:context]].merge!(YAML.load_file(source)) rescue raise ConfigError.new("Problems loading file: #{source}")
64
- when /\.json/i then @config[options[:context]].merge!(JSON.parse(File.read(source))) rescue raise ConfigError.new("Problems loading file: #{source}")
65
- when Hash then @config[options[:context]].merge!(source)
74
+ when /\.(yml|yaml)/i then @config[context].merge!(YAML.load_file(source)) rescue raise ConfigError.new("Problems loading file: #{source}")
75
+ when /\.json/i then @config[context].merge!(JSON.parse(File.read(source))) rescue raise ConfigError.new("Problems loading file: #{source}")
76
+ when Hash then @config[context].merge!(source)
66
77
  else
67
78
  yield self if block_given?
68
79
  end
@@ -72,24 +83,38 @@ module ConfigContext
72
83
  end
73
84
 
74
85
  def to_hash
75
- @config ||= { }
86
+ @config || init
76
87
  end
77
88
 
78
- def to_s
79
- "#{@config.inspect}"
80
- end
81
-
82
89
  def inspect
90
+
91
+ @config || init
83
92
  @config.inspect
84
93
  end
94
+
95
+ def to_s
96
+ "#{@config.inspect}"
97
+ end
85
98
 
86
99
  ##
87
100
  # Backward compability...
88
101
  def [](key)
89
- @config ||= { }
102
+
103
+ @config || init
90
104
  @config[key]
91
105
  end
92
106
 
107
+ def fetch(key,default=nil)
108
+
109
+ @config || init
110
+ if @config.include?(key)
111
+
112
+ @config[key]
113
+ else
114
+ default ? default : nil
115
+ end
116
+ end
117
+
93
118
  deprecate :load, :configure
94
119
  deprecate :all, :to_hash
95
120
  end
@@ -4,7 +4,7 @@ module ConfigContext
4
4
  INFO = {
5
5
  :major =>0,
6
6
  :minor =>7,
7
- :patch =>2
7
+ :patch =>3
8
8
  }
9
9
 
10
10
  def self.number(version_info=INFO)
@@ -1,4 +1,4 @@
1
- $:.unshift(File.join(File.dirname(__FILE__), %w[.. .. lib]))
1
+ $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
2
2
 
3
3
 
4
4
  require 'rubygems'
@@ -8,9 +8,11 @@ require 'config_context'
8
8
 
9
9
  class TestConfigContext < Test::Unit::TestCase
10
10
 
11
- TEST_SYMBOL = 'symbol value'
12
- TEST_LIST = [1, 2, 3]
13
- TEST_HASH = { "a"=>1, "b"=>2, "c"=>3 }
11
+ TEST_FILE_JSON = File.join(File.dirname(__FILE__), %w[fixtures test.json])
12
+ TEST_FILE_YAML = File.join(File.dirname(__FILE__), %w[fixtures test.yml])
13
+ TEST_SYMBOL = 'symbol value'
14
+ TEST_LIST = [1, 2, 3]
15
+ TEST_HASH = { "a"=>1, "b"=>2, "c"=>3 }
14
16
 
15
17
 
16
18
  def setup
@@ -29,6 +31,7 @@ class TestConfigContext < Test::Unit::TestCase
29
31
  ConfigContext.erase!
30
32
  end
31
33
 
34
+
32
35
  def test_configure_properties
33
36
 
34
37
  assert_equal(ConfigContext.mysymbol, TEST_SYMBOL)
@@ -37,6 +40,7 @@ class TestConfigContext < Test::Unit::TestCase
37
40
  assert_equal(ConfigContext.mysymbol, ConfigContext[:mysymbol])
38
41
  end
39
42
 
43
+
40
44
  def test_check_the_presence_of_some_properties
41
45
 
42
46
  assert(ConfigContext.mysymbol?)
@@ -44,6 +48,7 @@ class TestConfigContext < Test::Unit::TestCase
44
48
  assert(ConfigContext.myhash?)
45
49
  assert(!ConfigContext.thisdonotexist?)
46
50
  end
51
+
47
52
 
48
53
  def test_retrive_all_properties
49
54
 
@@ -53,6 +58,18 @@ class TestConfigContext < Test::Unit::TestCase
53
58
  :myhash =>TEST_HASH
54
59
  })
55
60
  end
61
+
62
+
63
+ def test_retrive_all_properties_with_defaults
64
+
65
+ assert(!ConfigContext.fetch(:mysymbol_))
66
+
67
+ assert_equal(ConfigContext.fetch(:mysymbol, "default value"), "symbol value")
68
+ assert_equal(ConfigContext.fetch(:mysymbol_, "default value"), "default value")
69
+ assert_equal(ConfigContext.fetch(:mysymbol_, [1,2,3]), [1,2,3])
70
+ assert_equal(ConfigContext.fetch(:mysymbol_, {:a=>'a', :b=>'b'}), {:a=>'a', :b=>'b'})
71
+ end
72
+
56
73
 
57
74
  def test_retrieve_all_property_keys
58
75
 
@@ -79,6 +96,7 @@ class TestConfigContext < Test::Unit::TestCase
79
96
  assert_equal(ConfigContext.myhash, new_hash)
80
97
  end
81
98
 
99
+
82
100
  def test_configure_from_a_hash
83
101
 
84
102
  config_hash = {
@@ -90,6 +108,7 @@ class TestConfigContext < Test::Unit::TestCase
90
108
  ConfigContext.configure(config_hash)
91
109
  assert_equal(ConfigContext.to_hash, config_hash)
92
110
  end
111
+
93
112
 
94
113
  def test_configuration_from_a_file_that_not_exist
95
114
 
@@ -98,6 +117,7 @@ class TestConfigContext < Test::Unit::TestCase
98
117
  assert_raises(ConfigContext::ConfigError) { ConfigContext.configure("this_file_do_not_exist.#{extension}") }
99
118
  end
100
119
  end
120
+
101
121
 
102
122
  def test_configuration_from_a_bad_format_file
103
123
 
@@ -109,24 +129,34 @@ class TestConfigContext < Test::Unit::TestCase
109
129
  :myhash =>TEST_HASH
110
130
  })
111
131
  end
132
+
133
+ ConfigContext.erase!
134
+
135
+ ['foo', 'bar', 'bazz'].each do |extension|
136
+
137
+ assert_equal(ConfigContext.configure("this_file_do_not_exist.#{extension}").to_hash, {})
138
+ end
112
139
  end
140
+
113
141
 
114
142
  def test_configure_from_a_yaml_file
115
143
 
116
- ConfigContext.configure(File.join(File.dirname(__FILE__), %w[ .. fixtures test.yml]))
144
+ ConfigContext.configure(TEST_FILE_YAML)
117
145
  assert_equal(ConfigContext.to_hash, {
118
- :mysymbol =>TEST_SYMBOL,
119
- :mylist =>TEST_LIST,
120
- :myhash =>TEST_HASH,
146
+ :mysymbol =>TEST_SYMBOL,
147
+ :mylist =>TEST_LIST,
148
+ :myhash =>TEST_HASH,
121
149
  "mysymbol" =>TEST_SYMBOL,
122
150
  "mylist" =>TEST_LIST,
123
151
  "myhash" =>TEST_HASH
124
152
  })
125
153
  end
154
+
126
155
 
127
156
  def test_configure_from_a_yaml_file_in_a_context
128
157
 
129
- ConfigContext.configure(File.join(File.dirname(__FILE__), %w[ .. fixtures test.yml]), :context=>:yaml)
158
+ ConfigContext.configure(TEST_FILE_YAML, :context=>:yaml)
159
+
130
160
  assert_equal(ConfigContext.to_hash, {
131
161
  :mysymbol =>TEST_SYMBOL,
132
162
  :mylist =>TEST_LIST,
@@ -137,24 +167,34 @@ class TestConfigContext < Test::Unit::TestCase
137
167
  "myhash" =>TEST_HASH
138
168
  }
139
169
  })
170
+
171
+ assert_equal(ConfigContext.yaml, {
172
+ "mysymbol" =>TEST_SYMBOL,
173
+ "mylist" =>TEST_LIST,
174
+ "myhash" =>TEST_HASH
175
+ })
140
176
  end
141
177
 
178
+
142
179
  def test_configure_from_a_json_file
143
180
 
144
- ConfigContext.configure(File.join(File.dirname(__FILE__), %w[ .. fixtures test.json]))
181
+ ConfigContext.configure(TEST_FILE_JSON)
182
+
145
183
  assert_equal(ConfigContext.to_hash, {
146
- :mysymbol =>TEST_SYMBOL,
147
- :mylist =>TEST_LIST,
148
- :myhash =>TEST_HASH,
184
+ :mysymbol =>TEST_SYMBOL,
185
+ :mylist =>TEST_LIST,
186
+ :myhash =>TEST_HASH,
149
187
  "mysymbol" =>TEST_SYMBOL,
150
188
  "mylist" =>TEST_LIST,
151
189
  "myhash" =>TEST_HASH
152
190
  })
153
191
  end
154
192
 
193
+
155
194
  def test_configure_from_a_json_file_in_a_context
156
195
 
157
- ConfigContext.configure(File.join(File.dirname(__FILE__), %w[ .. fixtures test.json]), :context=>:json)
196
+ ConfigContext.configure(TEST_FILE_JSON, :context=>:json)
197
+
158
198
  assert_equal(ConfigContext.to_hash, {
159
199
  :mysymbol =>TEST_SYMBOL,
160
200
  :mylist =>TEST_LIST,
@@ -165,5 +205,11 @@ class TestConfigContext < Test::Unit::TestCase
165
205
  "myhash" =>TEST_HASH
166
206
  }
167
207
  })
208
+
209
+ assert_equal(ConfigContext.json, {
210
+ "mysymbol" =>TEST_SYMBOL,
211
+ "mylist" =>TEST_LIST,
212
+ "myhash" =>TEST_HASH
213
+ })
168
214
  end
169
215
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: config_context
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.7.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-06-30 00:00:00.000000000Z
12
+ date: 2011-07-01 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &2153672740 !ruby/object:Gem::Requirement
16
+ requirement: &2156468160 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2153672740
24
+ version_requirements: *2156468160
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: jeweler
27
- requirement: &2153671400 !ruby/object:Gem::Requirement
27
+ requirement: &2156460860 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2153671400
35
+ version_requirements: *2156460860
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rcov
38
- requirement: &2153670380 !ruby/object:Gem::Requirement
38
+ requirement: &2156459540 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2153670380
46
+ version_requirements: *2156459540
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: json
49
- requirement: &2153669100 !ruby/object:Gem::Requirement
49
+ requirement: &2156458460 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *2153669100
57
+ version_requirements: *2156458460
58
58
  description: My config DSL
59
59
  email: javier.juarez@gmail.com
60
60
  executables: []
@@ -65,7 +65,7 @@ extra_rdoc_files:
65
65
  files:
66
66
  - lib/config_context.rb
67
67
  - lib/version.rb
68
- - test/unit/test_config_context.rb
68
+ - test/test_config_context.rb
69
69
  - LICENSE
70
70
  - README.rdoc
71
71
  homepage: http://github.com/jjuarez/config_context