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.
- data/README.rdoc +40 -75
- data/app_config.gemspec +4 -9
- data/lib/app_config.rb +32 -46
- data/rails/init.rb +7 -9
- data/test/app_config_test.rb +114 -46
- metadata +4 -9
- data/README +0 -1
- data/test/app_config.yml +0 -2
- data/test/development.yml +0 -5
- data/test/empty1.yml +0 -0
- data/test/empty2.yml +0 -0
data/README.rdoc
CHANGED
|
@@ -1,75 +1,40 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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/
|
data/app_config.gemspec
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
Gem::Specification.new do |s|
|
|
2
2
|
s.name = 'app_config'
|
|
3
|
-
s.version = '1.
|
|
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 = ['
|
|
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/
|
|
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
|
data/lib/app_config.rb
CHANGED
|
@@ -1,47 +1,33 @@
|
|
|
1
|
-
require '
|
|
2
|
-
require 'yaml'
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
data/rails/init.rb
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
require 'app_config'
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
data/test/app_config_test.rb
CHANGED
|
@@ -1,46 +1,114 @@
|
|
|
1
|
-
require '
|
|
2
|
-
require '
|
|
3
|
-
require
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
def
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
+
version: "1.5"
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
-
|
|
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:
|
|
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
|
data/test/app_config.yml
DELETED
data/test/development.yml
DELETED
data/test/empty1.yml
DELETED
|
File without changes
|
data/test/empty2.yml
DELETED
|
File without changes
|