config_context 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +11 -4
- data/lib/config_context.rb +31 -17
- data/lib/version.rb +2 -2
- data/test/unit/test_config_context.rb +17 -3
- metadata +5 -5
data/README.rdoc
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
= Config Context:
|
2
2
|
|
3
|
-
Your DSL config context...
|
3
|
+
Your minimal and DSL config context...
|
4
|
+
|
4
5
|
|
5
6
|
== Installing
|
6
7
|
|
@@ -8,7 +9,8 @@ The latest stable version is published in gemcutter.
|
|
8
9
|
|
9
10
|
gem install config_context
|
10
11
|
|
11
|
-
|
12
|
+
|
13
|
+
== How to use, some useful examples
|
12
14
|
|
13
15
|
require 'rubygems'
|
14
16
|
require 'config_context'
|
@@ -45,11 +47,16 @@ The latest stable version is published in gemcutter.
|
|
45
47
|
end
|
46
48
|
|
47
49
|
puts ConfigContext.a if( ConfigContext.a? )
|
48
|
-
puts ConfigContext.
|
50
|
+
puts ConfigContext.to_hash.inspect
|
51
|
+
|
52
|
+
|
53
|
+
##
|
54
|
+
#
|
55
|
+
#
|
49
56
|
|
50
57
|
== TODO
|
51
58
|
|
52
|
-
*
|
59
|
+
* ???
|
53
60
|
|
54
61
|
|
55
62
|
== License
|
data/lib/config_context.rb
CHANGED
@@ -4,35 +4,48 @@ 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
|
|
11
|
+
|
11
12
|
private
|
13
|
+
|
14
|
+
|
15
|
+
def _method_to_key( method )
|
16
|
+
method.to_s.delete( '=?!' ).to_sym
|
17
|
+
end
|
18
|
+
|
12
19
|
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
|
20
|
+
@config[_method_to_key( method )] = arguments.length == 1 ? arguments[0] : arguments
|
16
21
|
end
|
17
22
|
|
18
23
|
def _property?( method )
|
19
|
-
|
20
|
-
property_key = method.to_s.delete( '?' ).to_sym
|
21
|
-
@config.keys.include?( property_key )
|
24
|
+
@config.keys.include?( _method_to_key( method ) )
|
22
25
|
end
|
23
26
|
|
24
27
|
def _get_property( method )
|
25
28
|
@config[method]
|
26
29
|
end
|
27
30
|
|
28
|
-
def
|
31
|
+
def configure_from_hash( hash )
|
32
|
+
@config.merge!( hash )
|
33
|
+
end
|
34
|
+
|
35
|
+
def configure_from_yaml( config_file )
|
29
36
|
|
30
|
-
YAML.load_file( config_file ).each
|
31
|
-
|
32
|
-
@config[key] = value
|
33
|
-
end
|
37
|
+
YAML.load_file( config_file ).each { |k, v| @config[k] = v }
|
34
38
|
rescue Exception => e
|
35
39
|
raise ConfigContext::Error.new( e.message )
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.deprecate( old_method, new_method )
|
43
|
+
|
44
|
+
define_method( old_method ) do |*arguments, &block|
|
45
|
+
|
46
|
+
warn( "Warning: #{old_method}() is deprecated. Use #{new_method}() instead." )
|
47
|
+
send( new_method, *arguments, &block )
|
48
|
+
end
|
36
49
|
end
|
37
50
|
|
38
51
|
|
@@ -50,9 +63,11 @@ module ConfigContext
|
|
50
63
|
|
51
64
|
def configure( *arguments, &block )
|
52
65
|
|
53
|
-
|
66
|
+
case arguments[0]
|
54
67
|
when /\.(yml|yaml)/i
|
55
68
|
configure_from_yaml( arguments[0] )
|
69
|
+
when Hash
|
70
|
+
configure_from_hash( arguments[0] )
|
56
71
|
else
|
57
72
|
yield self if block_given?
|
58
73
|
end
|
@@ -61,8 +76,7 @@ module ConfigContext
|
|
61
76
|
def to_hash()
|
62
77
|
@config
|
63
78
|
end
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
alias :all :to_hash
|
79
|
+
|
80
|
+
deprecate :load, :configure
|
81
|
+
deprecate :all, :to_hash
|
68
82
|
end
|
data/lib/version.rb
CHANGED
@@ -18,6 +18,7 @@ class TestConfigContext < Test::Unit::TestCase
|
|
18
18
|
setup do
|
19
19
|
|
20
20
|
ConfigContext.configure do |config|
|
21
|
+
|
21
22
|
config.mysymbol = TEST_SYMBOL
|
22
23
|
config.mylist = TEST_LIST
|
23
24
|
config.myhash = TEST_HASH
|
@@ -57,16 +58,29 @@ class TestConfigContext < Test::Unit::TestCase
|
|
57
58
|
end
|
58
59
|
end
|
59
60
|
|
60
|
-
should "
|
61
|
+
should "configure from a Yaml file" do
|
61
62
|
|
62
|
-
assert_raises( ConfigContext::Error ) { ConfigContext.
|
63
|
+
assert_raises( ConfigContext::Error ) { ConfigContext.configure( "very_bad_file.yml" ) }
|
64
|
+
|
63
65
|
ConfigContext.configure( File.join( File.dirname( __FILE__ ), %w[ .. fixtures test.yml] ) )
|
64
|
-
assert_equal( ConfigContext.
|
66
|
+
assert_equal( ConfigContext.to_hash, {
|
65
67
|
:mysymbol =>TEST_SYMBOL,
|
66
68
|
:mylist =>TEST_LIST,
|
67
69
|
:myhash =>TEST_HASH
|
68
70
|
} )
|
69
71
|
end
|
72
|
+
|
73
|
+
should "configurre from a Hash" do
|
74
|
+
|
75
|
+
config_hash = {
|
76
|
+
:mysymbol =>TEST_SYMBOL,
|
77
|
+
:mylist =>TEST_LIST,
|
78
|
+
:myhash =>TEST_HASH
|
79
|
+
}
|
80
|
+
|
81
|
+
ConfigContext.configure( config_hash )
|
82
|
+
assert_equal( ConfigContext.to_hash, config_hash )
|
83
|
+
end
|
70
84
|
|
71
85
|
should "update properties" do
|
72
86
|
|
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: 11
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 5
|
9
|
+
- 0
|
10
|
+
version: 0.5.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-05-
|
18
|
+
date: 2011-05-20 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|