configru 3.4.0 → 3.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ChangeLog.md +4 -0
- data/Gemfile.lock +1 -1
- data/README.md +5 -2
- data/lib/configru/config.rb +6 -4
- data/lib/configru/exceptions.rb +9 -9
- data/lib/configru/option.rb +1 -1
- data/lib/configru/version.rb +1 -1
- data/spec/config_spec.rb +8 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 411032b2676edb0558982ceeeba01327afa061ea
|
4
|
+
data.tar.gz: e4cdc8a16b7d93d38fea32109f4f63c5470a2137
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c97c37b22cf0bf4e28bd076970b82d6a325cdb85a68d7312c821fcd747c943ac007fbb0650e0138c7ba33fd77c568b48e3010f85900bd540804b54db719533bb
|
7
|
+
data.tar.gz: 424fcc556bd4f9f42694db51fed76673153c0dc0ce4cf8ee9f6f4a095ebd27b819b58945f4751640be76980ceb39cebceac09418fd3afea37cb940c351955f0c
|
data/ChangeLog.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -7,7 +7,7 @@ YAML configuration file loader
|
|
7
7
|
Install the gem or add it to your `Gemfile`:
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
gem 'configru', '~> 3.
|
10
|
+
gem 'configru', '~> 3.5.0'
|
11
11
|
```
|
12
12
|
|
13
13
|
Next, require it and load a configuration:
|
@@ -33,7 +33,10 @@ end
|
|
33
33
|
```
|
34
34
|
|
35
35
|
`option` takes 4 arguments: the configuration key, the type of value,
|
36
|
-
the default value, and
|
36
|
+
the default value, and a validation check. The type defaults to
|
37
|
+
`Object`, the default value defaults to `nil` and the validation check
|
38
|
+
defaults to nothing. Note that a value of `nil` will be treated as the
|
39
|
+
correct type regardless of the specified type.
|
37
40
|
|
38
41
|
The configuration option above can then be accessed in two ways:
|
39
42
|
|
data/lib/configru/config.rb
CHANGED
@@ -24,6 +24,7 @@ module Configru
|
|
24
24
|
# Load all defaults if no files were loaded
|
25
25
|
# TODO: Some way to not special case this
|
26
26
|
@option_path = Array.new
|
27
|
+
@file = '(none)'
|
27
28
|
load_group(@options, self, {}) if loaded_files.empty?
|
28
29
|
end
|
29
30
|
|
@@ -31,6 +32,7 @@ module Configru
|
|
31
32
|
|
32
33
|
def load_file(file)
|
33
34
|
@option_path = Array.new
|
35
|
+
@file = file
|
34
36
|
load_group(@options, self, YAML.load_file(file) || {})
|
35
37
|
end
|
36
38
|
|
@@ -41,7 +43,7 @@ module Configru
|
|
41
43
|
# option is a group
|
42
44
|
if option.is_a? Hash
|
43
45
|
if input.has_key?(key) && !input[key].is_a?(Hash)
|
44
|
-
raise OptionTypeError.new(@option_path, Hash, input[key].class)
|
46
|
+
raise OptionTypeError.new(@file, @option_path, Hash, input[key].class)
|
45
47
|
end
|
46
48
|
group_output = output[key] || StructHash.new
|
47
49
|
load_group(option, group_output, input[key] || {})
|
@@ -56,17 +58,17 @@ module Configru
|
|
56
58
|
@option_path.pop
|
57
59
|
next
|
58
60
|
elsif option.is_a? RequiredOption
|
59
|
-
raise OptionRequiredError.new(@option_path)
|
61
|
+
raise OptionRequiredError.new(@file, @option_path)
|
60
62
|
else # option has not been set
|
61
63
|
value = option.default
|
62
64
|
end
|
63
65
|
|
64
66
|
unless option.type?(value)
|
65
|
-
raise OptionTypeError.new(@option_path, option.type, value.class)
|
67
|
+
raise OptionTypeError.new(@file, @option_path, option.type, value.class)
|
66
68
|
end
|
67
69
|
|
68
70
|
unless option.valid?(value)
|
69
|
-
raise OptionValidationError.new(@option_path, option.validation)
|
71
|
+
raise OptionValidationError.new(@file, @option_path, option.validation)
|
70
72
|
end
|
71
73
|
|
72
74
|
output[key] = option.transform(value)
|
data/lib/configru/exceptions.rb
CHANGED
@@ -1,28 +1,28 @@
|
|
1
1
|
module Configru
|
2
2
|
class OptionError < RuntimeError
|
3
|
-
def initialize(path, message)
|
4
|
-
super("#{path.join('.')}: #{message}")
|
3
|
+
def initialize(file, path, message)
|
4
|
+
super("in #{file} at #{path.join('.')}: #{message}")
|
5
5
|
end
|
6
6
|
end
|
7
7
|
|
8
8
|
class OptionRequiredError < OptionError
|
9
|
-
def initialize(path)
|
10
|
-
super(path, 'option required')
|
9
|
+
def initialize(file, path)
|
10
|
+
super(file, path, 'option required')
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
14
|
class OptionTypeError < OptionError
|
15
|
-
def initialize(path, expected, got)
|
16
|
-
super(path, "expected #{expected}, got #{got}")
|
15
|
+
def initialize(file, path, expected, got)
|
16
|
+
super(file, path, "expected #{expected}, got #{got}")
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
20
|
class OptionValidationError < OptionError
|
21
|
-
def initialize(path, validation
|
21
|
+
def initialize(file, path, validation)
|
22
22
|
if validation.is_a?(Proc)
|
23
|
-
super(path,
|
23
|
+
super(file, path, 'failed validation')
|
24
24
|
else
|
25
|
-
super(path, "failed validation `#{validation.inspect}`")
|
25
|
+
super(file, path, "failed validation `#{validation.inspect}`")
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
data/lib/configru/option.rb
CHANGED
data/lib/configru/version.rb
CHANGED
data/spec/config_spec.rb
CHANGED
@@ -96,6 +96,14 @@ describe Configru::Config do
|
|
96
96
|
end.to raise_error(Configru::OptionTypeError)
|
97
97
|
end
|
98
98
|
|
99
|
+
it 'treats nil as if it were the right type' do
|
100
|
+
c = described_class.new do
|
101
|
+
option :string, String
|
102
|
+
end
|
103
|
+
|
104
|
+
c.string.should == nil
|
105
|
+
end
|
106
|
+
|
99
107
|
it 'validates options against values' do
|
100
108
|
expect do
|
101
109
|
c = described_class.new(example_file :d) do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: configru
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Curtis McEnroe
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-10 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: YAML configuration file loader
|
14
14
|
email:
|