config_context 0.6.2 → 0.7.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.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010-2030 Javier Juarez Martinez
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -5,79 +5,58 @@ Your minimal and DSL config context...
5
5
 
6
6
  == Installing
7
7
 
8
- The latest stable version is published in gemcutter.
8
+ The latest stable version is published in rubygems.
9
9
 
10
10
  gem install config_context
11
11
 
12
12
 
13
- == How to use, some useful examples
13
+ == How to use, well, look this useful examples:
14
14
 
15
15
  require 'rubygems'
16
16
  require 'config_context'
17
17
 
18
18
  ##
19
19
  # Puts all your configuration context in a block
20
- #
21
20
  ConfigContext.configure do |config|
22
21
 
23
22
  config.a = "Value of a"
24
23
  config.b = "Value of b"
25
- config.c = { also:=>"complex", :values=>nil }
24
+ config.c = { also:=>"complex", :values=>['like', 'this'] }
26
25
  end
27
26
 
28
27
  ##
29
28
  # Retrieve your properties
30
- #
31
29
  puts ConfigContext.a
32
30
  puts ConfigContext.b
33
31
  puts ConfigContext.c
34
32
 
35
33
  ##
36
34
  # Check the presence of a property
37
- #
38
- puts ConfigContext.b if( ConfigContext.b? )
35
+ puts ConfigContext.b if ConfigContext.b?
39
36
 
40
37
  ##
41
38
  # Load your config from a YAML file
42
- #
43
39
  begin
44
- ConfigContext.load( "settings.yml" )
40
+ ConfigContext.configure("settings.yml")
45
41
  rescue ConfigContext::Error => e
46
- fail( e.message )
42
+ fail e.message
47
43
  end
48
-
49
- puts ConfigContext.a if( ConfigContext.a? )
50
- puts ConfigContext.to_hash.inspect
51
44
 
45
+ ##
46
+ # Load your config from a JSON file
47
+ begin
48
+ ConfigContext.configure("settings.json")
49
+ rescue ConfigContext::Error => e
50
+ fail e.message
51
+ end
52
+
53
+ puts ConfigContext
52
54
 
53
55
  ##
54
- #
55
- #
56
+ # Reset the context !!!
57
+ ConfigContext.erase!
58
+ ConfigContext.to_hash == {}
56
59
 
57
60
  == TODO
58
61
 
59
- * ???
60
-
61
-
62
- == License
63
-
64
- Copyright (c) 2010-2030 Javier Juarez Martinez
65
-
66
- Permission is hereby granted, free of charge, to any person obtaining
67
- a copy of this software and associated documentation files (the
68
- "Software"), to deal in the Software without restriction, including
69
- without limitation the rights to use, copy, modify, merge, publish,
70
- distribute, sublicense, and/or sell copies of the Software, and to
71
- permit persons to whom the Software is furnished to do so, subject to
72
- the following conditions:
73
-
74
- The above copyright notice and this permission notice shall be
75
- included in all copies or substantial portions of the Software.
76
-
77
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
78
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
79
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
80
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
81
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
82
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
83
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
62
+ * Sugestions are wellcome guys
@@ -1,27 +1,13 @@
1
1
  require 'yaml'
2
+ require 'json'
2
3
 
3
4
 
4
5
  module ConfigContext
5
6
  extend self
6
7
 
7
- @config ||= {}
8
-
9
8
  class ConfigError < StandardError; end
10
9
 
11
-
12
10
  private
13
- def _add_property(property, *arguments)
14
- @config[property.to_s.delete("=?").to_sym] = arguments.length == 1 ? arguments[0] : arguments
15
- end
16
-
17
- def _exist_property?(property)
18
- @config.keys.include?(property.to_s.delete("=?").to_sym)
19
- end
20
-
21
- def _get_property(property)
22
- @config[property]
23
- end
24
-
25
11
  def self.deprecate(old_method, new_method)
26
12
 
27
13
  define_method(old_method) do |*arguments, &block|
@@ -35,37 +21,89 @@ module ConfigContext
35
21
  public
36
22
  def method_missing(method, *arguments, &block)
