config_context 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,79 +4,75 @@ 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
12
  private
13
-
14
-
15
- def _method_to_key( method )
16
- method.to_s.delete( '=?!' ).to_sym
13
+ def _add_property(property, *arguments)
14
+ @config[property.to_s.delete("=?").to_sym] = arguments.length == 1 ? arguments[0] : arguments
17
15
  end
18
16
 
19
- def _add_property( method, *arguments )
20
- @config[_method_to_key( method )] = arguments.length == 1 ? arguments[0] : arguments
17
+ def _exist_property?(property)
18
+ @config.keys.include?(property.to_s.delete("=?").to_sym)
21
19
  end
22
20
 
23
- def _property?( method )
24
- @config.keys.include?( _method_to_key( method ) )
25
- end
26
-
27
- def _get_property( method )
28
- @config[method]
29
- end
30
-
31
- def configure_from_hash( hash )
32
- @config.merge!( hash )
21
+ def _get_property(property)
22
+ @config[property]
33
23
  end
34
24
 
35
- def configure_from_yaml( config_file )
25
+ def self.deprecate(old_method, new_method)
36
26
 
37
- YAML.load_file( config_file ).each { |k, v| @config[k] = v }
38
- rescue Exception => e
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|
27
+ define_method(old_method) do |*arguments, &block|
45
28
 
46
- warn( "Warning: #{old_method}() is deprecated. Use #{new_method}() instead." )
47
- send( new_method, *arguments, &block )
29
+ warn("#{old_method}() is deprecated. Use #{new_method}() instead.")
30
+ send(new_method, *arguments, &block)
48
31
  end
49
32
  end
50
33
 
51
34
 
52
- public
53
- def method_missing( method, *arguments, &block )
35
+ public
36
+ def method_missing(method, *arguments, &block)
54
37
 
55
- if( method.to_s =~ /(.+)=$/ )
56
- _add_property( method, *arguments )
57
- elsif( method.to_s =~ /(.+)\?$/ )
58
- _property?( method )
38
+ case method.to_s
39
+ when /(.+)=$/ then _add_property(method, *arguments)
40
+ when /(.+)\?$/ then _exist_property?(method)
59
41
  else
60
- _get_property( method )
42
+ _get_property(method)
61
43
  end
62
44
  end
63
45
 
64
- def configure( *arguments, &block )
65
-
46
+ def configure(*arguments, &block)
47
+
66
48
  case arguments[0]
67
- when /\.(yml|yaml)/i
68
- configure_from_yaml( arguments[0] )
49
+ when /\.(yml|yaml|conf|config)/i
50
+ if(File.exist?(arguments[0]))
51
+ @config.merge!(YAML.load_file(arguments[0]))
52
+ else
53
+ raise ConfigContext::Error.new("The config file: #{arguments[0]} do not exist")
54
+ end
69
55
  when Hash
70
- configure_from_hash( arguments[0] )
56
+ @config.merge!(arguments[0])
71
57
  else
72
58
  yield self if block_given?
73
- end
59
+ end
60
+
61
+ self
62
+ rescue Exception=>e
63
+ raise ConfigContext::Error.new(e.message)
74
64
  end
75
65
 
76
66
  def to_hash()
77
67
  @config
78
68
  end
79
69
 
70
+ ##
71
+ # Backward compability...
72
+ def [](key)
73
+ @config[key]
74
+ end
75
+
80
76
  deprecate :load, :configure
81
77
  deprecate :all, :to_hash
82
78
  end
data/lib/version.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Version
2
2
  INFO = {
3
3
  :major =>0,
4
- :minor =>5,
4
+ :minor =>6,
5
5
  :patch =>0
6
6
  }
7
7
 
@@ -30,6 +30,7 @@ class TestConfigContext < Test::Unit::TestCase
30
30
  assert_equal( ConfigContext.mysymbol, TEST_SYMBOL )
31
31
  assert_equal( ConfigContext.mylist, TEST_LIST )
32
32
  assert_equal( ConfigContext.myhash, TEST_HASH )
