scotch-app_config 1.4 → 1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,75 +1,40 @@
1
- Conf## Summary
2
-
3
- Application level configuration.
4
-
5
- ## Author
6
-
7
- Christopher J. Bottaro
8
-
9
- ### Accessing the Conf object
10
-
11
- After installing this plugin, the Conf object will be global available. Entries are accessed via object member notation:
12
-
13
- Conf.my_config_entry
14
-
15
- Nested entries are supported:
16
-
17
- Conf.my_section.some_entry
18
-
19
- ### Common config file
20
-
21
- Config entries defined in
22
-
23
- RAILS_ROOT/config/app_config.yml
24
-
25
- will be available to all environments.
26
-
27
- ### Environment specific config files
28
-
29
- You can have environment specific config files. Environment specific config entries take precedence over common config entries.
30
- Example development environment config f=ile:
31
-
32
- RAILS_ROOT/config/environments/development.yml
33
-
34
- Example production environment config file:
35
-
36
- RAILS_ROOT/config/environments/production.yml
37
-
38
- ### Embedded Ruby (ERB)
39
-
40
- Embedded Ruby is allowed in the configuration files. See examples below.
41
-
42
- ### Examples
43
-
44
- Consider the two following config files.
45
-
46
- RAILS\_ROOT/config/app\_config.yml:
47
-
48
- size: 1
49
- server: google.com
50
-
51
- RAILS_ROOT/config/environments/development.yml:
52
-
53
- size: 2
54
- computed: <%= 1 + 2 + 3 %>
55
- section:
56
- size: 3
57
- servers: [ {name: yahoo.com}, {name: amazon.com} ]
58
-
59
- Notice that the environment specific config entries overwrite the common entries.
60
-
61
- Conf.size -> 2
62
- Conf.server -> google.com
63
-
64
- Notice the embedded Ruby.
65
-
66
- Conf.computed -> 6
67
-
68
- Notice that object member notation is maintained even in nested entries.
69
-
70
- Conf.section.size -> 3
71
-
72
- Notice array notation and object member notation is maintained.
73
-
74
- Conf.section.servers[0].name -> yahoo.com
75
- Conf.section.servers[1].name -> amazon.com
1
+ = AppConfig
2
+
3
+ It allows to have a file (config/config.yml) with application configuration parameters.
4
+ It should have a "common" section with all parameters along with default values and can also
5
+ contain a section for each of the rails environments (development, test, production, or
6
+ your custom one). The values from the current environment section will override the values in the
7
+ "common" section.
8
+
9
+ If some developer needs his own specific values for his working copy, he can simply create
10
+ a config/config.local.yml file and override any value there, again having a "common" section
11
+ and a section for each environment.
12
+
13
+ Nothing is mandatory (files, sections) -- you just have what you really need. The files are parsed with ERB,
14
+ so they can contain some Ruby
15
+
16
+ == Example usage
17
+
18
+ common:
19
+ admin_email: admin@domain.com
20
+ xml_rpc_url: http://domain.com:8000/
21
+ media_path: <%= RAILS_ROOT %>/tmp/media
22
+
23
+ development:
24
+ xml_rpc_url: http://localhost:8000/
25
+
26
+ test:
27
+ xml_rpc_url: http://localhost:8008/
28
+
29
+ Then, in the application you can use the config parameters like this:
30
+
31
+ #{Conf.site_url}
32
+
33
+ So it means that you've got a Config object which holds all the configuration parameters defined.
34
+ It doesn't allow to change the values in the application code, BTW.
35
+
36
+ == Authors and credits
37
+
38
+ Authors:: Eugene Bolshakov,
39
+ Site URL:: http://www.taknado.com
40
+ Based on:: http://kpumuk.info/ruby-on-rails/flexible-application-configuration-in-ruby-on-rails/lang-pref/en/
@@ -1,18 +1,17 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'app_config'
3
- s.version = '1.4'
3
+ s.version = '1.5'
4
4
  s.date = '2008-09-03'
5
5
 
6
6
  s.summary = "Application level configuration"