37
23
 
24
+ @config ||= { }
25
+
38
26
  case method.to_s
39
- when /(.+)=$/ then _add_property(method, *arguments)
40
- when /(.+)\?$/ then _exist_property?(method)
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)
41
29
  else
42
- _get_property(method)
43
- end
30
+
31
+ if @config.keys.include?(method)
32
+ @config[method]
33
+ else
34
+ super
35
+ end
36
+ end
37
+ end
38
+
39
+ def erase!()
40
+ @config = { }
44
41
  end
45
42
 
46
- def configure(*arguments, &block)
47
-
48
- case arguments[0]
49
- when /\.(yml|yaml|conf|config)/i
50
- @config.merge!(YAML.load_file(arguments[0]))
51
- when Hash
52
- @config.merge!(arguments[0])
53
- else
43
+ # def configure(*arguments, &block)
44
+ #
45
+ # @config ||= { }
46
+ #
47
+ # source = arguments[0]
48
+ #
49
+ # case source
50
+ # when /\.(yml|yaml)/i then @config.merge!(YAML.load_file(source)) rescue raise ConfigError.new("Problems loading the config file")
51
+ # when /\.json/i then @config.merge!(JSON.parse(File.read(source))) rescue raise ConfigError.new("Problems loading the config file")
52
+ # when Hash then @config.merge!(source)
53
+ # when Symbol then @config[source.to_sym] = { }
54
+ # else yield self if block_given?
55
+ # end
56
+ #
57
+ # self
58
+ # end
59
+
60
+ def configure(source=nil, options={}, &block)
61
+
62
+ @config ||= { }
63
+
64
+ options = {:source=>nil, :context=>:root}.merge(options)
65
+
66
+ if options[:context] == :root
67
+
68
+ case source
69
+ when /\.(yml|yaml)/i then @config.merge!(YAML.load_file(source)) rescue raise ConfigError.new("Problems loading file")
70
+ when /\.json/i then @config.merge!(JSON.parse(File.read(source))) rescue raise ConfigError.new("Problems loading file")
71
+ when Hash then @config.merge!(source)
72
+ else
54
73
  yield self if block_given?
74
+ end
75
+ else
76
+
77
+ @config[options[:context]] ||= { }
78
+
79
+ case source
80
+ when /\.(yml|yaml)/i then @config[options[:context]].merge!(YAML.load_file(source)) rescue raise ConfigError.new("Problems loading file")
81
+ when /\.json/i then @config[options[:context]].merge!(JSON.parse(File.read(source))) rescue raise ConfigError.new("Problems loading file")
82
+ when Hash then @config[options[:context]].merge!(source)
83
+ else
84
+ yield self if block_given?
85
+ end
55
86
  end
56
87
 
57
88
  self
58
- rescue StandardError=>e
59
- raise ConfigError.new(e.message)
60
89
  end
61
90
 
62
- def to_hash()
63
- @config
91
+ def to_hash
92
+ @config ||= { }
93
+ end
94
+
95
+ def to_s
96
+ "#{@config.inspect}"
97
+ end
98
+
99
+ def inspect
100
+ @config.inspect
64
101
  end
65
102
 
66
103
  ##
67
104
  # Backward compability...
68
105
  def [](key)
106
+ @config ||= { }
69
107
  @config[key]
70
108
  end
71
109
 
@@ -3,8 +3,8 @@ module ConfigContext
3
3
 
4
4
  INFO = {
5
5
  :major =>0,
6
- :minor =>6,
7
- :patch =>2
6
+ :minor =>7,
7
+ :patch =>0
8
8
  }
9
9
 
10
10
  def self.number(version_info=INFO)
@@ -1,108 +1,169 @@
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'
5
5
  require 'test/unit'
6
- require 'shoulda'
7
6
  require 'config_context'
8
7
 
9
8
 
10
9
  class TestConfigContext < Test::Unit::TestCase
11
10
 
12
- context "A ConfigContext" do
11
+ TEST_SYMBOL = 'symbol value'
12
+ TEST_LIST = [1, 2, 3]
13
+ TEST_HASH = { "a"=>1, "b"=>2, "c"=>3 }
14
+
15
+
16
+ def setup
13
17
 
