jcnetdev-app_config 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1 @@
1
+ README.rdoc
data/README.rdoc ADDED
@@ -0,0 +1,56 @@
1
+ == Summary
2
+ Application level configuration.
3
+
4
+ == Author
5
+ Christopher J. Bottaro
6
+
7
+ === Accessing the AppConfig object
8
+ After installing this plugin, the AppConfig object will be global available. Entries are accessed via object member notation:
9
+ AppConfig.my_config_entry
10
+ Nested entries are supported:
11
+ AppConfig.my_section.some_entry
12
+
13
+ === Common config file
14
+ Config entries defined in
15
+ RAILS_ROOT/config/app_config.yml
16
+ will be available to all environments.
17
+
18
+ === Environment specific config files
19
+ You can have environment specific config files. Environment specific config entries take precedence over common config entries.
20
+
21
+ Example development environment config file:
22
+ RAILS_ROOT/config/environments/development.yml
23
+
24
+ Example production environment config file:
25
+ RAILS_ROOT/config/environments/production.yml
26
+
27
+ === Embedded Ruby (ERB)
28
+ Embedded Ruby is allowed in the configuration files. See examples below.
29
+
30
+ === Examples
31
+ Consider the two following config files.
32
+
33
+ RAILS_ROOT/config/app_config.yml:
34
+ size: 1
35
+ server: google.com
36
+
37
+ RAILS_ROOT/config/environments/development.yml:
38
+ size: 2
39
+ computed: <%= 1 + 2 + 3 %>
40
+ section:
41
+ size: 3
42
+ servers: [ {name: yahoo.com}, {name: amazon.com} ]
43
+
44
+ Notice that the environment specific config entries overwrite the common entries.
45
+ AppConfig.size -> 2
46
+ AppConfig.server -> google.com
47
+
48
+ Notice the embedded Ruby.
49
+ AppConfig.computed -> 6
50
+
51
+ Notice that object member notation is maintained even in nested entries.
52
+ AppConfig.section.size -> 3
53
+
54
+ Notice array notation and object member notation is maintained.
55
+ AppConfig.section.servers[0].name -> yahoo.com
56
+ AppConfig.section.servers[1].name -> amazon.com
@@ -0,0 +1,32 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'app_config'
3
+ s.version = '1.0'
4
+ s.date = '2008-07-04'
5
+
6
+ s.summary = "Application level configuration"
7
+ s.description = "Allow application wide configuration settings via YML files"
8
+
9
+ s.authors = ['RailsJedi', 'Christopher J. Bottaro']
10
+ s.email = 'railsjedi@gmail.com'
11
+ s.homepage = 'http://github.com/jcnetdev/app_config'
12
+
13
+ s.has_rdoc = true
14
+ s.rdoc_options = ["--main", "README"]
15
+ s.extra_rdoc_files = ["README"]
16
+
17
+ s.add_dependency 'rails', ['>= 2.1']
18
+
19
+ s.files = ["README",
20
+ "README.rdoc",
21
+ "app_config.gemspec",
22
+ "init.rb",
23
+ "lib/app_config.rb",
24
+ "rails/init.rb"]
25
+
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"]
31
+
32
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/rails/init"
data/lib/app_config.rb ADDED
@@ -0,0 +1,41 @@
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
+ module ApplicationConfig
8
+
9
+ # 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
10
+ # if the first file if they exist in the first file.
11
+ def self.load_files(conf_path_1, conf_path_2 = nil)
12
+
13
+ conf1 = YAML.load(ERB.new(IO.read(conf_path_1)).result) if conf_path_1 and File.exists?(conf_path_1)
14
+ conf1 = {} if !conf1 or conf1.empty?
15
+
16
+ conf2 = YAML.load(ERB.new(IO.read(conf_path_2)).result) if conf_path_2 and File.exists?(conf_path_2)
17
+ conf2 = {} if !conf2 or conf2.empty?
18
+
19
+ conf = conf1.merge(conf2)
20
+ (!conf or conf.empty?) ? OpenStruct.new : convert(conf)
21
+
22
+ end
23
+
24
+ # Recursively converts Hashes to OpenStructs (including Hashes inside Arrays)
25
+ def self.convert(h) #:nodoc:
26
+ s = OpenStruct.new
27
+ h.each do |k, v|
28
+ s.new_ostruct_member(k)
29
+ if v.instance_of?(Hash)
30
+ s.send( (k+'=').to_sym, convert(v))
31
+ elsif v.instance_of?(Array)
32
+ converted_array = v.collect { |e| e.instance_of?(Hash) ? convert(e) : e }
33
+ s.send( (k+'=').to_sym, converted_array)
34
+ else
35
+ s.send( (k+'=').to_sym, v)
36
+ end
37
+ end
38
+ s
39
+ end
40
+
41
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'app_config'
2
+
3
+ ::AppConfig = ApplicationConfig.load_files(RAILS_ROOT+"/config/app_config.yml",
4
+ RAILS_ROOT+"/config/environments/#{RAILS_ENV}.yml")
@@ -0,0 +1,2 @@
1
+ size: 1
2
+ server: google.com
@@ -0,0 +1,44 @@
1
+ require 'test/unit'
2
+ require 'app_config'
3
+
4
+ class AppConfigTest < Test::Unit::TestCase
5
+
6
+ def test_missing_files
7
+ config = ApplicationConfig.load_files('not_here1', 'not_here2')
8
+ assert_equal OpenStruct.new, config
9
+ end
10
+
11
+ def test_empty_files
12
+ config = ApplicationConfig.load_files('test/empty1.yml', 'test/empty2.yml')
13
+ assert_equal OpenStruct.new, config
14
+ end
15
+
16
+ def test_common
17
+ config = ApplicationConfig.load_files('test/app_config.yml')
18
+ assert_equal 1, config.size
19
+ assert_equal 'google.com', config.server
20
+ end
21
+
22
+ def test_environment_override
23
+ config = ApplicationConfig.load_files('test/app_config.yml', 'test/development.yml')
24
+ assert_equal 2, config.size
25
+ assert_equal 'google.com', config.server
26
+ end
27
+
28
+ def test_nested
29
+ config = ApplicationConfig.load_files('test/development.yml')
30
+ assert_equal 3, config.section.size
31
+ end
32
+
33
+ def test_array
34
+ config = ApplicationConfig.load_files('test/development.yml')
35
+ assert_equal 'yahoo.com', config.section.servers[0].name
36
+ assert_equal 'amazon.com', config.section.servers[1].name
37
+ end
38
+
39
+ def test_erb
40
+ config = ApplicationConfig.load_files('test/development.yml')
41
+ assert_equal 6, config.computed
42
+ end
43
+
44
+ end
@@ -0,0 +1,5 @@
1
+ size: 2
2
+ computed: <%= 1 + 2 + 3 %>
3
+ section:
4
+ size: 3
5
+ servers: [ {name: yahoo.com}, {name: amazon.com} ]
data/test/empty1.yml ADDED
File without changes
data/test/empty2.yml ADDED
File without changes
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jcnetdev-app_config
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.0"
5
+ platform: ruby
6
+ authors:
7
+ - RailsJedi
8
+ - Christopher J. Bottaro
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2008-07-04 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rails
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "2.1"
24
+ version:
25
+ description: Allow application wide configuration settings via YML files
26
+ email: railsjedi@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README
33
+ files:
34
+ - README
35
+ - README.rdoc
36
+ - app_config.gemspec
37
+ - init.rb
38
+ - lib/app_config.rb
39
+ - rails/init.rb
40
+ has_rdoc: true
41
+ homepage: http://github.com/jcnetdev/app_config
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --main
45
+ - README
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.2.0
64
+ signing_key:
65
+ specification_version: 2
66
+ summary: Application level configuration
67
+ test_files:
68
+ - test/app_config.yml
69
+ - test/app_config_test.rb
70
+ - test/development.yml
71
+ - test/empty1.yml
72
+ - test/empty2.yml