7
7
  s.description = "Allow application wide configuration settings via YML files"
8
8
 
9
- s.authors = ['RailsJedi', 'Christopher J. Bottaro']
10
- s.email = 'railsjedi@gmail.com'
9
+ s.authors = ['Eugene Bolshakov']
11
10
  s.homepage = 'http://github.com/scotch/app_config'
12
11
 
13
12
  s.has_rdoc = true
14
13
  s.rdoc_options = ["--main", "README"]
15
- s.extra_rdoc_files = ["README"]
14
+ s.extra_rdoc_files = ["README.rdoc"]
16
15
 
17
16
  s.add_dependency 'rails', ['>= 2.1']
18
17
 
@@ -23,10 +22,6 @@ Gem::Specification.new do |s|
23
22
  "lib/app_config.rb",
24
23
  "rails/init.rb"]
25
24
 
26
- s.test_files = ["test/app_config.yml",
27
- "test/app_config_test.rb",
28
- "test/development.yml",
29
- "test/empty1.yml",
30
- "test/empty2.yml"]
25
+ s.test_files = ["test/app_config_test.rb"]
31
26
 
32
27
  end
@@ -1,47 +1,33 @@
1
- require 'ostruct'
2
- require 'yaml'
3
- require 'erb'
4
-
5
- # == Summary
6
- # This is API documentation, NOT documentation on how to use this plugin. For that, see the README.
7
- class ApplicationConfig
8
-
9
- cattr_accessor :conf_paths
10
-
11
- # Create a config object (OpenStruct) from a yaml file. If a second yaml file is given, then the sections of that file will overwrite the sections
12
- # if the first file if they exist in the first file.
13
- def self.load_files(*conf_load_paths)
14
- self.conf_paths = []
15
- self.conf_paths += conf_load_paths
16
- self.conf_paths.uniq!
17
-
18
- reload
19
- end
20
-
21
- def self.reload
22
- conf = {}
23
- conf_paths.each do |path|
24
- new_conf = YAML.load(ERB.new(IO.read(path)).result) if path and File.exists?(path)
25
- conf.merge!(new_conf) if new_conf
26
- end
27
- return convert(conf)
28
- end
29
-
30
- # Recursively converts Hashes to OpenStructs (including Hashes inside Arrays)
31
- def self.convert(h) #:nodoc:
32
- s = OpenStruct.new
33
- h.each do |k, v|
34
- s.new_ostruct_member(k)
35
- if v.instance_of?(Hash)
36
- s.send( (k+'=').to_sym, convert(v))
37
- elsif v.instance_of?(Array)
38
- converted_array = v.collect { |e| e.instance_of?(Hash) ? convert(e) : e }
39
- s.send( (k+'=').to_sym, converted_array)
40
- else
41
- s.send( (k+'=').to_sym, v)
42
- end
43
- end
44
- s
45
- end
46
-
1
+ require 'erb'
2
+ require 'yaml'
3
+
4
+ class AppConfig
5
+
6
+ def initialize(file = nil)
7
+ @sections = {}
8
+ @params = {}
9
+ use_file!(file) if file
10
+ end
11
+
12
+ def use_file!(file)
13
+ begin
14
+ hash = YAML::load(ERB.new(IO.read(file)).result)
15
+ @sections.merge!(hash) {|key, old_val, new_val| (old_val || new_val).merge new_val }
16
+ @params.merge!(@sections['common'])
17
+ rescue; end
18
+ end
19
+
20
+ def use_section!(section)
21
+ @params.merge!(@sections[section.to_s]) if @sections.key?(section.to_s)
22
+ end
23
+
24
+ def method_missing(param)
25
+ param = param.to_s
26
+ if @params.key?(param)
27
+ @params[param]
28
+ else
29
+ raise "Invalid AppConfig Parameter " + param
30
+ end
31
+ end
32
+
47
33
  end
