application_config 0.0.2
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/.gitignore +1 -0
- data/LICENSE +21 -0
- data/README +56 -0
- data/application_config.gemspec +18 -0
- data/lib/application_config.rb +8 -0
- data/lib/application_config/base.rb +34 -0
- data/lib/application_config/data_structures/always_null_node.rb +34 -0
- data/lib/application_config/data_structures/nested_hash.rb +22 -0
- data/lib/application_config/data_structures/value_node.rb +21 -0
- data/lib/application_config/railtie.rb +20 -0
- data/lib/application_config_test_ext.rb +38 -0
- data/tasks/app_config_tasks.rake +4 -0
- data/test/app_config_test.rb +8 -0
- data/test/test_helper.rb +3 -0
- metadata +72 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
.project
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2009, Asynchrony Solutions, Inc.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or otherwise distribute copies of the Software
|
8
|
+
to others without charge, and to permit persons to whom the Software is
|
9
|
+
furnished to do so, subject to the following conditions:
|
10
|
+
|
11
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
12
|
+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
13
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
14
|
+
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
15
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
16
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
17
|
+
IN THE SOFTWARE.
|
18
|
+
|
19
|
+
The above copyright notice, permission notice and warranty notice shall
|
20
|
+
be included in all copies or substantial portions of the Software.
|
21
|
+
|
data/README
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
ApplicationConfig
|
2
|
+
=================
|
3
|
+
|
4
|
+
Basically there are 2 levels of configuration:
|
5
|
+
|
6
|
+
1) application wide which is configured in config/application.yml
|
7
|
+
2) environment specific which is configured in config/environments/application-#{RAILS_ENV}.yml
|
8
|
+
|
9
|
+
The environment specific configs override the applicaiton wide config
|
10
|
+
|
11
|
+
|
12
|
+
Example
|
13
|
+
=======
|
14
|
+
|
15
|
+
script/install plugin git://github.com/asynchrony/application_config.git
|
16
|
+
|
17
|
+
Then if for example in your config/application.yml you had:
|
18
|
+
|
19
|
+
mail_host: example.com
|
20
|
+
|
21
|
+
And in your config/environments/application-production.yml you had:
|
22
|
+
|
23
|
+
mail_host: production.server.com
|
24
|
+
|
25
|
+
you could in your code do:
|
26
|
+
|
27
|
+
ApplicationConfig::Base['mail_host'].to_s or
|
28
|
+
ApplicationConfig::Base.mail_host.to_s
|
29
|
+
|
30
|
+
At the moment you really need the to_s on the end.
|
31
|
+
|
32
|
+
Also, if you want to do a nil check you need to put the applicaiton config on the LHS:
|
33
|
+
|
34
|
+
if ApplicationConfig::Base.mail_host == nil
|
35
|
+
|
36
|
+
Because of the way it's working.
|
37
|
+
|
38
|
+
For testing, first:
|
39
|
+
|
40
|
+
require 'application_config_test_ext'
|
41
|
+
|
42
|
+
in your test_helper
|
43
|
+
|
44
|
+
And then before any test you want to alter the config for:
|
45
|
+
|
46
|
+
ApplicationConfig::Base.add(
|
47
|
+
<<EOF
|
48
|
+
mail_host: test_mail_host
|
49
|
+
EOF
|
50
|
+
)
|
51
|
+
|
52
|
+
Which will change the config for that test and then rollback to what it was before the test.
|
53
|
+
This allows you to test behavior off of the singleton config object.
|
54
|
+
|
55
|
+
|
56
|
+
Copyright (c) 2009 [name of plugin creator], released under the MIT license
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "application_config"
|
6
|
+
s.version = '0.0.2'
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["kenny ortmann", "mike gaffney", "nick bimpasis"]
|
9
|
+
s.email = ['kenny.ortmann@gmail.com']
|
10
|
+
s.homepage = "https://github.com/asynchrony/application_config"
|
11
|
+
s.summary = %q{a gem that you can use for application specific settings}
|
12
|
+
s.description = %q{a gem that you can use for application specific settings}
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
require 'application_config/base'
|
2
|
+
require 'application_config/data_structures/always_null_node'
|
3
|
+
require 'application_config/data_structures/nested_hash'
|
4
|
+
require 'application_config/data_structures/value_node'
|
5
|
+
|
6
|
+
if defined?(Rails)
|
7
|
+
require 'application_config/railtie'
|
8
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module ApplicationConfig
|
2
|
+
class Base
|
3
|
+
@@application_config = nil
|
4
|
+
|
5
|
+
def [](key)
|
6
|
+
return @config[key] if @config
|
7
|
+
nil
|
8
|
+
end
|
9
|
+
|
10
|
+
def add(config_text)
|
11
|
+
if @config
|
12
|
+
@config.merge!(YAML::load(config_text))
|
13
|
+
else
|
14
|
+
@config = ApplicationConfig::DataStructures::NestedHash.new(YAML::load(config_text))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.singleton(application_config)
|
19
|
+
@@application_config ||= application_config
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.[](key)
|
23
|
+
@@application_config[key]
|
24
|
+
end
|
25
|
+
|
26
|
+
def method_missing(method_name)
|
27
|
+
self[method_name.to_s]
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.method_missing(method_name)
|
31
|
+
self[method_name.to_s]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module ApplicationConfig
|
2
|
+
module DataStructures
|
3
|
+
class AlwaysNullNode
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
|
7
|
+
end
|
8
|
+
|
9
|
+
def nil?
|
10
|
+
return true
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_s
|
14
|
+
""
|
15
|
+
end
|
16
|
+
|
17
|
+
def ==(other)
|
18
|
+
other == nil
|
19
|
+
end
|
20
|
+
|
21
|
+
def eql?(other)
|
22
|
+
other.eql?(nil)
|
23
|
+
end
|
24
|
+
|
25
|
+
def [](key)
|
26
|
+
ApplicationConfig::DataStructures::AlwaysNullNode.new
|
27
|
+
end
|
28
|
+
|
29
|
+
def method_missing(method)
|
30
|
+
ApplicationConfig::DataStructures::AlwaysNullNode.new
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module ApplicationConfig
|
2
|
+
|
3
|
+
module DataStructures
|
4
|
+
class NestedHash < Hash
|
5
|
+
|
6
|
+
def initialize(hash = {})
|
7
|
+
self.update(hash)
|
8
|
+
end
|
9
|
+
|
10
|
+
def [](key)
|
11
|
+
value = fetch(key, nil)
|
12
|
+
return ApplicationConfig::DataStructures::AlwaysNullNode.new unless value
|
13
|
+
return ApplicationConfig::DataStructures::ValueNode.new(value) unless value.kind_of?(Hash)
|
14
|
+
return ApplicationConfig::DataStructures::NestedHash.new(value)
|
15
|
+
end
|
16
|
+
|
17
|
+
def method_missing(method_name)
|
18
|
+
self[method_name.to_s]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
module ApplicationConfig
|
3
|
+
module DataStructures
|
4
|
+
class ValueNode
|
5
|
+
extend Forwardable
|
6
|
+
def_delegators :@value, :eql?, :==, :to_s, :to_str, :to_i
|
7
|
+
|
8
|
+
def initialize(value)
|
9
|
+
@value = value
|
10
|
+
end
|
11
|
+
|
12
|
+
def [](key)
|
13
|
+
ApplicationConfig::DataStructures::AlwaysNullNode.new
|
14
|
+
end
|
15
|
+
|
16
|
+
def method_missing(method_name)
|
17
|
+
ApplicationConfig::DataStructures::AlwaysNullNode.new
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Rails
|
2
|
+
module ApplicationConfig
|
3
|
+
class Railtie < Rails::Railtie
|
4
|
+
initializer "setup config" do
|
5
|
+
begin
|
6
|
+
config = ::ApplicationConfig::Base.new
|
7
|
+
[Rails.root.join("config", "application.yml"), Rails.root.join("config", "environments", "application-#{Rails.env.to_s}.yml")].each do |config_file|
|
8
|
+
next unless config_file.file?
|
9
|
+
read = IO.read(config_file)
|
10
|
+
erb_result = ERB.new(read).result
|
11
|
+
config.add(erb_result)
|
12
|
+
end
|
13
|
+
::ApplicationConfig::Base.singleton(config)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module ApplicationConfig
|
2
|
+
class Base
|
3
|
+
@@orig = nil
|
4
|
+
|
5
|
+
def self.reset_config_after_test
|
6
|
+
if @@orig
|
7
|
+
@@application_config = @@orig.dup
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.add(config_text_yml)
|
12
|
+
unless @@orig
|
13
|
+
@@orig = @@application_config.dup
|
14
|
+
end
|
15
|
+
@@application_config.add(config_text_yml)
|
16
|
+
end
|
17
|
+
|
18
|
+
def dup
|
19
|
+
new_config = ApplicationConfig::Base.new
|
20
|
+
new_config.config = @config.dup
|
21
|
+
new_config
|
22
|
+
end
|
23
|
+
|
24
|
+
protected
|
25
|
+
|
26
|
+
def config=(config)
|
27
|
+
@config = config
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class ActiveSupport::TestCase
|
33
|
+
teardown :reset_application_config
|
34
|
+
|
35
|
+
def reset_application_config
|
36
|
+
ApplicationConfig::Base.reset_config_after_test
|
37
|
+
end
|
38
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: application_config
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.2
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- kenny ortmann
|
9
|
+
- mike gaffney
|
10
|
+
- nick bimpasis
|
11
|
+
autorequire:
|
12
|
+
bindir: bin
|
13
|
+
cert_chain: []
|
14
|
+
|
15
|
+
date: 2011-02-10 00:00:00 -06:00
|
16
|
+
default_executable:
|
17
|
+
dependencies: []
|
18
|
+
|
19
|
+
description: a gem that you can use for application specific settings
|
20
|
+
email:
|
21
|
+
- kenny.ortmann@gmail.com
|
22
|
+
executables: []
|
23
|
+
|
24
|
+
extensions: []
|
25
|
+
|
26
|
+
extra_rdoc_files: []
|
27
|
+
|
28
|
+
files:
|
29
|
+
- .gitignore
|
30
|
+
- LICENSE
|
31
|
+
- README
|
32
|
+
- application_config.gemspec
|
33
|
+
- lib/application_config.rb
|
34
|
+
- lib/application_config/base.rb
|
35
|
+
- lib/application_config/data_structures/always_null_node.rb
|
36
|
+
- lib/application_config/data_structures/nested_hash.rb
|
37
|
+
- lib/application_config/data_structures/value_node.rb
|
38
|
+
- lib/application_config/railtie.rb
|
39
|
+
- lib/application_config_test_ext.rb
|
40
|
+
- tasks/app_config_tasks.rake
|
41
|
+
- test/app_config_test.rb
|
42
|
+
- test/test_helper.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: https://github.com/asynchrony/application_config
|
45
|
+
licenses: []
|
46
|
+
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.5.0
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: a gem that you can use for application specific settings
|
71
|
+
test_files: []
|
72
|
+
|