easy_config 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +31 -0
- data/README.md +32 -0
- data/Rakefile +11 -0
- data/easy_config.gemspec +24 -0
- data/lib/easy_config/config_file.rb +26 -0
- data/lib/easy_config/configuration.rb +11 -0
- data/lib/easy_config/env.rb +15 -0
- data/lib/easy_config/path_resolver.rb +15 -0
- data/lib/easy_config/version.rb +3 -0
- data/lib/easy_config.rb +11 -0
- data/spec/easy_config/config_file_spec.rb +32 -0
- data/spec/easy_config/configuration_spec.rb +8 -0
- data/spec/easy_config/env_spec.rb +36 -0
- data/spec/easy_config/path_resolver_spec.rb +36 -0
- data/spec/easy_config_spec.rb +25 -0
- data/spec/fixtures/facebook.yml +3 -0
- data/spec/fixtures/github.yml +1 -0
- data/spec/fixtures/redis.yml +7 -0
- data/spec/spec_helper.rb +3 -0
- metadata +150 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pkg/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
easy_config (0.0.1)
|
5
|
+
methodize (~> 0.2.1)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.1.3)
|
11
|
+
fakefs (0.4.0)
|
12
|
+
methodize (0.2.1)
|
13
|
+
rake (0.9.2.2)
|
14
|
+
rspec (2.9.0)
|
15
|
+
rspec-core (~> 2.9.0)
|
16
|
+
rspec-expectations (~> 2.9.0)
|
17
|
+
rspec-mocks (~> 2.9.0)
|
18
|
+
rspec-core (2.9.0)
|
19
|
+
rspec-expectations (2.9.0)
|
20
|
+
diff-lcs (~> 1.1.3)
|
21
|
+
rspec-mocks (2.9.0)
|
22
|
+
|
23
|
+
PLATFORMS
|
24
|
+
ruby
|
25
|
+
|
26
|
+
DEPENDENCIES
|
27
|
+
bundler (>= 1.0.0)
|
28
|
+
easy_config!
|
29
|
+
fakefs
|
30
|
+
rake
|
31
|
+
rspec (~> 2.9.0)
|
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# Easy config
|
2
|
+
Rails and Rack applications configuration made easy.
|
3
|
+
|
4
|
+
# Installation
|
5
|
+
|
6
|
+
`gem install easy-config`
|
7
|
+
|
8
|
+
or in your Gemfile
|
9
|
+
|
10
|
+
`gem "easy-config"`
|
11
|
+
|
12
|
+
# How it works
|
13
|
+
|
14
|
+
EasyConfig allows you to access your config files easily. All you have to do is
|
15
|
+
to put your files in config dir and be a valid YAML file (with .yml as extension).
|
16
|
+
|
17
|
+
If you desire you can split your configurations by environment. Easy config treats it
|
18
|
+
automatically.
|
19
|
+
|
20
|
+
## Example
|
21
|
+
|
22
|
+
We can access database.yml file as:
|
23
|
+
|
24
|
+
`EasyConfig.database.adapter`
|
25
|
+
|
26
|
+
And if you have your our custom config file named my_customs.yml:
|
27
|
+
|
28
|
+
`EasyConfig.my_customs.custom_var`
|
29
|
+
|
30
|
+
# TODO
|
31
|
+
|
32
|
+
Support ENV['DATABASE_URL'] to EasyConfig.database.url
|
data/Rakefile
ADDED
data/easy_config.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.expand_path("../lib/easy_config/version", __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'easy_config'
|
5
|
+
s.version = EasyConfig::VERSION
|
6
|
+
s.date = '2012-03-17'
|
7
|
+
s.summary = "Easy config, Rails and Rack application's configuration made easy."
|
8
|
+
s.description = s.summary
|
9
|
+
s.authors = ["Felipe Oliveira"]
|
10
|
+
s.email = 'felipecvo@gmail.com'
|
11
|
+
s.homepage = 'http://rubygems.org/gems/easy_config'
|
12
|
+
|
13
|
+
s.required_rubygems_version = ">= 1.3.6"
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {spec}/*`.split("\n")
|
17
|
+
s.require_path = 'lib'
|
18
|
+
|
19
|
+
s.add_dependency "methodize", "~> 0.2.1"
|
20
|
+
|
21
|
+
s.add_development_dependency "bundler", ">= 1.0.0"
|
22
|
+
s.add_development_dependency "rspec", "~> 2.9.0"
|
23
|
+
s.add_development_dependency "rake"
|
24
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class EasyConfig::ConfigFile
|
2
|
+
attr_reader :name
|
3
|
+
|
4
|
+
def initialize(path)
|
5
|
+
@path = path
|
6
|
+
@name = File.basename(path, '.*').to_sym
|
7
|
+
end
|
8
|
+
|
9
|
+
def configuration
|
10
|
+
@configuration ||= create_configuration
|
11
|
+
end
|
12
|
+
|
13
|
+
def create_configuration
|
14
|
+
yaml = YAML::load_file(@path)
|
15
|
+
|
16
|
+
yaml = yaml[EasyConfig::Env.current] if EasyConfig::Env.has_environment?(yaml)
|
17
|
+
|
18
|
+
EasyConfig::Configuration.new(yaml)
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.all
|
22
|
+
@all ||= Dir.glob(EasyConfig::PathResolver.config_path).map do |path|
|
23
|
+
EasyConfig::ConfigFile.new path
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module EasyConfig::PathResolver
|
2
|
+
def self.config_path=(value)
|
3
|
+
@config_path = value
|
4
|
+
end
|
5
|
+
|
6
|
+
def self.config_path
|
7
|
+
if defined?(@config_path) and @config_path
|
8
|
+
File.join(@config_path, '*.yml')
|
9
|
+
elsif defined?(Rails)
|
10
|
+
File.join(Rails.root, 'config', '*.yml')
|
11
|
+
elsif defined?(Sinatra)
|
12
|
+
File.join(Sinatra::Application.root, 'config', '*.yml')
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/easy_config.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
module EasyConfig
|
2
|
+
def self.method_missing(name)
|
3
|
+
file = nil
|
4
|
+
file.configuration if file = EasyConfig::ConfigFile.all.find { |f| f.name == name }
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
require 'easy_config/config_file'
|
9
|
+
require 'easy_config/configuration'
|
10
|
+
require 'easy_config/env'
|
11
|
+
require 'easy_config/path_resolver'
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe EasyConfig::ConfigFile do
|
4
|
+
before { EasyConfig::PathResolver.config_path = './spec/fixtures' }
|
5
|
+
after { EasyConfig::PathResolver.config_path = nil }
|
6
|
+
|
7
|
+
context "all files" do
|
8
|
+
subject { EasyConfig::ConfigFile.all }
|
9
|
+
it { should_not be_nil }
|
10
|
+
its(:size) { should eq 3 }
|
11
|
+
end
|
12
|
+
|
13
|
+
context "specific file" do
|
14
|
+
subject { EasyConfig::ConfigFile.all.first }
|
15
|
+
it { should_not be_nil }
|
16
|
+
its(:name) { should eq :facebook }
|
17
|
+
its(:configuration) { should_not be_nil }
|
18
|
+
its(:configuration) { should be_a EasyConfig::Configuration }
|
19
|
+
its('configuration.app_id') { should eq 1234 }
|
20
|
+
its('configuration.permissions.roles') { should eq 'master' }
|
21
|
+
end
|
22
|
+
|
23
|
+
context "per environment file" do
|
24
|
+
subject { EasyConfig::ConfigFile.all.find { |c| c.name == :redis } }
|
25
|
+
it { should_not be_nil }
|
26
|
+
its(:name) { should eq :redis }
|
27
|
+
its(:configuration) { should_not be_nil }
|
28
|
+
its(:configuration) { should be_a EasyConfig::Configuration }
|
29
|
+
its('configuration.host') { should eq 'localhost' }
|
30
|
+
its('configuration.port') { should eq 4567 }
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe EasyConfig::Env do
|
4
|
+
context :current do
|
5
|
+
context "Rails.env" do
|
6
|
+
before { module ::Rails; def self.env; "production"; end; end }
|
7
|
+
after { Object.send(:remove_const, :Rails) }
|
8
|
+
subject { EasyConfig::Env.current }
|
9
|
+
it { should eq "production" }
|
10
|
+
end
|
11
|
+
|
12
|
+
context "Rack env" do
|
13
|
+
before { ENV['RACK_ENV'] = "test" }
|
14
|
+
after { ENV['RACK_ENV'] = nil }
|
15
|
+
subject { EasyConfig::Env.current }
|
16
|
+
it { should eq "test" }
|
17
|
+
end
|
18
|
+
|
19
|
+
context "default environment" do
|
20
|
+
subject { EasyConfig::Env.current }
|
21
|
+
it { should eq "development" }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context :has_environment? do
|
26
|
+
context "with environments" do
|
27
|
+
subject { EasyConfig::Env.has_environment?({ 'development' => { 'key' => 'value' } }) }
|
28
|
+
it { should be_true }
|
29
|
+
end
|
30
|
+
|
31
|
+
context "without environments" do
|
32
|
+
subject { EasyConfig::Env.has_environment?({ 'key' => 'value' }) }
|
33
|
+
it { should be_false }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe EasyConfig::PathResolver do
|
4
|
+
context "#config_path" do
|
5
|
+
context "resolve Rails config dir" do
|
6
|
+
before { module ::Rails; def self.root; "./"; end; end }
|
7
|
+
after { Object.send(:remove_const, :Rails) }
|
8
|
+
subject { EasyConfig::PathResolver.config_path }
|
9
|
+
it { should eq "./config/*.yml" }
|
10
|
+
end
|
11
|
+
|
12
|
+
context "resolve Sinatra config dir" do
|
13
|
+
before { module ::Sinatra; module Application; def self.root; "/sinatra/"; end; end; end }
|
14
|
+
after { Object.send(:remove_const, :Sinatra) }
|
15
|
+
subject { EasyConfig::PathResolver.config_path }
|
16
|
+
it { should eq "/sinatra/config/*.yml" }
|
17
|
+
end
|
18
|
+
|
19
|
+
context "allow custom config path" do
|
20
|
+
before { EasyConfig::PathResolver.config_path = './my_custom_path/' }
|
21
|
+
subject { EasyConfig::PathResolver.config_path }
|
22
|
+
it { should eq "./my_custom_path/*.yml" }
|
23
|
+
end
|
24
|
+
|
25
|
+
context "allow reset custom config path" do
|
26
|
+
before { EasyConfig::PathResolver.config_path = './my_custom_path/' }
|
27
|
+
subject {
|
28
|
+
EasyConfig::PathResolver.config_path = nil
|
29
|
+
module ::Rails; def self.root; "./"; end; end
|
30
|
+
EasyConfig::PathResolver.config_path
|
31
|
+
}
|
32
|
+
after { Object.send(:remove_const, :Rails) }
|
33
|
+
it { should eq "./config/*.yml" }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe EasyConfig do
|
4
|
+
context "mising methods" do
|
5
|
+
it 'should treat it' do
|
6
|
+
lambda { EasyConfig.facebook }.should_not raise_error
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
context "access per env config file" do
|
11
|
+
subject { EasyConfig.redis }
|
12
|
+
its(:host) { should eq 'localhost' }
|
13
|
+
its(:port) { should eq 4567 }
|
14
|
+
end
|
15
|
+
|
16
|
+
context "access simle config file" do
|
17
|
+
subject { EasyConfig.github }
|
18
|
+
its(:user) { should eq 'felipecvo' }
|
19
|
+
end
|
20
|
+
|
21
|
+
context "inexistent files" do
|
22
|
+
subject { EasyConfig.no_exist }
|
23
|
+
it { should be_nil }
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
user: 'felipecvo'
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easy_config
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Felipe Oliveira
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-03-17 00:00:00 -03:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: methodize
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 21
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
- 2
|
33
|
+
- 1
|
34
|
+
version: 0.2.1
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: bundler
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 23
|
46
|
+
segments:
|
47
|
+
- 1
|
48
|
+
- 0
|
49
|
+
- 0
|
50
|
+
version: 1.0.0
|
51
|
+
type: :development
|
52
|
+
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: rspec
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 43
|
62
|
+
segments:
|
63
|
+
- 2
|
64
|
+
- 9
|
65
|
+
- 0
|
66
|
+
version: 2.9.0
|
67
|
+
type: :development
|
68
|
+
version_requirements: *id003
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
prerelease: false
|
72
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 3
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
type: :development
|
82
|
+
version_requirements: *id004
|
83
|
+
description: Easy config, Rails and Rack application's configuration made easy.
|
84
|
+
email: felipecvo@gmail.com
|
85
|
+
executables: []
|
86
|
+
|
87
|
+
extensions: []
|
88
|
+
|
89
|
+
extra_rdoc_files: []
|
90
|
+
|
91
|
+
files:
|
92
|
+
- .gitignore
|
93
|
+
- Gemfile
|
94
|
+
- Gemfile.lock
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- easy_config.gemspec
|
98
|
+
- lib/easy_config.rb
|
99
|
+
- lib/easy_config/config_file.rb
|
100
|
+
- lib/easy_config/configuration.rb
|
101
|
+
- lib/easy_config/env.rb
|
102
|
+
- lib/easy_config/path_resolver.rb
|
103
|
+
- lib/easy_config/version.rb
|
104
|
+
- spec/easy_config/config_file_spec.rb
|
105
|
+
- spec/easy_config/configuration_spec.rb
|
106
|
+
- spec/easy_config/env_spec.rb
|
107
|
+
- spec/easy_config/path_resolver_spec.rb
|
108
|
+
- spec/easy_config_spec.rb
|
109
|
+
- spec/fixtures/facebook.yml
|
110
|
+
- spec/fixtures/github.yml
|
111
|
+
- spec/fixtures/redis.yml
|
112
|
+
- spec/spec_helper.rb
|
113
|
+
has_rdoc: true
|
114
|
+
homepage: http://rubygems.org/gems/easy_config
|
115
|
+
licenses: []
|
116
|
+
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options: []
|
119
|
+
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
none: false
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
hash: 3
|
128
|
+
segments:
|
129
|
+
- 0
|
130
|
+
version: "0"
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
none: false
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
hash: 23
|
137
|
+
segments:
|
138
|
+
- 1
|
139
|
+
- 3
|
140
|
+
- 6
|
141
|
+
version: 1.3.6
|
142
|
+
requirements: []
|
143
|
+
|
144
|
+
rubyforge_project:
|
145
|
+
rubygems_version: 1.4.2
|
146
|
+
signing_key:
|
147
|
+
specification_version: 3
|
148
|
+
summary: Easy config, Rails and Rack application's configuration made easy.
|
149
|
+
test_files: []
|
150
|
+
|