@@ -1,9 +1,7 @@
1
- require 'app_config'
2
-
3
- ::Conf = ApplicationConfig.load_files(RAILS_ROOT+"/config/app_config.yml",
4
- RAILS_ROOT+"/config/environments/#{RAILS_ENV}.yml")
5
-
6
- def Conf.reload!
7
- Conf.marshal_load(ApplicationConfig.reload.marshal_dump)
8
- return Conf
9
- end
1
+ require 'app_config'
2
+
3
+ c = AppConfig.new
4
+ c.use_file!("#{RAILS_ROOT}/config/config.yml")
5
+ c.use_file!("#{RAILS_ROOT}/config/config.local.yml")
6
+ c.use_section!(RAILS_ENV)
7
+ ::Conf = c
@@ -1,46 +1,114 @@
1
- require 'rubygems'
2
- require 'test/unit'
3
- require 'active_support'
4
- require 'lib/app_config'
5
-
6
- class AppConfigTest < Test::Unit::TestCase
7
-
8
- def test_missing_files
9
- config = ApplicationConfig.load_files('not_here1', 'not_here2')
10
- assert_equal OpenStruct.new, config
11
- end
12
-
13
- def test_empty_files
14
- config = ApplicationConfig.load_files('test/empty1.yml', 'test/empty2.yml')
15
- assert_equal OpenStruct.new, config
16
- end
17
-
18
- def test_common
19
- config = ApplicationConfig.load_files('test/app_config.yml')
20
- assert_equal 1, config.size
21
- assert_equal 'google.com', config.server
22
- end
23
-
24
- def test_environment_override
25
- config = ApplicationConfig.load_files('test/app_config.yml', 'test/development.yml')
26
- assert_equal 2, config.size
27
- assert_equal 'google.com', config.server
28
- end
29
-
30
- def test_nested
31
- config = ApplicationConfig.load_files('test/development.yml')
32
- assert_equal 3, config.section.size
33
- end
34
-
35
- def test_array
36
- config = ApplicationConfig.load_files('test/development.yml')
37
- assert_equal 'yahoo.com', config.section.servers[0].name
38
- assert_equal 'amazon.com', config.section.servers[1].name
39
- end
40
-
41
- def test_erb
42
- config = ApplicationConfig.load_files('test/development.yml')
43
- assert_equal 6, config.computed
44
- end
45
-
46
- end
1
+ require 'test/unit'
2
+ require 'fileutils'
3
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/app_config.rb")
4
+
5
+ class AppConfigTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ @dir = File.dirname(__FILE__) + "/"
9
+ @files = []
10
+ end
11
+
12
+ def test_should_read_from_yml
13
+ test_config 'common' => {'a' => '1', 'b' => '2'} do |file, hash|
14
+ config = AppConfig.new(file)
15
+ assert_equal_with_hash config, hash['common']
16
+ end
17
+ end
18
+
19
+ def test_should_ignore_non_existent_file
20
+ config = AppConfig.new('nonexsistentfile')
21
+ assert_not_nil config
22
+ end
23
+
24
+ def test_should_igonre_non_existent_section
25
+ test_config 'common' => {'a' => '1', 'b' => '2' } do |file, hash|
26
+ config = AppConfig.new(file)
27
+ config.use_section!('nonexistentsection')
28
+ assert_equal config.b, '2'
29
+ end
30
+ end
31
+
32
+ def test_should_parse_yaml_with_erb
33
+ test_config 'common' => {'a' => '1', 'b' => '<%= 2 + 2 %>' } do |file, hash|
34
+ config = AppConfig.new(file)
35
+ assert_equal config.b, 4
36
+ end
37
+ end
38
+
39
+ def test_should_override_params_with_given_section
40
+ test_config 'common' => {'a' => '1', 'b' => '<%= 2 + 2 %>'},
41
+ 'special' => {'a' => 1, 'b' => 5 } do |file, hash|
42
+ config = AppConfig.new(file)
43
+ config.use_section!('special')
44
+ assert_equal_with_hash config, hash['special']
45
+ end
46
+ test_config 'common' => {'a' => '1', 'b' => '<%= 2 + 2 %>'},
47
+ 'special' => {'b' => 5 } do |file, hash|
48
+ config = AppConfig.new(file)
49
+ config.use_section!('special')
50
+ assert_equal_with_hash config, {'a' => '1', 'b' => 5}
51
+ end
52
+ end
53
+
54
+ def test_should_ovveride_params_with_another_file
55
+ test_config({'common' => {'a' => '1', 'b' => '<%= 2 + 2 %>'},
56
+ 'special' => {'b' => 5 }}, 'config') do |file1, hash1|
57
+ test_config({'common' => {'a' => '2'},
58
+ 'special' => {'b' => 8 }}, 'config.local') do |file2, hash2|
59
+ config = AppConfig.new(file1)
60
+ assert_equal_with_hash config, {'a' => '1', 'b' => 4}
61
+ config.use_file!(file2)
62
+ assert_equal_with_hash config, {'a' => '2', 'b' => 4}
63
+ end
64
+ end
65
+ end
66
+
67
+ def test_should_ovveride_params_with_another_file_and_use_proper_section
68
+ test_config({'common' => {'a' => '1', 'b' => '<%= 2 + 2 %>', 'c' => 2},
69
+ 'special' => {'b' => 5, 'd' => 6 },
70
+ 'extra' => {'f' => 4, 'a' => 8}}, 'config') do |file1, hash1|
71
+ test_config({'common' => {'a' => '2'},
72
+ 'special' => {'b' => 8 }}, 'config.local') do |file2, hash2|
73
+ config = AppConfig.new
74
+ config.use_file!(file1)
75
+ config.use_file!(file2)
76
+ config.use_section!('special')
77
+ assert_equal_with_hash config, {'a' => '2', 'b' => 8, 'c' => 2, 'd' => 6}
78
+ end
79
+ end
80
+ end
81
+
82
+ def teardown
83
+ begin
84
+ FileUtils.rm @files
85
+ rescue
86
+ end
87
+ end
88
+
89
+ protected
90
+
91
+ def file_path(file)
92
+ @dir + file + '.yml'
93
+ end
94
+
95
+ def create_config_file(file, hash)
96
+ file = @dir + file + '.yml'
97
+ @files << file
98
+ File.open(file, 'w') do |f|
99
+ f.write hash.to_yaml
100
+ end
101
+ end
102
+
103
+ def assert_equal_with_hash(config, hash)
104
+ hash.each do |key, value|
105
+ assert_equal config.send(key), value
106
+ end
107
+ end
108
+
109
+ def test_config(hash, file = 'config', &blk)
110
+ create_config_file file, hash
111
+ blk.call(file_path(file), hash)
112
+ end
113
+
114
+ end
metadata CHANGED
@@ -1,11 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scotch-app_config
3
3
  version: !ruby/object:Gem::Version