14
- TEST_SYMBOL = 'symbol value'
15
- TEST_LIST = [1, 2, 3]
16
- TEST_HASH = { :a=>1, :b=>2, :c=>3 }
17
-
18
- setup do
19
-
20
- ConfigContext.configure do |config|
21
-
22
- config.mysymbol = TEST_SYMBOL
23
- config.mylist = TEST_LIST
24
- config.myhash = TEST_HASH
25
- end
18
+ ConfigContext.configure do |config|
19
+
20
+ config.mysymbol = TEST_SYMBOL
21
+ config.mylist = TEST_LIST
22
+ config.myhash = TEST_HASH
26
23
  end
24
+ end
25
+
26
+
27
+ def teardown
27
28
 
28
- should "configure properties" do
29
+ ConfigContext.erase!
30
+ end
31
+
32
+ def test_configure_properties
33
+
34
+ assert_equal(ConfigContext.mysymbol, TEST_SYMBOL)
35
+ assert_equal(ConfigContext.mylist, TEST_LIST)
36
+ assert_equal(ConfigContext.myhash, TEST_HASH)
37
+ assert_equal(ConfigContext.mysymbol, ConfigContext[:mysymbol])
38
+ end
39
+
40
+ def test_check_the_presence_of_some_properties
41
+
42
+ assert(ConfigContext.mysymbol?)
43
+ assert(ConfigContext.mylist?)
44
+ assert(ConfigContext.myhash?)
45
+ assert(!ConfigContext.thisdonotexist?)
46
+ end
47
+
48
+ def test_retrive_all_properties
49
+
50
+ assert_equal(ConfigContext.to_hash, {
51
+ :mysymbol =>TEST_SYMBOL,
52
+ :mylist =>TEST_LIST,
53
+ :myhash =>TEST_HASH
54
+ })
55
+ end
56
+
57
+ def test_retrieve_all_property_keys
58
+
59
+ [:mysymbol, :mylist, :myhash].each do |key|
29
60
 
30
- assert_equal( ConfigContext.mysymbol, TEST_SYMBOL )
31
- assert_equal( ConfigContext.mylist, TEST_LIST )
32
- assert_equal( ConfigContext.myhash, TEST_HASH )
33
- assert_equal( ConfigContext.mysymbol, ConfigContext[:mysymbol])
61
+ assert(ConfigContext.to_hash.keys.include?(key))
34
62
  end
63
+ end
35
64
 
36
- should "check the presence of some properties" do
65
+ def test_update_properties
37
66
 
38
- assert( ConfigContext.mysymbol? )
39
- assert( ConfigContext.mylist? )
40
- assert( ConfigContext.myhash? )
67
+ assert_equal(ConfigContext.mysymbol, TEST_SYMBOL)
68
+ ConfigContext.mysymbol = "A"
69
+ assert_equal(ConfigContext.mysymbol, "A")
41
70
 
42
- assert( !ConfigContext.thisdonotexist? )
43
- end
71
+ new_test_list = [4,5,6]
72
+ assert_equal(ConfigContext.mylist, TEST_LIST)
73
+ ConfigContext.mylist = new_test_list
74
+ assert_equal(ConfigContext.mylist, new_test_list)
75
+
76
+ new_hash = { :d=>4, :e=>5, :f=>6 }
77
+ assert_equal(ConfigContext.myhash, TEST_HASH)
78
+ ConfigContext.myhash = new_hash
79
+ assert_equal(ConfigContext.myhash, new_hash)
80
+ end
81
+
82
+ def test_configure_from_a_hash
44
83
 
45
- should "retrive all properties" do
46
-
47
- assert_equal( ConfigContext.to_hash, {
48
- :mysymbol =>TEST_SYMBOL,
49
- :mylist =>TEST_LIST,
50
- :myhash =>TEST_HASH
51
- } )
52
- end
84
+ config_hash = {
85
+ :mysymbol =>TEST_SYMBOL,
86
+ :mylist =>TEST_LIST,
87
+ :myhash =>TEST_HASH
88
+ }
53
89
 
