scotch-app_config 1.3

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 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.3'
4
+ s.date = '2008-09-06'
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/scotch/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,47 @@
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
+
47
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'app_config'
2
+
3
+ ::AppConfig = ApplicationConfig.load_files(RAILS_ROOT+"/config/app_config.yml",
4
+ RAILS_ROOT+"/config/environments/#{RAILS_ENV}.yml")
5
+
6
+ def AppConfig.reload!
7
+ AppConfig.marshal_load(ApplicationConfig.reload.marshal_dump)
8
+ return AppConfig
9
+ end
@@ -0,0 +1,2 @@
1
+ size: 1
2
+ server: google.com
@@ -0,0 +1,46 @@
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
@@ -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: scotch-app_config
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.3"
5
+ platform: ruby
6
+ authors:
7
+ - RailsJedi
8
+ - Christopher J. Bottaro
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2008-09-06 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/scotch/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