config_context 0.3.4 → 0.3.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +6 -15
- data/lib/config_context.rb +31 -33
- data/lib/version.rb +2 -3
- data/test/unit/test_config_context.rb +18 -53
- metadata +31 -5
data/README.rdoc
CHANGED
@@ -2,13 +2,11 @@
|
|
2
2
|
|
3
3
|
Your DSL config context...
|
4
4
|
|
5
|
-
|
6
5
|
== Installing
|
7
6
|
|
8
7
|
The latest stable version is published in gemcutter.
|
9
8
|
|
10
9
|
gem install config_context
|
11
|
-
|
12
10
|
|
13
11
|
== How to use
|
14
12
|
|
@@ -20,11 +18,9 @@ The latest stable version is published in gemcutter.
|
|
20
18
|
#
|
21
19
|
ConfigContext.configure do |config|
|
22
20
|
|
23
|
-
config.a
|
24
|
-
config.b
|
25
|
-
config.c
|
26
|
-
config[:d] = [ "Value", "of", :d ]
|
27
|
-
config['String key'] = "My value"
|
21
|
+
config.a = "Value of a"
|
22
|
+
config.b = "Value of b"
|
23
|
+
config.c = { also:=>"complex", :values=>nil }
|
28
24
|
end
|
29
25
|
|
30
26
|
##
|
@@ -33,9 +29,6 @@ The latest stable version is published in gemcutter.
|
|
33
29
|
puts ConfigContext.a
|
34
30
|
puts ConfigContext.b
|
35
31
|
puts ConfigContext.c
|
36
|
-
puts ConfigContext.d
|
37
|
-
puts ConfigContext[:d]
|
38
|
-
puts ConfigContext['String key']
|
39
32
|
|
40
33
|
##
|
41
34
|
# Check the presence of a property
|
@@ -43,18 +36,16 @@ The latest stable version is published in gemcutter.
|
|
43
36
|
puts ConfigContext.b if( ConfigContext.b? )
|
44
37
|
|
45
38
|
##
|
46
|
-
# Load your config from a YAML file
|
39
|
+
# Load your config from a YAML file
|
47
40
|
#
|
48
41
|
begin
|
49
|
-
ConfigContext.load( "settings.yml"
|
42
|
+
ConfigContext.load( "settings.yml" )
|
50
43
|
rescue ConfigContext::Error => e
|
51
44
|
fail( e.message )
|
52
45
|
end
|
53
46
|
|
54
47
|
puts ConfigContext.a if( ConfigContext.a? )
|
55
48
|
puts ConfigContext.all.inspect
|
56
|
-
puts ConfigContext.keys
|
57
|
-
|
58
49
|
|
59
50
|
== TODO
|
60
51
|
|
@@ -82,4 +73,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
82
73
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
83
74
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
84
75
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
85
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
76
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/config_context.rb
CHANGED
@@ -4,59 +4,57 @@ require 'yaml'
|
|
4
4
|
module ConfigContext
|
5
5
|
extend self
|
6
6
|
|
7
|
-
@config =
|
7
|
+
@config = Hash.new
|
8
8
|
|
9
9
|
class Error < StandardError; end
|
10
10
|
|
11
|
-
|
11
|
+
private
|
12
|
+
def _add_property( method, *arguments )
|
13
|
+
|
14
|
+
property_key = method.to_s.delete( '=' ).to_sym
|
15
|
+
@config[property_key] = arguments.length == 1 ? arguments[0] : arguments
|
16
|
+
end
|
12
17
|
|
13
|
-
|
18
|
+
def _property?( method )
|
19
|
+
|
20
|
+
property_key = method.to_s.delete( '?' ).to_sym
|
14
21
|
|
15
|
-
|
22
|
+
@config.keys.include?( property_key )
|
23
|
+
end
|
24
|
+
|
25
|
+
def _get_property( method )
|
26
|
+
|
27
|
+
@config[method]
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
public
|
32
|
+
def method_missing( method, *arguments, &block )
|
33
|
+
|
34
|
+
if( method.to_s =~ /(.+)=$/ )
|
35
|
+
_add_property( method, *arguments )
|
16
36
|
elsif( method.to_s =~ /(.+)\?$/ )
|
17
|
-
|
18
|
-
@config.has_key?( method.to_s.delete( '?' ).to_sym )
|
37
|
+
_property?( method )
|
19
38
|
else
|
20
|
-
|
21
|
-
@config[method] if @config.has_key?( method )
|
39
|
+
_get_property( method )
|
22
40
|
end
|
23
41
|
end
|
24
42
|
|
25
|
-
def load( config_file
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
@config.merge!( YAML.load_file( config_file ) ) { |k, ov, nv| ov }
|
43
|
+
def load( config_file )
|
44
|
+
|
45
|
+
YAML.load_file( config_file ).each do |key,value|
|
46
|
+
|
47
|
+
@config[key] = value
|
31
48
|
end
|
32
|
-
|
33
|
-
@config
|
34
49
|
rescue Exception => e
|
35
50
|
raise ConfigContext::Error.new( e.message )
|
36
51
|
end
|
37
52
|
|
38
53
|
def configure()
|
39
|
-
|
40
54
|
yield self
|
41
55
|
end
|
42
56
|
|
43
|
-
def []( key )
|
44
|
-
|
45
|
-
@config[key]
|
46
|
-
end
|
47
|
-
|
48
|
-
def []=( key, value )
|
49
|
-
|
50
|
-
@config[key] = value
|
51
|
-
end
|
52
|
-
|
53
57
|
def all()
|
54
|
-
|
55
58
|
@config
|
56
59
|
end
|
57
|
-
|
58
|
-
def keys
|
59
|
-
|
60
|
-
@config.keys
|
61
|
-
end
|
62
60
|
end
|
data/lib/version.rb
CHANGED
@@ -11,24 +11,17 @@ class TestConfigContext < Test::Unit::TestCase
|
|
11
11
|
|
12
12
|
context "A ConfigContext" do
|
13
13
|
|
14
|
-
TEST_SYMBOL
|
15
|
-
TEST_LIST
|
16
|
-
TEST_HASH
|
17
|
-
|
18
|
-
TEST_OTHERSYMBOL = "othersymbol"
|
19
|
-
|
20
|
-
|
14
|
+
TEST_SYMBOL = 'symbol value'
|
15
|
+
TEST_LIST = [1, 2, 3]
|
16
|
+
TEST_HASH = { :a=>1, :b=>2, :c=>3 }
|
17
|
+
|
21
18
|
setup do
|
22
19
|
|
23
20
|
ConfigContext.configure do |config|
|
24
|
-
|
25
21
|
config.mysymbol = TEST_SYMBOL
|
26
22
|
config.mylist = TEST_LIST
|
27
23
|
config.myhash = TEST_HASH
|
28
24
|
end
|
29
|
-
|
30
|
-
ConfigContext[TEST_STRING] = TEST_STRING
|
31
|
-
ConfigContext[:othersymbol] = TEST_OTHERSYMBOL
|
32
25
|
end
|
33
26
|
|
34
27
|
should "configure properties" do
|
@@ -36,9 +29,6 @@ class TestConfigContext < Test::Unit::TestCase
|
|
36
29
|
assert_equal( ConfigContext.mysymbol, TEST_SYMBOL )
|
37
30
|
assert_equal( ConfigContext.mylist, TEST_LIST )
|
38
31
|
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 )
|
42
32
|
end
|
43
33
|
|
44
34
|
should "check the presence of some properties" do
|
@@ -46,9 +36,6 @@ class TestConfigContext < Test::Unit::TestCase
|
|
46
36
|
assert( ConfigContext.mysymbol? )
|
47
37
|
assert( ConfigContext.mylist? )
|
48
38
|
assert( ConfigContext.myhash? )
|
49
|
-
assert( ConfigContext.othersymbol? )
|
50
|
-
|
51
|
-
assert( !ConfigContext.string? ) # Pay attention to this behaviour!!!
|
52
39
|
|
53
40
|
assert( !ConfigContext.thisdonotexist? )
|
54
41
|
end
|
@@ -56,54 +43,30 @@ class TestConfigContext < Test::Unit::TestCase
|
|
56
43
|
should "retrive all properties" do
|
57
44
|
|
58
45
|
assert_equal( ConfigContext.all, {
|
59
|
-
:mysymbol
|
60
|
-
:mylist
|
61
|
-
:myhash
|
62
|
-
'string' =>TEST_STRING,
|
63
|
-
:othersymbol =>TEST_OTHERSYMBOL
|
46
|
+
:mysymbol =>TEST_SYMBOL,
|
47
|
+
:mylist =>TEST_LIST,
|
48
|
+
:myhash =>TEST_HASH
|
64
49
|
} )
|
65
50
|
end
|
66
51
|
|
67
52
|
should "retrieve all property keys" do
|
68
53
|
|
69
|
-
[ :mysymbol, :mylist, :myhash
|
54
|
+
[ :mysymbol, :mylist, :myhash ].each do |key|
|
70
55
|
|
71
|
-
assert( ConfigContext.keys.include?( key ) )
|
56
|
+
assert( ConfigContext.all.keys.include?( key ) )
|
72
57
|
end
|
73
58
|
end
|
74
59
|
|
75
|
-
should "load a Yaml file
|
60
|
+
should "load a Yaml file" do
|
76
61
|
|
62
|
+
assert_raises( ConfigContext::Error ) { ConfigContext.load( "very_bad_file.yml" ) }
|
77
63
|
ConfigContext.load( File.join( File.dirname( __FILE__ ), %w[ .. fixtures test.yml] ) )
|
78
64
|
assert_equal( ConfigContext.all, {
|
79
65
|
:mysymbol =>TEST_SYMBOL,
|
80
66
|
:mylist =>TEST_LIST,
|
81
|
-
:myhash =>TEST_HASH
|
82
|
-
'string' =>TEST_STRING,
|
83
|
-
:othersymbol =>TEST_OTHERSYMBOL
|
67
|
+
:myhash =>TEST_HASH
|
84
68
|
} )
|
85
69
|
end
|
86
|
-
|
87
|
-
should "load a Yaml file with key collisions" do
|
88
|
-
|
89
|
-
original_value = "The Original value is here"
|
90
|
-
|
91
|
-
ConfigContext.configure { |config| config.mysymbol = original_value }
|
92
|
-
|
93
|
-
ConfigContext.load( File.join( File.dirname( __FILE__ ), %w[ .. fixtures test.yml] ), :allow_collisions=>false )
|
94
|
-
assert_equal( ConfigContext.all, {
|
95
|
-
:mysymbol =>original_value,
|
96
|
-
:mylist =>TEST_LIST,
|
97
|
-
:myhash =>TEST_HASH,
|
98
|
-
'string' =>TEST_STRING,
|
99
|
-
:othersymbol =>TEST_OTHERSYMBOL
|
100
|
-
} )
|
101
|
-
end
|
102
|
-
|
103
|
-
should "load a Yaml file with error" do
|
104
|
-
|
105
|
-
assert_raises( ConfigContext::Error ) { ConfigContext.load( "very bad file.yml" ) }
|
106
|
-
end
|
107
70
|
|
108
71
|
should "update properties" do
|
109
72
|
|
@@ -111,13 +74,15 @@ class TestConfigContext < Test::Unit::TestCase
|
|
111
74
|
ConfigContext.mysymbol = "A"
|
112
75
|
assert_equal( ConfigContext.mysymbol, "A" )
|
113
76
|
|
77
|
+
new_test_list = [4,5,6]
|
114
78
|
assert_equal( ConfigContext.mylist, TEST_LIST )
|
115
|
-
ConfigContext.mylist =
|
116
|
-
assert_equal( ConfigContext.mylist,
|
79
|
+
ConfigContext.mylist = new_test_list
|
80
|
+
assert_equal( ConfigContext.mylist, new_test_list )
|
117
81
|
|
82
|
+
new_hash = { :d=>4, :e=>5, :f=>6 }
|
118
83
|
assert_equal( ConfigContext.myhash, TEST_HASH )
|
119
|
-
ConfigContext.myhash=
|
120
|
-
assert_equal( ConfigContext.myhash,
|
84
|
+
ConfigContext.myhash = new_hash
|
85
|
+
assert_equal( ConfigContext.myhash, new_hash )
|
121
86
|
end
|
122
87
|
end
|
123
88
|
end
|
metadata
CHANGED
@@ -1,8 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: config_context
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 25
|
4
5
|
prerelease:
|
5
|
-
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 3
|
9
|
+
- 5
|
10
|
+
version: 0.3.5
|
6
11
|
platform: ruby
|
7
12
|
authors:
|
8
13
|
- Javier Juarez
|
@@ -10,9 +15,23 @@ autorequire:
|
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
17
|
|
13
|
-
date: 2011-04-
|
14
|
-
|
15
|
-
|
18
|
+
date: 2011-04-29 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
type: :development
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
name: jeweler
|
33
|
+
version_requirements: *id001
|
34
|
+
prerelease: false
|
16
35
|
description: My config DSL
|
17
36
|
email: javier.juarez@gmail.com
|
18
37
|
executables: []
|
@@ -26,6 +45,7 @@ files:
|
|
26
45
|
- lib/version.rb
|
27
46
|
- test/unit/test_config_context.rb
|
28
47
|
- README.rdoc
|
48
|
+
has_rdoc: true
|
29
49
|
homepage: http://github.com/jjuarez/config_context
|
30
50
|
licenses:
|
31
51
|
- MIT
|
@@ -39,17 +59,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
39
59
|
requirements:
|
40
60
|
- - ">="
|
41
61
|
- !ruby/object:Gem::Version
|
62
|
+
hash: 3
|
63
|
+
segments:
|
64
|
+
- 0
|
42
65
|
version: "0"
|
43
66
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
67
|
none: false
|
45
68
|
requirements:
|
46
69
|
- - ">="
|
47
70
|
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
72
|
+
segments:
|
73
|
+
- 0
|
48
74
|
version: "0"
|
49
75
|
requirements: []
|
50
76
|
|
51
77
|
rubyforge_project: http://github.com/jjuarez/config_context
|
52
|
-
rubygems_version: 1.
|
78
|
+
rubygems_version: 1.6.2
|
53
79
|
signing_key:
|
54
80
|
specification_version: 3
|
55
81
|
summary: A Config Context for little applications
|