config_context 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,61 +1,56 @@
1
1
  require 'yaml'
2
2
 
3
3
 
4
- module ConfigContext
5
-
6
- class ConfigContextError < StandardError; end
4
+ module ConfigContext
5
+ class ConfigContextError < StandardError
6
+ end
7
7
 
8
8
  class << self
9
9
 
10
+ def init
11
+ @config ||= Hash.new
12
+ end
13
+
10
14
  def method_missing( method, *arguments, &block )
15
+
16
+ init
17
+ if( method =~ /(.+)=$/)
11
18
 
12
- @config ||= {}
13
-
14
- if( method =~/(.+)=$/)
15
-
16
- key = method.to_s.delete( '=$' ).to_sym
19
+ key = method.to_s.delete( '=$' ).to_sym
17
20
  @config[key] = (arguments.length == 1) ? arguments[0] : arguments
18
21
  else
19
-
20
22
  return @config[method] if @config.keys.include?( method )
21
23
  end
22
24
  end
23
-
25
+
24
26
  def configure
25
27
  yield self
26
28
  end
27
29
 
28
30
  def []( key )
29
-
30
- @config ||= {}
31
-
32
- if( key.instance_of?( String ) )
33
- @config[key] ? @config[key] : @config[key.to_sym]
34
- elsif( key.instance_of?( Symbol ) )
35
- @config[key] ? @config[key] : @config[key.to_s]
36
- else
37
- nil
38
- end
31
+ init
32
+ return @config[key] if @config[key]
33
+ nil
39
34
  end
40
-
35
+
41
36
  def[]=( key, value )
42
-
43
- @config ||= {}
44
- @config[key] = value
37
+ init
38
+ @config[key]=value
45
39
  end
46
-
40
+
47
41
  def all
48
-
49
- @config ||= {}
42
+ init
50
43
  @config
51
44
  end
52
-
53
- def load( config_file )
54
45
 
55
- @config ||= {}
46
+ def load( config_file )
47
+ init
48
+ yf = YAML.load_file( config_file )
56
49
 
57
- yaml_config = YAML.load_file( config_file )
58
- yaml_config.keys.each { |key| @config[key] = yaml_config[key] }
50
+ yf.keys.each do |key|
51
+
52
+ @config[key] = yf[key]
53
+ end
59
54
  rescue Exception => e
60
55
  raise ConfigContextError.new( e.message )
61
56
  nil
@@ -1,13 +1,12 @@
1
- module Version
2
- module Info
3
-
4
- MAJOR = 0
5
- MINOR = 1
6
- PATCH = 0
7
-
8
- end
1
+ module ConfigContext
2
+ module Version
3
+ INFO = {
4
+ :major =>0,
5
+ :minor =>2,
6
+ :patch =>0
7
+ }
9
8
 
10
- NAME = 'config_context'
11
- INFO = "#{Info::MAJOR}.#{Info::MINOR}.#{Info::PATCH}"
12
- COMPLETE = "#{NAME}-#{INFO}"
9
+ NAME = 'config_context'
10
+ VERSION = INFO.values.join( '.' )
11
+ end
13
12
  end
@@ -1,68 +1,63 @@
1
1
  $:.unshift( File.join( File.dirname( __FILE__ ), %w[.. .. lib] ) )
2
2
 
3
+
4
+ require 'rubygems'
3
5
  require 'test/unit'
6
+ require 'shoulda'
4
7
  require 'config_context'
5
8
 
6
9
 
7
10
  class TestConfigContext < Test::Unit::TestCase
8
-
9
- def test_case_configure
10
-
11
- ConfigContext.configure do |config|
12
- config.a = "a"
13
- config.b = "b"
14
- config.c = "c"
15
- end
16
-
17
- ConfigContext["d"] = "d"
18
11
 
19
- assert_equal( ConfigContext.a, "a" )
20
- assert_equal( ConfigContext.b, "b" )
21
- assert_equal( ConfigContext.c, "c" )
22
- assert_equal( ConfigContext["d"], "d" )
23
- assert_nil( ConfigContext.d, "d" )
24
- end
12
+ context "A ConfigContext" do
25
13
 
26
- def test_case_all
27
-
28
- ConfigContext.configure do |config|
29
- config.a = "a"
30
- config.b = "b"
31
- config.c = "c"
32
- end
33
-
34
- ConfigContext["d"] = "d"
14
+ setup do
35
15
 
36
- assert_equal( ConfigContext.all, { :a=>'a', :b=>'b', :c=>'c', "d"=>'d' } )
37
- end
16
+ ConfigContext.configure do |config|
38
17
 
39
- def test_case_hash
40
-
41
- ConfigContext.configure do |config|
42
- config[:a] = "a"
43
- config[:b] = "b"
44
- config[:c] = "c"
18
+ config.element = "element"
19
+ config.mylist = [1,2,3]
20
+ config.myhash = { :a=>1, :b=>2, :c=>3 }
21
+ end
22
+
23
+ ConfigContext['string'] = "String"
24
+ ConfigContext[:mysymbol] = "I am a pretty symbol"
45
25
  end
46
26
 
47
- ConfigContext["d"] = "d"
27
+ should "Test configure members" do
48
28
 
49
- assert_equal( ConfigContext.a, "a" )
50
- assert_equal( ConfigContext.b, "b" )
51
- assert_equal( ConfigContext.c, "c" )
52
-
53
- assert_equal( ConfigContext[:a], "a" )
54
- assert_equal( ConfigContext[:b], "b" )
55
- assert_equal( ConfigContext[:c], "c" )
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" )
35
+ end
36
+
37
+ should "Test all properties" do
38
+
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' } )
40
+ end
56
41
 
57
- assert_equal( ConfigContext[:d], "d" )
58
- assert_equal( ConfigContext["d"], "d" )
42
+ should "Test yaml load" do
59
43
 
60
- assert_equal( ConfigContext.all, { :a=>'a', :b=>'b', :c=>'c', "d"=>'d' } )
61
- end
62
-
63
- def test_case_yaml
44
+ 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' } )
46
+ end
64
47
 
65
- ConfigContext.load( File.join( File.dirname( __FILE__ ), %w[ .. fixtures test.yml] ) )
66
- assert_equal( ConfigContext.all, { :a=>'a', :b=>'b', :c=>'c', "d"=>'d' } )
48
+ should "Test update property" do
49
+
50
+ assert_equal( ConfigContext.element, "element" )
51
+ ConfigContext.element = "A"
52
+ assert_equal( ConfigContext.element, "A" )
53
+
54
+ assert_equal( ConfigContext.mylist, [1,2,3] )
55
+ ConfigContext.mylist = [4,5,6]
56
+ assert_equal( ConfigContext.mylist, [4,5,6] )
57
+
58
+ assert_equal( ConfigContext.myhash, { :a=>1, :b=>2, :c=>3 } )
59
+ ConfigContext.myhash= { :d=>4, :e=>5, :f=>6 }
60
+ assert_equal( ConfigContext.myhash, { :d=>4, :e=>5, :f=>6 } )
61
+ end
67
62
  end
68
63
  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.1.0
5
+ version: 0.2.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-02-21 00:00:00 +01:00
13
+ date: 2011-03-04 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies: []
16
16