config_context 0.2.1 → 0.3.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/README.rdoc +40 -25
- data/lib/config_context.rb +21 -46
- data/lib/version.rb +2 -2
- data/test/unit/tc_config_context.rb +81 -24
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -2,50 +2,65 @@
|
|
2
2
|
|
3
3
|
Your DSL config context...
|
4
4
|
|
5
|
+
|
5
6
|
== Installing
|
6
7
|
|
7
8
|
The latest stable version is published in gemcutter.
|
8
9
|
|
9
|
-
|
10
|
-
|
10
|
+
gem install config_context
|
11
|
+
|
11
12
|
|
12
13
|
== How to use
|
13
14
|
|
14
15
|
require 'rubygems'
|
15
|
-
require '
|
16
|
-
|
16
|
+
require 'config_context'
|
17
|
+
|
17
18
|
##
|
18
|
-
#
|
19
|
-
|
19
|
+
# Puts all your configuration context in a block
|
20
|
+
#
|
21
|
+
ConfigContext.configure do |config|
|
20
22
|
|
21
|
-
|
22
|
-
|
23
|
+
config.a = "Value of a"
|
24
|
+
config.b = "Value of b"
|
25
|
+
config.c = { also:=>"complex", :values=>nil }
|
26
|
+
config[:d] = [ "Value", "of", :d ]
|
27
|
+
config['String key'] = "My value"
|
23
28
|
end
|
24
|
-
|
29
|
+
|
25
30
|
##
|
26
|
-
#
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
31
|
+
# Retrieve your properties
|
32
|
+
#
|
33
|
+
puts ConfigContext.a
|
34
|
+
puts ConfigContext.b
|
35
|
+
puts ConfigContext.c
|
36
|
+
puts ConfigContext.d
|
37
|
+
puts ConfigContext[:d]
|
38
|
+
puts ConfigContext['String key']
|
39
|
+
|
40
|
+
##
|
41
|
+
# Check the presence of a property
|
42
|
+
#
|
43
|
+
puts ConfigContext.b if( ConfigContext.b? )
|
44
|
+
|
33
45
|
##
|
34
|
-
#
|
46
|
+
# Load your config from a YAML file and overrides some properties...
|
47
|
+
#
|
35
48
|
begin
|
36
|
-
|
37
|
-
rescue
|
38
|
-
fail(
|
49
|
+
ConfigContext.load( "settings.yml", :allow_collisions=>true )
|
50
|
+
rescue ConfigContext::Error => e
|
51
|
+
fail( e.message )
|
39
52
|
end
|
40
|
-
|
41
|
-
puts
|
42
|
-
puts
|
43
|
-
puts
|
53
|
+
|
54
|
+
puts ConfigContext.a if( ConfigContext.a? )
|
55
|
+
puts ConfigContext.all.inspect
|
56
|
+
puts ConfigContext.keys
|
44
57
|
|
58
|
+
|
45
59
|
== TODO
|
46
60
|
|
47
61
|
* Document all this stuff in english... I promise
|
48
|
-
|
62
|
+
|
63
|
+
|
49
64
|
== License
|
50
65
|
|
51
66
|
Copyright (c) 2010-2030 Javier Juarez Martinez
|
data/lib/config_context.rb
CHANGED
@@ -4,68 +4,43 @@ require 'yaml'
|
|
4
4
|
module ConfigContext
|
5
5
|
extend self
|
6
6
|
|
7
|
-
|
7
|
+
@config = { }
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
@config ||= {}
|
12
|
-
end
|
13
|
-
|
9
|
+
class Error < StandardError; end
|
14
10
|
|
15
11
|
def method_missing( method, *arguments, &block )
|
16
12
|
|
17
|
-
|
18
|
-
|
19
|
-
if( method =~ /(.+)=$/)
|
13
|
+
if( method =~ /(.+)=$/ )
|
20
14
|
|
21
|
-
config_key = method.to_s.delete( '
|
15
|
+
config_key = method.to_s.delete( '=' ).to_sym
|
22
16
|
@config[config_key] = (arguments.length == 1) ? arguments[0] : arguments
|
17
|
+
elsif( method =~ /(.+)\?$/ )
|
18
|
+
|
19
|
+
@config.has_key?( method.to_s.delete( '?' ).to_sym )
|
23
20
|
else
|
24
|
-
|
21
|
+
@config[method] if @config.has_key?( method )
|
25
22
|
end
|
26
23
|
end
|
27
24
|
|
25
|
+
def configure; yield self; end
|
28
26
|
|
29
|
-
def
|
30
|
-
|
31
|
-
yield self
|
32
|
-
end
|
33
|
-
|
34
|
-
|
35
|
-
def []( key )
|
36
|
-
|
37
|
-
self.init unless @config
|
38
|
-
|
39
|
-
return @config[key] if @config[key]
|
40
|
-
nil
|
41
|
-
end
|
42
|
-
|
43
|
-
|
44
|
-
def[]=( key, value )
|
45
|
-
|
46
|
-
self.init unless @config
|
47
|
-
|
48
|
-
@config[key]=value
|
49
|
-
end
|
50
|
-
|
27
|
+
def []( key ) return @config[key]; end
|
51
28
|
|
52
|
-
def
|
53
|
-
|
54
|
-
self.init unless @config
|
29
|
+
def[]=( key, value ) @config[key] = value; end
|
55
30
|
|
56
|
-
|
57
|
-
|
31
|
+
def all; @config; end
|
32
|
+
|
33
|
+
def keys; @config.keys; end
|
58
34
|
|
59
35
|
|
60
|
-
def load( config_file )
|
36
|
+
def load( config_file, options = { :allow_collisions => true } )
|
61
37
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
38
|
+
if( options[:allow_collisions] )
|
39
|
+
@config.merge!( YAML.load_file( config_file ) )
|
40
|
+
else
|
41
|
+
@config.merge!( YAML.load_file( config_file ) ) { |key, original_value, new_value| original_value }
|
42
|
+
end
|
66
43
|
rescue Exception => e
|
67
|
-
raise
|
68
|
-
else
|
69
|
-
@config
|
44
|
+
raise ConfigContext::Error.new( e.message )
|
70
45
|
end
|
71
46
|
end
|
data/lib/version.rb
CHANGED
@@ -11,51 +11,108 @@ class TestConfigContext < Test::Unit::TestCase
|
|
11
11
|
|
12
12
|
context "A ConfigContext" do
|
13
13
|
|
14
|
+
TEST_SYMBOL = 'symbol'
|
15
|
+
TEST_LIST = [1, 2, 3]
|
16
|
+
TEST_HASH = { :a=>1, :b=>2, :c=>3 }
|
17
|
+
TEST_STRING = 'string'
|
18
|
+
TEST_OTHERSYMBOL = "othersymbol"
|
19
|
+
|
20
|
+
|
14
21
|
setup do
|
15
22
|
|
16
23
|
ConfigContext.configure do |config|
|
17
24
|
|
18
|
-
config.
|
19
|
-
config.mylist
|
20
|
-
config.myhash
|
25
|
+
config.mysymbol = TEST_SYMBOL
|
26
|
+
config.mylist = TEST_LIST
|
27
|
+
config.myhash = TEST_HASH
|
21
28
|
end
|
22
29
|
|
23
|
-
ConfigContext[
|
24
|
-
ConfigContext[:
|
30
|
+
ConfigContext[TEST_STRING] = TEST_STRING
|
31
|
+
ConfigContext[:othersymbol] = TEST_OTHERSYMBOL
|
25
32
|
end
|
26
33
|
|
27
|
-
should "
|
34
|
+
should "configure properties" do
|
28
35
|
|
29
|
-
assert_equal( ConfigContext.
|
30
|
-
assert_equal( ConfigContext.mylist,
|
31
|
-
assert_equal( ConfigContext.myhash,
|
32
|
-
assert_equal( ConfigContext[
|
33
|
-
assert_equal( ConfigContext.
|
34
|
-
assert_equal( ConfigContext[:
|
36
|
+
assert_equal( ConfigContext.mysymbol, TEST_SYMBOL )
|
37
|
+
assert_equal( ConfigContext.mylist, TEST_LIST )
|
38
|
+
assert_equal( ConfigContext.myhash, TEST_HASH )
|
39
|
+
assert_equal( ConfigContext[TEST_STRING], TEST_STRING )
|
40
|
+
assert_equal( ConfigContext.othersymbol, TEST_OTHERSYMBOL )
|
41
|
+
assert_equal( ConfigContext[:othersymbol], TEST_OTHERSYMBOL )
|
35
42
|
end
|
36
|
-
|
37
|
-
should "
|
43
|
+
|
44
|
+
should "check the presence of some properties" do
|
45
|
+
|
46
|
+
assert( ConfigContext.mysymbol? )
|
47
|
+
assert( ConfigContext.mylist? )
|
48
|
+
assert( ConfigContext.myhash? )
|
49
|
+
assert( ConfigContext.othersymbol? )
|
50
|
+
|
51
|
+
assert( !ConfigContext.string? ) # Pay attention to this behaviour!!!
|
52
|
+
|
53
|
+
assert( !ConfigContext.thisdonotexist? )
|
54
|
+
end
|
55
|
+
|
56
|
+
should "retrive all properties" do
|
38
57
|
|
39
|
-
assert_equal( ConfigContext.all, {
|
58
|
+
assert_equal( ConfigContext.all, {
|
59
|
+
:mysymbol =>TEST_SYMBOL,
|
60
|
+
:mylist =>TEST_LIST,
|
61
|
+
:myhash =>TEST_HASH,
|
62
|
+
'string' =>TEST_STRING,
|
63
|
+
:othersymbol =>TEST_OTHERSYMBOL
|
64
|
+
} )
|
40
65
|
end
|
41
|
-
|
42
|
-
should "Test yaml load" do
|
43
66
|
|
67
|
+
should "retrieve all property keys" do
|
68
|
+
|
69
|
+
assert_equal( ConfigContext.keys, [ :mysymbol, :mylist, :myhash, 'string', :othersymbol ] )
|
70
|
+
end
|
71
|
+
|
72
|
+
should "load a Yaml file without keys collisions" do
|
73
|
+
|
44
74
|
ConfigContext.load( File.join( File.dirname( __FILE__ ), %w[ .. fixtures test.yml] ) )
|
45
|
-
assert_equal( ConfigContext.all, {
|
75
|
+
assert_equal( ConfigContext.all, {
|
76
|
+
:mysymbol =>TEST_SYMBOL,
|
77
|
+
:mylist =>TEST_LIST,
|
78
|
+
:myhash =>TEST_HASH,
|
79
|
+
'string' =>TEST_STRING,
|
80
|
+
:othersymbol =>TEST_OTHERSYMBOL
|
81
|
+
} )
|
82
|
+
end
|
83
|
+
|
84
|
+
should "load a Yaml file with key collisions" do
|
85
|
+
|
86
|
+
original_value = "The Original value is here"
|
87
|
+
|
88
|
+
ConfigContext.configure { |config| config.mysymbol = original_value }
|
89
|
+
|
90
|
+
ConfigContext.load( File.join( File.dirname( __FILE__ ), %w[ .. fixtures test.yml] ), :allow_collisions=>false )
|
91
|
+
assert_equal( ConfigContext.all, {
|
92
|
+
:mysymbol =>original_value,
|
93
|
+
:mylist =>TEST_LIST,
|
94
|
+
:myhash =>TEST_HASH,
|
95
|
+
'string' =>TEST_STRING,
|
96
|
+
:othersymbol =>TEST_OTHERSYMBOL
|
97
|
+
} )
|
98
|
+
end
|
99
|
+
|
100
|
+
should "load a Yaml file with error" do
|
101
|
+
|
102
|
+
assert_raises( ConfigContext::Error ) { ConfigContext.load( "very bad file.yml" ) }
|
46
103
|
end
|
47
104
|
|
48
|
-
should "
|
105
|
+
should "update properties" do
|
49
106
|
|
50
|
-
assert_equal( ConfigContext.
|
51
|
-
ConfigContext.
|
52
|
-
assert_equal( ConfigContext.
|
107
|
+
assert_equal( ConfigContext.mysymbol, TEST_SYMBOL )
|
108
|
+
ConfigContext.mysymbol = "A"
|
109
|
+
assert_equal( ConfigContext.mysymbol, "A" )
|
53
110
|
|
54
|
-
assert_equal( ConfigContext.mylist,
|
111
|
+
assert_equal( ConfigContext.mylist, TEST_LIST )
|
55
112
|
ConfigContext.mylist = [4,5,6]
|
56
113
|
assert_equal( ConfigContext.mylist, [4,5,6] )
|
57
114
|
|
58
|
-
assert_equal( ConfigContext.myhash,
|
115
|
+
assert_equal( ConfigContext.myhash, TEST_HASH )
|
59
116
|
ConfigContext.myhash= { :d=>4, :e=>5, :f=>6 }
|
60
117
|
assert_equal( ConfigContext.myhash, { :d=>4, :e=>5, :f=>6 } )
|
61
118
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: config_context
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.3.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Javier Juarez
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-03-
|
13
|
+
date: 2011-03-15 00:00:00 +01:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|