config_context 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +71 -0
- data/lib/config_context.rb +52 -0
- data/lib/version.rb +13 -0
- data/test/unit/tc_config_context.rb +60 -0
- metadata +59 -0
data/README.rdoc
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
= Remote Executor:
|
2
|
+
|
3
|
+
Your DSL config context...
|
4
|
+
|
5
|
+
== Installing
|
6
|
+
|
7
|
+
The latest stable version is published in gemcutter.
|
8
|
+
|
9
|
+
gem source --add http://gemcutter.org
|
10
|
+
gem install context_config
|
11
|
+
|
12
|
+
|
13
|
+
== How to use
|
14
|
+
|
15
|
+
require 'rubygems'
|
16
|
+
require 'context_config'
|
17
|
+
|
18
|
+
##
|
19
|
+
# Pure config in a block
|
20
|
+
ContextConfig.configure do |my_config|
|
21
|
+
|
22
|
+
my_config.a = "Value of a"
|
23
|
+
my_comfig.b = "Value of b"
|
24
|
+
end
|
25
|
+
|
26
|
+
##
|
27
|
+
# Config like hash notation in a block
|
28
|
+
ContextConfig.configure do |other_config|
|
29
|
+
|
30
|
+
other_config[:key] = ["value1", "value2" ]
|
31
|
+
other_config[:other_key] = { :new=>"values" }
|
32
|
+
end
|
33
|
+
|
34
|
+
##
|
35
|
+
# Loading config from YAML file
|
36
|
+
begin
|
37
|
+
ContextConfig.load( "settings.yml" )
|
38
|
+
rescue ContextConfigError => cce
|
39
|
+
fail( cce.message )
|
40
|
+
end
|
41
|
+
|
42
|
+
puts ContextConfig.a
|
43
|
+
puts ContextConfig[:b]
|
44
|
+
puts ContextConfig.all.inspect
|
45
|
+
|
46
|
+
== TODO
|
47
|
+
|
48
|
+
* Document all this stuff in english... I promise
|
49
|
+
|
50
|
+
== License
|
51
|
+
|
52
|
+
Copyright (c) 2010-2030 Javier Juarez Martinez
|
53
|
+
|
54
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
55
|
+
a copy of this software and associated documentation files (the
|
56
|
+
"Software"), to deal in the Software without restriction, including
|
57
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
58
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
59
|
+
permit persons to whom the Software is furnished to do so, subject to
|
60
|
+
the following conditions:
|
61
|
+
|
62
|
+
The above copyright notice and this permission notice shall be
|
63
|
+
included in all copies or substantial portions of the Software.
|
64
|
+
|
65
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
66
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
67
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
68
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
69
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
70
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
71
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
|
4
|
+
module ConfigContext
|
5
|
+
|
6
|
+
class ConfigContextError < StandardError; end
|
7
|
+
|
8
|
+
class << self
|
9
|
+
|
10
|
+
def method_missing( method, *arguments, &block )
|
11
|
+
|
12
|
+
@config ||= {}
|
13
|
+
|
14
|
+
if( method =~/(.+)=$/)
|
15
|
+
|
16
|
+
key = method.to_s.delete( '=$' ).to_sym
|
17
|
+
@config[key] = (arguments.length == 1) ? arguments[0] : arguments
|
18
|
+
else
|
19
|
+
|
20
|
+
return @config[method] if @config.keys.include?( method )
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def configure
|
25
|
+
yield self
|
26
|
+
end
|
27
|
+
|
28
|
+
def []( key )
|
29
|
+
@config[key.to_sym] if @config
|
30
|
+
end
|
31
|
+
|
32
|
+
def[]=( key, value )
|
33
|
+
@config[key.to_sym] = value if @config
|
34
|
+
end
|
35
|
+
|
36
|
+
def all
|
37
|
+
@config
|
38
|
+
end
|
39
|
+
|
40
|
+
def load( config_file )
|
41
|
+
|
42
|
+
@config ||= {}
|
43
|
+
|
44
|
+
yaml_config = YAML.load_file( config_file )
|
45
|
+
yaml_config.keys.each { |key| @config[key] = yaml_config[key] }
|
46
|
+
rescue Exception => e
|
47
|
+
raise ConfigContextError.new( e.message )
|
48
|
+
else
|
49
|
+
@config
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/version.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
$:.unshift( File.join( File.dirname( __FILE__ ), %w[.. .. lib] ) )
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'config_context'
|
5
|
+
|
6
|
+
|
7
|
+
class TestLibraryFileName < Test::Unit::TestCase
|
8
|
+
|
9
|
+
TEST_HASH = { :a=>'a', :b=>'b', :c=>'c' }
|
10
|
+
TEST_FILE = File.join( File.dirname( __FILE__ ), %w[ .. fixtures test.yml] )
|
11
|
+
|
12
|
+
def test_case_configure
|
13
|
+
|
14
|
+
ConfigContext.configure do |config|
|
15
|
+
config.a = "a"
|
16
|
+
config.b = "b"
|
17
|
+
config.c = "c"
|
18
|
+
end
|
19
|
+
|
20
|
+
assert_equal( ConfigContext.a, "a" )
|
21
|
+
assert_equal( ConfigContext.b, "b" )
|
22
|
+
assert_equal( ConfigContext.c, "c" )
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_case_all
|
26
|
+
|
27
|
+
ConfigContext.configure do |config|
|
28
|
+
config.a = "a"
|
29
|
+
config.b = "b"
|
30
|
+
config.c = "c"
|
31
|
+
end
|
32
|
+
|
33
|
+
assert_equal( ConfigContext.all, TEST_HASH )
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_case_hash
|
37
|
+
|
38
|
+
ConfigContext.configure do |config|
|
39
|
+
config[:a] = "a"
|
40
|
+
config[:b] = "b"
|
41
|
+
config[:c] = "c"
|
42
|
+
end
|
43
|
+
|
44
|
+
assert_equal( ConfigContext.a, "a" )
|
45
|
+
assert_equal( ConfigContext.b, "b" )
|
46
|
+
assert_equal( ConfigContext.c, "c" )
|
47
|
+
|
48
|
+
assert_equal( ConfigContext[:a], "a" )
|
49
|
+
assert_equal( ConfigContext[:b], "b" )
|
50
|
+
assert_equal( ConfigContext[:c], "c" )
|
51
|
+
|
52
|
+
assert_equal( ConfigContext.all, TEST_HASH )
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_case_yaml
|
56
|
+
|
57
|
+
ConfigContext.load( TEST_FILE )
|
58
|
+
assert_equal( ConfigContext.all, TEST_HASH )
|
59
|
+
end
|
60
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: config_context
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Javier Juarez
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-02-14 00:00:00 +01:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: My config DSL
|
18
|
+
email: javier.juarez@gmail.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files:
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- lib/config_context.rb
|
27
|
+
- lib/version.rb
|
28
|
+
- test/unit/tc_config_context.rb
|
29
|
+
- README.rdoc
|
30
|
+
has_rdoc: true
|
31
|
+
homepage: http://github.com/jjuarez/config_context
|
32
|
+
licenses:
|
33
|
+
- MIT
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
requirements: []
|
52
|
+
|
53
|
+
rubyforge_project: http://github.com/jjuarez/config_context
|
54
|
+
rubygems_version: 1.5.2
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: A Config Context for little applications
|
58
|
+
test_files:
|
59
|
+
- test/unit/tc_config_context.rb
|