config_context 0.3.5 → 0.4.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/lib/config_context.rb +26 -18
- data/lib/version.rb +2 -2
- data/test/unit/test_config_context.rb +3 -3
- metadata +7 -7
data/lib/config_context.rb
CHANGED
@@ -4,7 +4,7 @@ require 'yaml'
|
|
4
4
|
module ConfigContext
|
5
5
|
extend self
|
6
6
|
|
7
|
-
@config =
|
7
|
+
@config = {}
|
8
8
|
|
9
9
|
class Error < StandardError; end
|
10
10
|
|
@@ -18,17 +18,25 @@ module ConfigContext
|
|
18
18
|
def _property?( method )
|
19
19
|
|
20
20
|
property_key = method.to_s.delete( '?' ).to_sym
|
21
|
-
|
22
21
|
@config.keys.include?( property_key )
|
23
22
|
end
|
24
23
|
|
25
24
|
def _get_property( method )
|
26
|
-
|
27
25
|
@config[method]
|
28
26
|
end
|
27
|
+
|
28
|
+
def configure_from_yaml( config_file )
|
29
|
+
|
30
|
+
YAML.load_file( config_file ).each do |key,value|
|
31
|
+
|
32
|
+
@config[key] = value
|
33
|
+
end
|
34
|
+
rescue Exception => e
|
35
|
+
raise ConfigContext::Error.new( e.message )
|
36
|
+
end
|
29
37
|
|
30
38
|
|
31
|
-
public
|
39
|
+
public
|
32
40
|
def method_missing( method, *arguments, &block )
|
33
41
|
|
34
42
|
if( method.to_s =~ /(.+)=$/ )
|
@@ -39,22 +47,22 @@ module ConfigContext
|
|
39
47
|
_get_property( method )
|
40
48
|
end
|
41
49
|
end
|
42
|
-
|
43
|
-
def
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
def configure()
|
54
|
-
yield self
|
50
|
+
|
51
|
+
def configure( *arguments, &block )
|
52
|
+
|
53
|
+
configuration = case arguments[0]
|
54
|
+
when /\.(yml|yaml)/i
|
55
|
+
configure_from_yaml( arguments[0] )
|
56
|
+
else
|
57
|
+
yield self if block_given?
|
58
|
+
end
|
55
59
|
end
|
56
60
|
|
57
|
-
def
|
61
|
+
def to_hash()
|
58
62
|
@config
|
59
63
|
end
|
64
|
+
|
65
|
+
# Backward compat with older versions
|
66
|
+
alias :load :configure
|
67
|
+
alias :all :to_hash
|
60
68
|
end
|
data/lib/version.rb
CHANGED
@@ -42,7 +42,7 @@ class TestConfigContext < Test::Unit::TestCase
|
|
42
42
|
|
43
43
|
should "retrive all properties" do
|
44
44
|
|
45
|
-
assert_equal( ConfigContext.
|
45
|
+
assert_equal( ConfigContext.to_hash, {
|
46
46
|
:mysymbol =>TEST_SYMBOL,
|
47
47
|
:mylist =>TEST_LIST,
|
48
48
|
:myhash =>TEST_HASH
|
@@ -53,14 +53,14 @@ class TestConfigContext < Test::Unit::TestCase
|
|
53
53
|
|
54
54
|
[ :mysymbol, :mylist, :myhash ].each do |key|
|
55
55
|
|
56
|
-
assert( ConfigContext.
|
56
|
+
assert( ConfigContext.to_hash.keys.include?( key ) )
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
60
|
should "load a Yaml file" do
|
61
61
|
|
62
62
|
assert_raises( ConfigContext::Error ) { ConfigContext.load( "very_bad_file.yml" ) }
|
63
|
-
ConfigContext.
|
63
|
+
ConfigContext.configure( File.join( File.dirname( __FILE__ ), %w[ .. fixtures test.yml] ) )
|
64
64
|
assert_equal( ConfigContext.all, {
|
65
65
|
:mysymbol =>TEST_SYMBOL,
|
66
66
|
:mylist =>TEST_LIST,
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: config_context
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 4
|
9
|
+
- 0
|
10
|
+
version: 0.4.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Javier Juarez
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-05-19 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -79,5 +79,5 @@ rubygems_version: 1.6.2
|
|
79
79
|
signing_key:
|
80
80
|
specification_version: 3
|
81
81
|
summary: A Config Context for little applications
|
82
|
-
test_files:
|
83
|
-
|
82
|
+
test_files: []
|
83
|
+
|