simple-conf 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +4 -0
- data/Rakefile +5 -0
- data/lib/simple-conf/loader.rb +13 -1
- data/lib/simple-conf/version.rb +1 -1
- data/spec/simple_conf_spec.rb +49 -2
- metadata +2 -2
data/README.md
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
Simple configuration library for the loading yml files from the config folder.
|
4
4
|
|
5
|
+
[![Build Status](https://secure.travis-ci.org/oivoodoo/simple-conf.png?branch=master)](http://travis-ci.org/oivoodoo/simple-conf)
|
6
|
+
|
5
7
|
## Installation
|
6
8
|
|
7
9
|
Add this line to your application's Gemfile:
|
@@ -36,6 +38,8 @@ Create in the config folder configuration.yml file with content like:
|
|
36
38
|
- test2.example.com
|
37
39
|
production:
|
38
40
|
domain: "production.example.com"
|
41
|
+
username: <%= ENV['username'] %>
|
42
|
+
password: <%= ENV['password'] %>
|
39
43
|
```
|
40
44
|
|
41
45
|
Now you can use your file in the project:
|
data/Rakefile
CHANGED
data/lib/simple-conf/loader.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'ostruct'
|
2
2
|
require 'yaml'
|
3
|
+
require 'erb'
|
3
4
|
|
4
5
|
module SimpleConf
|
5
6
|
class Loader
|
@@ -13,6 +14,10 @@ module SimpleConf
|
|
13
14
|
yaml_file.each_pair do |key, value|
|
14
15
|
set(key, value)
|
15
16
|
end
|
17
|
+
|
18
|
+
yaml_file.fetch(Rails.env, {}).each_pair do |key, value|
|
19
|
+
set(key, value)
|
20
|
+
end if rails_environment_defined?
|
16
21
|
end
|
17
22
|
|
18
23
|
def path
|
@@ -23,7 +28,7 @@ module SimpleConf
|
|
23
28
|
|
24
29
|
def yaml_file
|
25
30
|
content = File.open(path).read
|
26
|
-
YAML.load(content)
|
31
|
+
YAML.load(ERB.new(content).result)
|
27
32
|
end
|
28
33
|
|
29
34
|
private
|
@@ -42,6 +47,13 @@ module SimpleConf
|
|
42
47
|
struct = OpenStruct.new(value) rescue value
|
43
48
|
klass.instance_variable_set(:"@#{key}", struct)
|
44
49
|
end
|
50
|
+
|
51
|
+
def rails_environment_defined?
|
52
|
+
rails_klass = Module.const_get('Rails')
|
53
|
+
return rails_klass.is_a?(Module)
|
54
|
+
rescue NameError
|
55
|
+
return false
|
56
|
+
end
|
45
57
|
end
|
46
58
|
|
47
59
|
def self.included(base)
|
data/lib/simple-conf/version.rb
CHANGED
data/spec/simple_conf_spec.rb
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
+
module Rails
|
4
|
+
def self.env
|
5
|
+
"staging"
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
3
9
|
class Configuration
|
4
10
|
include SimpleConf
|
5
11
|
end
|
@@ -27,11 +33,52 @@ describe SimpleConf do
|
|
27
33
|
|
28
34
|
it { Configuration.production.domain.should == "production.example.com" }
|
29
35
|
end
|
30
|
-
end
|
31
36
|
|
32
|
-
describe SimpleConf do
|
33
37
|
context "on include to options class generate properties with only one key and value" do
|
34
38
|
it { Options.domain.should == "example.com" }
|
35
39
|
end
|
36
40
|
end
|
37
41
|
|
42
|
+
describe SimpleConf do
|
43
|
+
context "on include properties depending on rails environment" do
|
44
|
+
it { Configuration.domain.should == "staging.example.com" }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class TestObject ; end
|
49
|
+
|
50
|
+
describe SimpleConf do
|
51
|
+
before do
|
52
|
+
file = double('file', :read => attrib('test').to_yaml)
|
53
|
+
File.stub!(:open).and_return file
|
54
|
+
end
|
55
|
+
|
56
|
+
context "when we have env params in the configuration file" do
|
57
|
+
before { ENV['simple_conf_test_password'] = 'password' }
|
58
|
+
|
59
|
+
after { ENV['simple_conf_test_password'] = nil }
|
60
|
+
|
61
|
+
let(:loader) { SimpleConf::Loader.new(TestObject) }
|
62
|
+
|
63
|
+
context "on run loader" do
|
64
|
+
before { loader.run }
|
65
|
+
|
66
|
+
it "should return username" do
|
67
|
+
TestObject.test.username.should == 'fred'
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should return password fetched from the environment" do
|
71
|
+
TestObject.test.password.should == 'password'
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def attrib(environment)
|
77
|
+
{
|
78
|
+
environment => {
|
79
|
+
"username" => "fred",
|
80
|
+
"password" => "<%= ENV['simple_conf_test_password'] %>"
|
81
|
+
}
|
82
|
+
}
|
83
|
+
end
|
84
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple-conf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-09-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|