54
- should "retrieve all property keys" do
55
-
56
- [ :mysymbol, :mylist, :myhash ].each do |key|
57
-
58
- assert( ConfigContext.to_hash.keys.include?( key ) )
59
- end
90
+ ConfigContext.configure(config_hash)
91
+ assert_equal(ConfigContext.to_hash, config_hash)
92
+ end
93
+
94
+ def test_configuration_from_a_file_that_not_exist
95
+
96
+ ['json', 'yaml', 'yml'].each do |extension|
97
+
98
+ assert_raises(ConfigContext::ConfigError) { ConfigContext.configure("this_file_do_not_exist.#{extension}") }
60
99
  end
61
-
62
- should "configure from a Yaml file" do
63
-
64
- [ "yml", "yaml", "conf", "config" ].each do |extension|
65
-
66
- assert_raises( ConfigContext::ConfigError ) { ConfigContext.configure( "this_file_do_not_exist.#{extension}" ) }
67
- end
68
-
69
- assert_equal( ConfigContext.to_hash, ConfigContext.configure( "total_foo_bar.file" ).to_hash )
70
-
71
- ConfigContext.configure( File.join( File.dirname( __FILE__ ), %w[ .. fixtures test.yml] ) )
72
- assert_equal( ConfigContext.to_hash, {
100
+ end
101
+
102
+ def test_configuration_from_a_bad_format_file
103
+
104
+ ['foo', 'bar', 'bazz'].each do |extension|
105
+
106
+ assert_equal(ConfigContext.configure("this_file_do_not_exist.#{extension}").to_hash, {
73
107
  :mysymbol =>TEST_SYMBOL,
74
108
  :mylist =>TEST_LIST,
75
109
  :myhash =>TEST_HASH
76
- } )
110
+ })
77
111
  end
