config_context 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
- rake
10
- gem install ./pk/config_context-${VERSION}.gem
10
+ gem install config_context
11
+
11
12
 
12
13
  == How to use
13
14
 
14
15
  require 'rubygems'
15
- require 'context_config'
16
-
16
+ require 'config_context'
17
+
17
18
  ##
18
- # Pure config in a block
19
- ContextConfig.configure do |my_config|
19
+ # Puts all your configuration context in a block
20
+ #
21
+ ConfigContext.configure do |config|
20
22
 
21
- my_config.a = "Value of a"
22
- my_comfig.b = "Value of b"
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
- # Config like hash notation in a block
27
- ContextConfig.configure do |other_config|
28
-
29
- other_config[:key] = ["value1", "value2" ]
30
- other_config[:other_key] = { :new=>"values" }
31
- end
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
- # Loading config from YAML file
46
+ # Load your config from a YAML file and overrides some properties...
47
+ #
35
48
  begin
36
- ContextConfig.load( "settings.yml" )
37
- rescue ContextConfigError => cce
38
- fail( cce.message )
49
+ ConfigContext.load( "settings.yml", :allow_collisions=>true )
50
+ rescue ConfigContext::Error => e
51
+ fail( e.message )
39
52
  end
40
-
41
- puts ContextConfig.a
42
- puts ContextConfig[:b]
43
- puts ContextConfig.all.inspect
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
@@ -4,68 +4,43 @@ require 'yaml'
4
4
  module ConfigContext
5
5
  extend self
6
6
 
7
- class ConfigContextError < StandardError; end
7
+ @config = { }
8
8
 
9
- def init
10
-
11
- @config ||= {}
12
- end
13
-
9
+ class Error < StandardError; end
14
10
 
15
11
  def method_missing( method, *arguments, &block )
16
12
 
17
- self.init unless @config
18
-
19
- if( method =~ /(.+)=$/)
13
+ if( method =~ /(.+)=$/ )
20
14
 
21
- config_key = method.to_s.delete( '=$' ).to_sym
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
- return @config[method] if @config.keys.include?( method )
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 configure
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 all
53
-
54
- self.init unless @config
29
+ def[]=( key, value ) @config[key] = value; end
55
30
 
56
- @config
57
- end
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
- self.init unless @config
63
-
64
- yf = YAML.load_file( config_file )
65
- yf.keys.each { |key| @config[key] = yf[key] }
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 ConfigContextError.new( e.message )
68
- else
69
- @config
44
+ raise ConfigContext::Error.new( e.message )
70
45
  end
71
46
  end
@@ -2,8 +2,8 @@ module ConfigContext
2
2
  module Version
3
3
  INFO = {
4
4
  :major =>0,
5
- :minor =>2,
6
- :patch =>1
5
+ :minor =>3,
6
+ :patch =>0
7
7
  }
8
8
 
9
9
  NAME = 'config_context'
@@ -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.element = "element"
19
- config.mylist = [1,2,3]
20
- config.myhash = { :a=>1, :b=>2, :c=>3 }
25
+ config.mysymbol = TEST_SYMBOL
26
+ config.mylist = TEST_LIST
27
+ config.myhash = TEST_HASH
21
28
  end
22
29
 
23
- ConfigContext['string'] = "String"
24
- ConfigContext[:mysymbol] = "I am a pretty symbol"
30
+ ConfigContext[TEST_STRING] = TEST_STRING
31
+ ConfigContext[:othersymbol] = TEST_OTHERSYMBOL
25
32
  end
26
33
 
27
- should "Test configure members" do
34
+ should "configure properties" do
28
35
 
29
- assert_equal( ConfigContext.element, "element" )
30
- assert_equal( ConfigContext.mylist, [1,2,3] )
31
- assert_equal( ConfigContext.myhash, { :a=>1, :b=>2, :c=>3 } )
32
- assert_equal( ConfigContext['string'], "String" )
33
- assert_equal( ConfigContext.mysymbol, "I am a pretty symbol" )
34
- assert_equal( ConfigContext[:mysymbol], "I am a pretty symbol" )
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 "Test all properties" do
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, { :element=>"element", :mylist=>[1,2,3], :myhash=>{ :a=>1, :b=>2, :c=>3 }, :mysymbol=> 'I am a pretty symbol', 'string'=>'String' } )
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, { :element=>'element', :mylist=>[1,2,3], :myhash=>{ :a=>1, :b=>2, :c=>3 }, :mysymbol=>'I am a pretty symbol', 'string'=>'String' } )
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 "Test update property" do
105
+ should "update properties" do
49
106
 
50
- assert_equal( ConfigContext.element, "element" )
51
- ConfigContext.element = "A"
52
- assert_equal( ConfigContext.element, "A" )
107
+ assert_equal( ConfigContext.mysymbol, TEST_SYMBOL )
108
+ ConfigContext.mysymbol = "A"
109
+ assert_equal( ConfigContext.mysymbol, "A" )
53
110
 
54
- assert_equal( ConfigContext.mylist, [1,2,3] )
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, { :a=>1, :b=>2, :c=>3 } )
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.2.1
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-14 00:00:00 +01:00
13
+ date: 2011-03-15 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies: []
16
16