4
- version: "1.4"
4
+ version: "1.5"
5
5
  platform: ruby
6
6
  authors:
7
- - RailsJedi
8
- - Christopher J. Bottaro
7
+ - Eugene Bolshakov
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
@@ -23,13 +22,13 @@ dependencies:
23
22
  version: "2.1"
24
23
  version:
25
24
  description: Allow application wide configuration settings via YML files
26
- email: railsjedi@gmail.com
25
+ email:
27
26
  executables: []
28
27
 
29
28
  extensions: []
30
29
 
31
30
  extra_rdoc_files:
32
- - README
31
+ - README.rdoc
33
32
  files:
34
33
  - README
35
34
  - README.rdoc
@@ -65,8 +64,4 @@ signing_key:
65
64
  specification_version: 2
66
65
  summary: Application level configuration
67
66
  test_files:
68
- - test/app_config.yml
69
67
  - test/app_config_test.rb
70
- - test/development.yml
71
- - test/empty1.yml
72
- - test/empty2.yml
data/README DELETED
@@ -1 +0,0 @@
1
- README.rdoc
@@ -1,2 +0,0 @@
1
- size: 1
2
- server: google.com
@@ -1,5 +0,0 @@
1
- size: 2
2
- computed: <%= 1 + 2 + 3 %>
3
- section:
4
- size: 3
5
- servers: [ {name: yahoo.com}, {name: amazon.com} ]
File without changes
File without changes