dsc 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +11 -0
- data/README.md +15 -1
- data/Rakefile +9 -0
- data/dsc.gemspec +1 -1
- data/lib/dsc.rb +7 -3
- data/lib/dsc/version.rb +1 -1
- data/spec/dsc_spec.rb +34 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 881de7b8a80bcf2a8890713e1483230cceed1d18
|
4
|
+
data.tar.gz: f71dc27b103396a7b05e5cca6b39f15143b5383d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e2ecf4b9812855f3270dc58cfc86349295a882fdcd62276a598ea9caf3d7206bbfcdc1908d541aac524a5fb9f3a3d5c85614f38261697933d770396a5a2e1baf
|
7
|
+
data.tar.gz: a9d5bce57bfcf40c962fdcacb28d8bafd95165af66eb8fc07a88514c19a0eaa4be2b7d5d86bec615fc889663d1c20d379c7b899ae7e1fc45683f3228f602ad6d
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/dsc.svg)](http://badge.fury.io/rb/dsc)
|
2
|
+
[![Travis CI](https://api.travis-ci.org/wimdu/dsc.svg?branch=master)](https://travis-ci.org/wimdu/dsc)
|
3
|
+
[![Code Climate](https://codeclimate.com/github/wimdu/dsc/badges/gpa.svg)](https://codeclimate.com/github/wimdu/dsc)
|
4
|
+
|
1
5
|
# Dead simple configuration
|
2
6
|
|
3
7
|
Read configuration from a YAML/JSON file and expose the keys as methods.
|
@@ -20,7 +24,7 @@ Or install it yourself as:
|
|
20
24
|
|
21
25
|
## Usage
|
22
26
|
|
23
|
-
Create a config file:
|
27
|
+
Create a config file (you can use ERB):
|
24
28
|
|
25
29
|
```yaml
|
26
30
|
development:
|
@@ -30,6 +34,7 @@ production:
|
|
30
34
|
username: hugo
|
31
35
|
password: secret
|
32
36
|
pool: 42
|
37
|
+
url: <%= ENV["DATABASE_URL"] %>
|
33
38
|
```
|
34
39
|
|
35
40
|
Open the file and use it:
|
@@ -39,10 +44,19 @@ require 'dsc'
|
|
39
44
|
|
40
45
|
database_config = Dsc.load_file('config/database.yml', env: 'production')
|
41
46
|
database_config.username # => 'hugo'
|
47
|
+
database_config.url # => 'adapter://hostname:4567/database'
|
42
48
|
```
|
43
49
|
|
44
50
|
That's it!
|
45
51
|
|
52
|
+
### Optional attributes
|
53
|
+
|
54
|
+
If you want to be able to not specify some of the configuration options:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
config = Dsc.load_file('config/config.yml', env: 'production', optional: [:pool, :password])
|
58
|
+
```
|
59
|
+
|
46
60
|
## Contributing
|
47
61
|
|
48
62
|
1. Fork it
|
data/Rakefile
CHANGED
data/dsc.gemspec
CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.required_ruby_version = '>= 2.0'
|
21
21
|
|
22
22
|
spec.add_dependency "hashie", "~> 3.0"
|
23
|
-
spec.add_development_dependency "bundler", "~> 1.
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
24
24
|
spec.add_development_dependency "rake", "~> 10.0"
|
25
25
|
spec.add_development_dependency "rspec", "~> 3.1"
|
26
26
|
end
|
data/lib/dsc.rb
CHANGED
@@ -1,13 +1,17 @@
|
|
1
|
-
require "dsc/version"
|
2
1
|
require "hashie/mash"
|
2
|
+
require "erb"
|
3
3
|
require "yaml"
|
4
|
+
|
5
|
+
require "dsc/version"
|
4
6
|
require "dsc/mash"
|
5
7
|
|
6
8
|
module Dsc
|
7
9
|
UndefinedAttribute = Class.new(NoMethodError)
|
8
10
|
|
9
|
-
def self.load_file(file_path, env: "development")
|
10
|
-
|
11
|
+
def self.load_file(file_path, env: "development", optional: [])
|
12
|
+
optional_hash = optional.map { |key| [key, nil] }.to_h
|
13
|
+
config_yaml = ERB.new(File.read(file_path)).result
|
14
|
+
config_hash = optional_hash.merge(YAML::load(config_yaml)[env])
|
11
15
|
|
12
16
|
Dsc::Mash.new(config_hash)
|
13
17
|
end
|
data/lib/dsc/version.rb
CHANGED
data/spec/dsc_spec.rb
CHANGED
@@ -9,6 +9,7 @@ RSpec.describe Dsc do
|
|
9
9
|
production:
|
10
10
|
host: 'http://www.wimdu.com'
|
11
11
|
awesome: true
|
12
|
+
some_inline_erb: <%= ENV['works_too'] %>
|
12
13
|
development:
|
13
14
|
host: 'http://example.com'
|
14
15
|
stable: false
|
@@ -18,12 +19,15 @@ development:
|
|
18
19
|
works: 'aswell'
|
19
20
|
EOF
|
20
21
|
end
|
22
|
+
|
23
|
+
ENV['works_too'] = "yep, it works"
|
21
24
|
end
|
22
25
|
|
23
|
-
subject(:dsc) { Dsc.load_file(config_file, env: env) }
|
26
|
+
subject(:dsc) { Dsc.load_file(config_file, env: env, optional: optional) }
|
24
27
|
|
25
28
|
let(:config_file) { File.expand_path(__FILE__ + '/../../tmp/config.yml') }
|
26
29
|
let(:env) { 'development' }
|
30
|
+
let(:optional) { [] }
|
27
31
|
|
28
32
|
it 'works' do
|
29
33
|
expect(dsc).to have_attributes(
|
@@ -44,6 +48,14 @@ EOF
|
|
44
48
|
)
|
45
49
|
end
|
46
50
|
|
51
|
+
context "when there are optional attributes" do
|
52
|
+
let(:optional) { %i(awesome) }
|
53
|
+
|
54
|
+
it "does not raise error when trying to access undefined but optional attribute" do
|
55
|
+
expect(dsc.awesome).to eq(nil)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
47
59
|
context 'with env production' do
|
48
60
|
let(:env) { 'production' }
|
49
61
|
|
@@ -53,6 +65,27 @@ EOF
|
|
53
65
|
awesome: true
|
54
66
|
)
|
55
67
|
end
|
68
|
+
|
69
|
+
context "when there are optional attributes" do
|
70
|
+
let(:optional) { %i(awesome) }
|
71
|
+
|
72
|
+
it "provides these attributes properly" do
|
73
|
+
expect(dsc).to have_attributes(
|
74
|
+
host: 'http://www.wimdu.com',
|
75
|
+
awesome: true
|
76
|
+
)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe 'ERB parsing' do
|
82
|
+
let(:env) { 'production' }
|
83
|
+
|
84
|
+
it 'works' do
|
85
|
+
expect(dsc).to have_attributes(
|
86
|
+
some_inline_erb: 'yep, it works'
|
87
|
+
)
|
88
|
+
end
|
56
89
|
end
|
57
90
|
end
|
58
91
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dsc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Johannes Barre
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2015-04-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: hashie
|
@@ -31,14 +31,14 @@ dependencies:
|
|
31
31
|
requirements:
|
32
32
|
- - "~>"
|
33
33
|
- !ruby/object:Gem::Version
|
34
|
-
version: '1.
|
34
|
+
version: '1.6'
|
35
35
|
type: :development
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
39
|
- - "~>"
|
40
40
|
- !ruby/object:Gem::Version
|
41
|
-
version: '1.
|
41
|
+
version: '1.6'
|
42
42
|
- !ruby/object:Gem::Dependency
|
43
43
|
name: rake
|
44
44
|
requirement: !ruby/object:Gem::Requirement
|
@@ -77,6 +77,7 @@ extra_rdoc_files: []
|
|
77
77
|
files:
|
78
78
|
- ".gitignore"
|
79
79
|
- ".ruby-version"
|
80
|
+
- ".travis.yml"
|
80
81
|
- Gemfile
|
81
82
|
- LICENSE.txt
|
82
83
|
- README.md
|