miniconfig 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.travis.yml +9 -0
- data/Gemfile +3 -0
- data/LICENSE +22 -0
- data/README.md +173 -0
- data/Rakefile +9 -0
- data/lib/miniconfig.rb +35 -0
- data/lib/miniconfig/version.rb +3 -0
- data/miniconfig.gemspec +26 -0
- data/spec/config_spec.rb +53 -0
- data/spec/fixtures/config_1.yml +2 -0
- data/spec/fixtures/config_1_1.yml +1 -0
- data/spec/fixtures/config_2.yml +6 -0
- data/spec/fixtures/config_2_1.yml +4 -0
- data/spec/fixtures/config_3.yml +6 -0
- data/spec/fixtures/config_3_1.yml +4 -0
- data/spec/minitest_helper.rb +8 -0
- metadata +125 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e56e0a7e8df417859d0aea5cc5ed2b737ab74f07
|
4
|
+
data.tar.gz: 183f1db6ad0d62bcb223234ae7f870f8929a67b2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1bf48c34ee02a772a29bac5981039f2ed3467e978c93d268571d776e01e2aa48c18a76468664eee4fe66a3505ca77da7beb7783cd342cc3cd6acfc3ef8b15d07
|
7
|
+
data.tar.gz: eb735fb8f7cb91ac280272346d47fa48df71d4ad08ab010d54a22fa3d439b9dfe36368cab35cf2f4de22f705d2a374a9f4393ed7373150b9f057970c0deed3cf
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Patricio Mac Adden
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,173 @@
|
|
1
|
+
# Miniconfig
|
2
|
+
|
3
|
+
[](https://travis-ci.org/patriciomacadden/miniconfig)
|
4
|
+
[](https://codeclimate.com/github/patriciomacadden/miniconfig)
|
5
|
+
|
6
|
+
Minimalistic configuration files for your projects.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'miniconfig'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
```
|
19
|
+
$ bundle
|
20
|
+
```
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
```
|
25
|
+
$ gem install miniconfig
|
26
|
+
```
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
Create your application configuration files in yaml format. For instance:
|
31
|
+
|
32
|
+
```yaml
|
33
|
+
# config/app.yml
|
34
|
+
app:
|
35
|
+
name: Amazing app with great configuration files handling
|
36
|
+
version: 0.1.0
|
37
|
+
some:
|
38
|
+
cool:
|
39
|
+
setting: Some Cool Setting
|
40
|
+
cooler:
|
41
|
+
setting: Some Other Even Cooler Setting
|
42
|
+
```
|
43
|
+
|
44
|
+
Then, load this configuration into an object:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
config = Miniconfig.load 'config/app.yml'
|
48
|
+
```
|
49
|
+
|
50
|
+
And access your application configuration:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
config.app.name # => "Amazing app with great configuration files handling"
|
54
|
+
config.app.version # => "0.1.0"
|
55
|
+
config.some.cool.setting # => "Some Cool Setting"
|
56
|
+
config.some.other.setting # => "Some Other Even Cooler Setting"
|
57
|
+
```
|
58
|
+
|
59
|
+
### Loading more than one file
|
60
|
+
|
61
|
+
You can load more than one file. Suppose that we have the file `config/app.yml`
|
62
|
+
as defined above (it contains general settings for your application) and
|
63
|
+
`config/development.yml` (which contains settings for your application in
|
64
|
+
development mode).
|
65
|
+
|
66
|
+
You can load those files using:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
config = Miniconfig.load 'config/app.yml', 'config/development.yml'
|
70
|
+
```
|
71
|
+
|
72
|
+
And then access your application configuration as usual:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
config.app.name # => "Amazing app with great configuration files handling"
|
76
|
+
config.app.version # => "0.1.0"
|
77
|
+
config.some.cool.setting # => "Some Cool Setting"
|
78
|
+
config.some.other.setting # => "Some Other Even Cooler Setting"
|
79
|
+
```
|
80
|
+
|
81
|
+
#### Precedence
|
82
|
+
|
83
|
+
When loading multiple yaml files, the values defined in the second have higher
|
84
|
+
precedence. See the example:
|
85
|
+
|
86
|
+
```yaml
|
87
|
+
# config/app.yml
|
88
|
+
app:
|
89
|
+
name: Application Name
|
90
|
+
version: 0.1.0
|
91
|
+
```
|
92
|
+
|
93
|
+
```yaml
|
94
|
+
# config/development.yml
|
95
|
+
app:
|
96
|
+
name: Application Name [DEVELOPMENT]
|
97
|
+
some:
|
98
|
+
setting: Some Setting
|
99
|
+
```
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
config.app.name # => "Application Name [DEVELOPMENT]"
|
103
|
+
config.app.version # => "0.1.0"
|
104
|
+
config.some.setting # => "Some Setting"
|
105
|
+
```
|
106
|
+
|
107
|
+
## Integration with Sinatra
|
108
|
+
|
109
|
+
The integration with sinatra is pretty simple, you must create a
|
110
|
+
helper method that creates the config object:
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
require 'sinatra'
|
114
|
+
|
115
|
+
helpers do
|
116
|
+
def config
|
117
|
+
@config ||= Miniconfig.load 'config/app.yml'
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
get '/' do
|
122
|
+
config.app.name
|
123
|
+
end
|
124
|
+
```
|
125
|
+
|
126
|
+
## Integration with Rails
|
127
|
+
|
128
|
+
The integration with rails is also pretty straightforward, as in
|
129
|
+
the previous example, just create a helper:
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
# config/application.rb
|
133
|
+
|
134
|
+
module SomeApplication
|
135
|
+
class Application < Rails::Application
|
136
|
+
# configuration options here
|
137
|
+
|
138
|
+
def miniconfig
|
139
|
+
@miniconfig ||= Miniconfig.load 'config/app.yml'
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
```
|
144
|
+
|
145
|
+
And then access the application instance:
|
146
|
+
|
147
|
+
```ruby
|
148
|
+
class SomeController < ApplicationController
|
149
|
+
def index
|
150
|
+
config = SomeApplication::Application.instance.miniconfig
|
151
|
+
|
152
|
+
render text: config.app.name
|
153
|
+
end
|
154
|
+
end
|
155
|
+
```
|
156
|
+
|
157
|
+
Obviously, this can be done in many different ways and integrated
|
158
|
+
with any Ruby application. But, for instance, if you need to access
|
159
|
+
the configuration options only in your rails app controllers, define this
|
160
|
+
method on the `ApplicationController`. Choose the one that suits best, and
|
161
|
+
may the force be with you.
|
162
|
+
|
163
|
+
## Contributing
|
164
|
+
|
165
|
+
1. Fork it
|
166
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
167
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
168
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
169
|
+
5. Create new Pull Request
|
170
|
+
|
171
|
+
## TODO
|
172
|
+
|
173
|
+
* Do not depend on `activesupport` (a.k.a. implement `deep_merge`)
|
data/Rakefile
ADDED
data/lib/miniconfig.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'active_support/core_ext'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
require 'miniconfig/version'
|
5
|
+
|
6
|
+
module Miniconfig
|
7
|
+
def self.load(*filenames)
|
8
|
+
data = {}
|
9
|
+
filenames.each do |filename|
|
10
|
+
data.deep_merge! YAML.load(File.read(filename))
|
11
|
+
end
|
12
|
+
Config.new(data)
|
13
|
+
end
|
14
|
+
|
15
|
+
class Config
|
16
|
+
def initialize(data = {})
|
17
|
+
build self, data
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def build(object, hash)
|
23
|
+
if hash.is_a? Hash
|
24
|
+
hash.each do |k, v|
|
25
|
+
value = v.is_a?(Hash) ? Config.new(v) : v
|
26
|
+
|
27
|
+
self.class.send(:attr_reader, k.to_sym)
|
28
|
+
instance_variable_set :"@#{k}", value
|
29
|
+
end
|
30
|
+
else
|
31
|
+
hash
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/miniconfig.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'miniconfig/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'miniconfig'
|
8
|
+
spec.version = Miniconfig::VERSION
|
9
|
+
spec.authors = ['Patricio Mac Adden']
|
10
|
+
spec.email = ['patriciomacadden@gmail.com']
|
11
|
+
spec.description = %q{Minimalistic configuration files for your projects.}
|
12
|
+
spec.summary = %q{Minimalistic configuration files for your projects.}
|
13
|
+
spec.homepage = ''
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_dependency 'activesupport'
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
24
|
+
spec.add_development_dependency 'minitest'
|
25
|
+
spec.add_development_dependency 'rake'
|
26
|
+
end
|
data/spec/config_spec.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'minitest_helper'
|
2
|
+
|
3
|
+
describe Miniconfig::Config do
|
4
|
+
it 'loads a single yaml file with 1 level' do
|
5
|
+
config = Miniconfig.load fixture_path('config_1.yml')
|
6
|
+
|
7
|
+
config.one.must_equal 1
|
8
|
+
config.two.must_equal 2
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'loads a single yaml file with 2 levels' do
|
12
|
+
config = Miniconfig.load fixture_path('config_2.yml')
|
13
|
+
|
14
|
+
config.one.one_one.must_equal 1.1
|
15
|
+
config.one.one_two.must_equal 1.2
|
16
|
+
config.two.two_one.must_equal 2.1
|
17
|
+
config.two.two_two.must_equal 2.2
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'loads a single yaml file with 3 levels' do
|
21
|
+
config = Miniconfig.load fixture_path('config_3.yml')
|
22
|
+
|
23
|
+
config.one.one_one.must_equal 1.1
|
24
|
+
config.one.one_two.must_equal 1.2
|
25
|
+
config.one.two.two_one.must_equal 2.1
|
26
|
+
config.one.two.two_two.must_equal 2.2
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'loads multiple yaml files with 1 level' do
|
30
|
+
config = Miniconfig.load fixture_path('config_1.yml'), fixture_path('config_1_1.yml')
|
31
|
+
|
32
|
+
config.one.must_equal 'One'
|
33
|
+
config.two.must_equal 2
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'loads multiple yaml files with 2 levels' do
|
37
|
+
config = Miniconfig.load fixture_path('config_2.yml'), fixture_path('config_2_1.yml')
|
38
|
+
|
39
|
+
config.one.one_one.must_equal 'One.One'
|
40
|
+
config.one.one_two.must_equal 1.2
|
41
|
+
config.two.two_one.must_equal 'Two.One'
|
42
|
+
config.two.two_two.must_equal 2.2
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'loads multiple yaml files with 3 levels' do
|
46
|
+
config = Miniconfig.load fixture_path('config_3.yml'), fixture_path('config_3_1.yml')
|
47
|
+
|
48
|
+
config.one.one_one.must_equal 'One.One'
|
49
|
+
config.one.one_two.must_equal 1.2
|
50
|
+
config.one.two.two_one.must_equal 2.1
|
51
|
+
config.one.two.two_two.must_equal 'Two.Two'
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
one: One
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: miniconfig
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Patricio Mac Adden
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-04-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Minimalistic configuration files for your projects.
|
70
|
+
email:
|
71
|
+
- patriciomacadden@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .travis.yml
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- lib/miniconfig.rb
|
83
|
+
- lib/miniconfig/version.rb
|
84
|
+
- miniconfig.gemspec
|
85
|
+
- spec/config_spec.rb
|
86
|
+
- spec/fixtures/config_1.yml
|
87
|
+
- spec/fixtures/config_1_1.yml
|
88
|
+
- spec/fixtures/config_2.yml
|
89
|
+
- spec/fixtures/config_2_1.yml
|
90
|
+
- spec/fixtures/config_3.yml
|
91
|
+
- spec/fixtures/config_3_1.yml
|
92
|
+
- spec/minitest_helper.rb
|
93
|
+
homepage: ''
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata: {}
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 2.0.0
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: Minimalistic configuration files for your projects.
|
117
|
+
test_files:
|
118
|
+
- spec/config_spec.rb
|
119
|
+
- spec/fixtures/config_1.yml
|
120
|
+
- spec/fixtures/config_1_1.yml
|
121
|
+
- spec/fixtures/config_2.yml
|
122
|
+
- spec/fixtures/config_2_1.yml
|
123
|
+
- spec/fixtures/config_3.yml
|
124
|
+
- spec/fixtures/config_3_1.yml
|
125
|
+
- spec/minitest_helper.rb
|