33
+ assert_equal( ConfigContext.mysymbol, ConfigContext[:mysymbol])
33
34
  end
34
35
 
35
36
  should "check the presence of some properties" do
@@ -60,22 +61,27 @@ class TestConfigContext < Test::Unit::TestCase
60
61
 
61
62
  should "configure from a Yaml file" do
62
63
 
63
- assert_raises( ConfigContext::Error ) { ConfigContext.configure( "very_bad_file.yml" ) }
64
+ [ "yml", "yaml", "conf", "config" ].each do |extension|
65
+
66
+ assert_raises( ConfigContext::Error ) { ConfigContext.configure( "this_file_do_not_exist.#{extension}" ) }
67
+ end
68
+
69
+ assert_equal( ConfigContext.to_hash, ConfigContext.configure( "total_foo_bar.file" ).to_hash )
64
70
 
65
71
  ConfigContext.configure( File.join( File.dirname( __FILE__ ), %w[ .. fixtures test.yml] ) )
66
72
  assert_equal( ConfigContext.to_hash, {
67
- :mysymbol =>TEST_SYMBOL,
68
- :mylist =>TEST_LIST,
69
- :myhash =>TEST_HASH
73
+ :mysymbol =>TEST_SYMBOL,
74
+ :mylist =>TEST_LIST,
75
+ :myhash =>TEST_HASH
70
76
  } )
71
77
  end
72
78
 
73
79
  should "configurre from a Hash" do
74
80
 
75
81
  config_hash = {
76
- :mysymbol =>TEST_SYMBOL,
77
- :mylist =>TEST_LIST,
78
- :myhash =>TEST_HASH
82
+ :mysymbol =>TEST_SYMBOL,
83
+ :mylist =>TEST_LIST,
84
+ :myhash =>TEST_HASH
79
85
  }
80
86
 
81
87
  ConfigContext.configure( config_hash )
metadata CHANGED
@@ -1,83 +1,61 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: config_context
3
- version: !ruby/object:Gem::Version
4
- hash: 11
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.0
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 5
9
- - 0
10
- version: 0.5.0
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Javier Juarez
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-05-20 00:00:00 +02:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
22
- type: :development
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 3
29
- segments:
30
- - 0
31
- version: "0"
12
+ date: 2011-06-07 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
32
15
  name: jeweler
33
- version_requirements: *id001
16
+ requirement: &2156909680 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
34
23
  prerelease: false
24
+ version_requirements: *2156909680
35
25
  description: My config DSL
36
26
  email: javier.juarez@gmail.com
37
27
  executables: []
38
-
39
28
  extensions: []
40
-
41
- extra_rdoc_files:
29
+ extra_rdoc_files:
42
30
  - README.rdoc
43
- files:
31
+ files:
44
32
  - lib/config_context.rb
45
33
  - lib/version.rb
46
34
  - test/unit/test_config_context.rb
47
35
  - README.rdoc
48
- has_rdoc: true
49
36
  homepage: http://github.com/jjuarez/config_context
50
- licenses:
37
+ licenses:
51
38
  - MIT
52
39
  post_install_message:
53
40
  rdoc_options: []
54
-
55
- require_paths:
41
+ require_paths:
56
42
  - lib
57
- required_ruby_version: !ruby/object:Gem::Requirement
43
+ required_ruby_version: !ruby/object:Gem::Requirement
58
44
  none: false
59
- requirements:
60
- - - ">="
61
- - !ruby/object:Gem::Version
62
- hash: 3
63
- segments:
64
- - 0
65
- version: "0"
66
- required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
50
  none: false
68
- requirements:
69
- - - ">="
70
- - !ruby/object:Gem::Version
71
- hash: 3
72
- segments:
73
- - 0
74
- version: "0"
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
75
55
  requirements: []
76
-
77
56
  rubyforge_project: http://github.com/jjuarez/config_context
78
- rubygems_version: 1.6.2
57
+ rubygems_version: 1.8.5
79
58
  signing_key:
80
59
  specification_version: 3
81
60
  summary: A Config Context for little applications
82
61
  test_files: []
83
-