konf 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.md +50 -0
- data/VERSION +1 -1
- data/konf.gemspec +51 -0
- data/lib/konf.rb +29 -1
- data/test/config.yml +6 -0
- data/test/konf_test.rb +70 -2
- metadata +11 -7
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Oleg Khabarov
|
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 sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -0,0 +1,50 @@
|
|
1
|
+
# Konf
|
2
|
+
|
3
|
+
**Konf** is a simple configuration reader.
|
4
|
+
|
5
|
+
## Setup
|
6
|
+
|
7
|
+
Simply install the gem and/or plug this into the Gemfile:
|
8
|
+
|
9
|
+
gem 'konf'
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
Assuming you have a configuration file like this:
|
14
|
+
|
15
|
+
development:
|
16
|
+
admin:
|
17
|
+
name: Dev
|
18
|
+
email: dev@test.test
|
19
|
+
production:
|
20
|
+
admin:
|
21
|
+
name: Pro
|
22
|
+
email: pro@test.test
|
23
|
+
|
24
|
+
You can load a YAML file, or provide a simple Hash and read configuration values like this:
|
25
|
+
|
26
|
+
>> conf = Konf.new('configuration.yml')
|
27
|
+
>> conf.development.name
|
28
|
+
=> 'Dev'
|
29
|
+
|
30
|
+
If you try to read a configuration that wasn't defined:
|
31
|
+
|
32
|
+
>> conf.development.invalid
|
33
|
+
=> Konf::NotFound: No configuration found for 'invalid'
|
34
|
+
|
35
|
+
You can check if configuration exists:
|
36
|
+
|
37
|
+
>> conf.development.admin?
|
38
|
+
=> true
|
39
|
+
>> conf.development.invalid?
|
40
|
+
=> false
|
41
|
+
|
42
|
+
You can namespace your configuration:
|
43
|
+
|
44
|
+
>> conf = Konf.new('configuration.yml', 'production')
|
45
|
+
>> conf.admin.name
|
46
|
+
=> 'Pro'
|
47
|
+
|
48
|
+
## Author
|
49
|
+
|
50
|
+
(c) 2011 Oleg Khabarov, released under the MIT license
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.1
|
data/konf.gemspec
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "konf"
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Oleg Khabarov"]
|
12
|
+
s.date = "2011-12-21"
|
13
|
+
s.description = "minimalistic configuration reader"
|
14
|
+
s.email = "oleg@khabarov.ca"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
"Gemfile",
|
21
|
+
"LICENSE",
|
22
|
+
"README.md",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"konf.gemspec",
|
26
|
+
"lib/konf.rb",
|
27
|
+
"test/config.yml",
|
28
|
+
"test/konf_test.rb"
|
29
|
+
]
|
30
|
+
s.homepage = "http://github.com/GBH/konf"
|
31
|
+
s.licenses = ["MIT"]
|
32
|
+
s.require_paths = ["lib"]
|
33
|
+
s.rubygems_version = "1.8.10"
|
34
|
+
s.summary = "minimalistic configuration reader"
|
35
|
+
|
36
|
+
if s.respond_to? :specification_version then
|
37
|
+
s.specification_version = 3
|
38
|
+
|
39
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
40
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
41
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
42
|
+
else
|
43
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
44
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
45
|
+
end
|
46
|
+
else
|
47
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
48
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
data/lib/konf.rb
CHANGED
@@ -1,3 +1,31 @@
|
|
1
|
-
|
1
|
+
require 'yaml'
|
2
|
+
require 'erb'
|
3
|
+
|
4
|
+
class Konf < Hash
|
5
|
+
class NotFound < StandardError; end
|
6
|
+
|
7
|
+
def initialize(source, root = nil)
|
8
|
+
hash = case source
|
9
|
+
when Hash
|
10
|
+
source
|
11
|
+
else
|
12
|
+
YAML.load(ERB.new(File.read(source)).result).to_hash
|
13
|
+
end
|
14
|
+
if root
|
15
|
+
hash = hash[root] or raise NotFound, "No configuration found for '#{root}'"
|
16
|
+
end
|
17
|
+
self.replace hash
|
18
|
+
end
|
19
|
+
|
20
|
+
def method_missing(name, *args, &block)
|
21
|
+
key = name.to_s
|
22
|
+
if key.gsub!(/\?$/, '')
|
23
|
+
has_key? key
|
24
|
+
else
|
25
|
+
raise NotFound, "No configuration found for '#{name}'" unless has_key?(key)
|
26
|
+
value = fetch key
|
27
|
+
value.is_a?(Hash) ? Konf.new(value) : value
|
28
|
+
end
|
29
|
+
end
|
2
30
|
|
3
31
|
end
|
data/test/config.yml
ADDED
data/test/konf_test.rb
CHANGED
@@ -3,8 +3,76 @@ require 'konf'
|
|
3
3
|
|
4
4
|
class KonfTest < Test::Unit::TestCase
|
5
5
|
|
6
|
-
def
|
7
|
-
|
6
|
+
def test_initialization_as_hash
|
7
|
+
conf = Konf.new('a' => {'b' => 'c'})
|
8
|
+
assert_equal ({'a' => {'b' => 'c'}}), conf
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_initialization_as_yaml
|
12
|
+
conf = Konf.new(File.expand_path('config.yml', File.dirname(__FILE__)))
|
13
|
+
assert_equal ({
|
14
|
+
'conf_a' => {
|
15
|
+
'conf_1' => 'value',
|
16
|
+
'conf_2' => 'value 2',
|
17
|
+
},
|
18
|
+
'conf_b' => {
|
19
|
+
'conf_1' => 'value'
|
20
|
+
}
|
21
|
+
}), conf
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_initialization_with_root
|
25
|
+
conf = Konf.new({'a' => {'b' => 'c'}}, 'a')
|
26
|
+
assert_equal ({'b' => 'c'}), conf
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_initialization_with_blank_source
|
30
|
+
begin
|
31
|
+
conf = Konf.new
|
32
|
+
rescue => e
|
33
|
+
assert_equal ArgumentError, e.class
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_initialization_with_invalid_file
|
38
|
+
begin
|
39
|
+
conf = Konf.new('invalid')
|
40
|
+
rescue => e
|
41
|
+
assert_equal Errno::ENOENT, e.class
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_initialization_with_invalid_root
|
46
|
+
begin
|
47
|
+
conf = Konf.new({'a' => {'b' => 'c'}}, 'd')
|
48
|
+
rescue => e
|
49
|
+
assert_equal Konf::NotFound, e.class
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_accessors
|
54
|
+
conf = Konf.new('a' => {'b' => 'c'})
|
55
|
+
|
56
|
+
assert_equal ({'b' => 'c'}), conf.a
|
57
|
+
assert_equal Konf, conf.a.class
|
58
|
+
assert_equal 'c', conf.a.b
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_accessors_undefined
|
62
|
+
conf = Konf.new('a' => {'b' => 'c'})
|
63
|
+
begin
|
64
|
+
conf.z
|
65
|
+
rescue => e
|
66
|
+
assert_equal Konf::NotFound, e.class
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_accessors_inspector
|
71
|
+
conf = Konf.new('a' => {'b' => 'c'})
|
72
|
+
assert conf.a?
|
73
|
+
assert !conf.z?
|
74
|
+
assert conf.a.b?
|
75
|
+
assert !conf.a.z?
|
8
76
|
end
|
9
77
|
|
10
78
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: konf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-12-
|
12
|
+
date: 2011-12-21 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
16
|
-
requirement: &
|
16
|
+
requirement: &70176349459780 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 1.0.0
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70176349459780
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: jeweler
|
27
|
-
requirement: &
|
27
|
+
requirement: &70176349459300 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,19 +32,23 @@ dependencies:
|
|
32
32
|
version: 1.6.4
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70176349459300
|
36
36
|
description: minimalistic configuration reader
|
37
37
|
email: oleg@khabarov.ca
|
38
38
|
executables: []
|
39
39
|
extensions: []
|
40
40
|
extra_rdoc_files:
|
41
|
+
- LICENSE
|
41
42
|
- README.md
|
42
43
|
files:
|
43
44
|
- Gemfile
|
45
|
+
- LICENSE
|
44
46
|
- README.md
|
45
47
|
- Rakefile
|
46
48
|
- VERSION
|
49
|
+
- konf.gemspec
|
47
50
|
- lib/konf.rb
|
51
|
+
- test/config.yml
|
48
52
|
- test/konf_test.rb
|
49
53
|
homepage: http://github.com/GBH/konf
|
50
54
|
licenses:
|
@@ -61,7 +65,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
61
65
|
version: '0'
|
62
66
|
segments:
|
63
67
|
- 0
|
64
|
-
hash:
|
68
|
+
hash: 2717882700731022676
|
65
69
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
70
|
none: false
|
67
71
|
requirements:
|