78
-
79
- should "configurre from a Hash" do
80
-
81
- config_hash = {
82
- :mysymbol =>TEST_SYMBOL,
83
- :mylist =>TEST_LIST,
84
- :myhash =>TEST_HASH
112
+ end
113
+
114
+ def test_configure_from_a_yaml_file
115
+
116
+ ConfigContext.configure(File.join(File.dirname(__FILE__), %w[ .. fixtures test.yml]))
117
+ assert_equal(ConfigContext.to_hash, {
118
+ :mysymbol =>TEST_SYMBOL,
119
+ :mylist =>TEST_LIST,
120
+ :myhash =>TEST_HASH,
121
+ "mysymbol" =>TEST_SYMBOL,
122
+ "mylist" =>TEST_LIST,
123
+ "myhash" =>TEST_HASH
124
+ })
125
+ end
126
+
127
+ def test_configure_from_a_yaml_file_in_a_context
128
+
129
+ ConfigContext.configure(File.join(File.dirname(__FILE__), %w[ .. fixtures test.yml]), :context=>:yaml)
130
+ assert_equal(ConfigContext.to_hash, {
131
+ :mysymbol =>TEST_SYMBOL,
132
+ :mylist =>TEST_LIST,
133
+ :myhash =>TEST_HASH,
134
+ :yaml=>{
135
+ "mysymbol" =>TEST_SYMBOL,
136
+ "mylist" =>TEST_LIST,
137
+ "myhash" =>TEST_HASH
85
138
  }
86
-
87
- ConfigContext.configure( config_hash )
88
- assert_equal( ConfigContext.to_hash, config_hash )
89
- end
90
-
91
- should "update properties" do
92
-
93
- assert_equal( ConfigContext.mysymbol, TEST_SYMBOL )
94
- ConfigContext.mysymbol = "A"
95
- assert_equal( ConfigContext.mysymbol, "A" )
139
+ })
140
+ end
96
141
 
97
- new_test_list = [4,5,6]
98
- assert_equal( ConfigContext.mylist, TEST_LIST )
99
- ConfigContext.mylist = new_test_list
100
- assert_equal( ConfigContext.mylist, new_test_list )
142
+ def test_configure_from_a_json_file
143
+
144
+ ConfigContext.configure(File.join(File.dirname(__FILE__), %w[ .. fixtures test.json]))
145
+ assert_equal(ConfigContext.to_hash, {
146
+ :mysymbol =>TEST_SYMBOL,
147
+ :mylist =>TEST_LIST,
148
+ :myhash =>TEST_HASH,
149
+ "mysymbol" =>TEST_SYMBOL,
150
+ "mylist" =>TEST_LIST,
151
+ "myhash" =>TEST_HASH
152
+ })
153
+ end
101
154
 
102
- new_hash = { :d=>4, :e=>5, :f=>6 }
103
- assert_equal( ConfigContext.myhash, TEST_HASH )
104
- ConfigContext.myhash = new_hash
105
- assert_equal( ConfigContext.myhash, new_hash )
106
- end
155
+ def test_configure_from_a_json_file_in_a_context
156
+
157
+ ConfigContext.configure(File.join(File.dirname(__FILE__), %w[ .. fixtures test.json]), :context=>:json)
158
+ assert_equal(ConfigContext.to_hash, {
159
+ :mysymbol =>TEST_SYMBOL,
160
+ :mylist =>TEST_LIST,
161
+ :myhash =>TEST_HASH,
162
+ :json=>{
163
+ "mysymbol" =>TEST_SYMBOL,
164
+ "mylist" =>TEST_LIST,
165
+ "myhash" =>TEST_HASH
166
+ }
167
+ })
107
168
  end
108
169
  end
metadata CHANGED
@@ -1,81 +1,96 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: config_context
3
- version: !ruby/object:Gem::Version
4
- hash: 3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.7.0
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 6
9
- - 2
10
- version: 0.6.2
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Javier Juarez
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-06-20 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
21
- requirement: &id001 !ruby/object:Gem::Requirement
12
+ date: 2011-06-22 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: &2156984360 !ruby/object:Gem::Requirement
22
17
  none: false
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- hash: 3
27
- segments:
28
- - 0
29
- version: "0"
30
- version_requirements: *id001
31
- type: :development
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2156984360
25
+ - !ruby/object:Gem::Dependency
32
26
  name: jeweler
27
+ requirement: &2156983600 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2156983600
36
+ - !ruby/object:Gem::Dependency
37
+ name: rcov
38
+ requirement: &2156982360 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2156982360
47
+ - !ruby/object:Gem::Dependency
48
+ name: json
49
+ requirement: &2156978880 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
33
56
  prerelease: false
57
+ version_requirements: *2156978880
34
58
  description: My config DSL
35
59
  email: javier.juarez@gmail.com
36
60
  executables: []
37
-
38
61
  extensions: []
39
-
40
- extra_rdoc_files:
62
+ extra_rdoc_files:
63
+ - LICENSE
41
64
  - README.rdoc
42
- files:
65
+ files:
43
66
  - lib/config_context.rb
44
67
  - lib/version.rb
45
68
  - test/unit/test_config_context.rb
69
+ - LICENSE
46
70
  - README.rdoc
47
71
  homepage: http://github.com/jjuarez/config_context
48
- licenses:
72
+ licenses:
49
73
  - MIT
50
74
  post_install_message:
51
75
  rdoc_options: []
52
-
53
- require_paths:
76
+ require_paths:
54
77
  - lib
55
- required_ruby_version: !ruby/object:Gem::Requirement
78
+ required_ruby_version: !ruby/object:Gem::Requirement
56
79
  none: false
57
- requirements:
58
- - - ">="
59
- - !ruby/object:Gem::Version
60
- hash: 3
61
- segments:
62
- - 0
63
- version: "0"
64
- required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
85
  none: false
66
- requirements:
67
- - - ">="
68
- - !ruby/object:Gem::Version
69
- hash: 3
70
- segments:
71
- - 0
72
- version: "0"
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
73
90
  requirements: []
74
-
75
91
  rubyforge_project: http://github.com/jjuarez/config_context
76
92
  rubygems_version: 1.8.5
77
93
  signing_key:
78
94
  specification_version: 3
79
95
  summary: A Config Context for little applications
80
96
  test_files: []
81
-