configru 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog.md +9 -0
- data/README.md +56 -13
- data/lib/configru/confighash.rb +3 -3
- data/lib/configru/dsl.rb +25 -11
- data/lib/configru/version.rb +1 -1
- data/lib/configru.rb +10 -12
- data/test/loaddsl_test.rb +24 -0
- metadata +23 -39
data/ChangeLog.md
CHANGED
@@ -1,5 +1,14 @@
|
|
1
1
|
# Change Log
|
2
2
|
|
3
|
+
## 0.4.0 (8 January 2012)
|
4
|
+
|
5
|
+
* Added `cascade 'c', ['a', 'b']` form
|
6
|
+
|
7
|
+
## 0.3.0 (30 July 2011)
|
8
|
+
|
9
|
+
* Added `Configru.loaded_files`
|
10
|
+
* Added the `options` method to the `Configru.load` DSL
|
11
|
+
|
3
12
|
## 0.2.0 (19 July 2011)
|
4
13
|
|
5
14
|
* Added loading defaults from file
|
data/README.md
CHANGED
@@ -2,16 +2,16 @@
|
|
2
2
|
|
3
3
|
Versatile configuration file loader for Ruby
|
4
4
|
|
5
|
-
|
5
|
+
# Installation
|
6
6
|
|
7
7
|
gem install configru
|
8
8
|
|
9
|
-
|
9
|
+
# Usage
|
10
10
|
|
11
11
|
Configru loads YAML configuration files and provides a simple way to access
|
12
12
|
configuration options.
|
13
13
|
|
14
|
-
|
14
|
+
## Loading Configuration Files
|
15
15
|
|
16
16
|
Configru provides a DSL for loading configuration files.
|
17
17
|
|
@@ -27,7 +27,7 @@ At the very least, the block passed to `Configru.load` must tell Configru which
|
|
27
27
|
files it should load. There are two different methods of loading configuration
|
28
28
|
files available.
|
29
29
|
|
30
|
-
|
30
|
+
### Just load a file already!
|
31
31
|
|
32
32
|
This is the simplest method of loading. It just loads a file.
|
33
33
|
|
@@ -37,7 +37,7 @@ Configru.load do
|
|
37
37
|
end
|
38
38
|
```
|
39
39
|
|
40
|
-
|
40
|
+
### First-of Loading
|
41
41
|
|
42
42
|
This method loads the first file that exists, ignoring all other files.
|
43
43
|
|
@@ -47,7 +47,7 @@ Configru.load do
|
|
47
47
|
end
|
48
48
|
```
|
49
49
|
|
50
|
-
|
50
|
+
### Cascading Loading
|
51
51
|
|
52
52
|
This method loads every file that exists in reverse order. Files listed first
|
53
53
|
overwrite the values from files listed later. (Files are listed in high to low
|
@@ -59,12 +59,13 @@ Configru.load do
|
|
59
59
|
end
|
60
60
|
```
|
61
61
|
|
62
|
-
|
62
|
+
## Accessing Options
|
63
63
|
|
64
64
|
Configuration options can be accessed as methods of the `Configru` module, or
|
65
65
|
`Configru` can be used as a Hash.
|
66
66
|
|
67
|
-
|
67
|
+
`foo.yml`
|
68
|
+
|
68
69
|
```yaml
|
69
70
|
nick: bob
|
70
71
|
server:
|
@@ -72,7 +73,8 @@ server:
|
|
72
73
|
port: 6782
|
73
74
|
```
|
74
75
|
|
75
|
-
|
76
|
+
`foo.rb`
|
77
|
+
|
76
78
|
```ruby
|
77
79
|
require 'configru'
|
78
80
|
require 'socket'
|
@@ -88,7 +90,7 @@ s.puts "Hello, I am #{Configru.nick}"
|
|
88
90
|
Configuration options with hyphens can be accessed as methods by replacing the
|
89
91
|
hyphens with underscores.
|
90
92
|
|
91
|
-
|
93
|
+
## Defaults
|
92
94
|
|
93
95
|
Configru's load DSL allows for setting configuration defaults using a block.
|
94
96
|
If no configuration files are found or if the configuration file omits an
|
@@ -130,7 +132,7 @@ end
|
|
130
132
|
```
|
131
133
|
|
132
134
|
Defaults can also be loaded from a YAML file by passing the filename to
|
133
|
-
`
|
135
|
+
`defaults`.
|
134
136
|
|
135
137
|
```ruby
|
136
138
|
Configru.load do
|
@@ -139,12 +141,53 @@ Configru.load do
|
|
139
141
|
end
|
140
142
|
```
|
141
143
|
|
142
|
-
|
144
|
+
## Verifying options
|
143
145
|
|
144
146
|
Configru provides a way to verify that configuration options meet certain
|
145
147
|
requirements. This is done using a `verify` block in `Configru.load`.
|
146
148
|
|
147
|
-
|
149
|
+
```ruby
|
150
|
+
Configru.load do
|
151
|
+
just 'foo.yml'
|
152
|
+
verify do
|
153
|
+
foo Fixnum
|
154
|
+
bar /^a+$/
|
155
|
+
baz ['one', 'two']
|
156
|
+
end
|
157
|
+
end
|
158
|
+
```
|
159
|
+
|
160
|
+
Upon loading the configuration, Configru checks each option against this verify
|
161
|
+
block. In most cases, the `===` operator is used to compare the values, but
|
162
|
+
there are some special cases. If the verification value is a class, `is_a?` is
|
163
|
+
used. If the verification value is an array, `include?` is used. This
|
164
|
+
effectively means that with a class, the value must be an instance of that
|
165
|
+
class, and with an array, the value must be one of the values in the array.
|
166
|
+
|
167
|
+
FIXME: Talk about how Configru deals with invalid options
|
168
|
+
|
169
|
+
## Doing two things at once
|
170
|
+
|
171
|
+
Configru also has an `options` block in `Configru.load` which allows for
|
172
|
+
combining the `defaults` and `verify` blocks.
|
173
|
+
|
174
|
+
```ruby
|
175
|
+
Configru.load do
|
176
|
+
just 'foo.yml'
|
177
|
+
options do
|
178
|
+
nick String, 'Dr. Nader'
|
179
|
+
server do
|
180
|
+
address String, 'abcd.com'
|
181
|
+
port (0..65535), 1111
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
```
|
186
|
+
|
187
|
+
In the `options` block, each option takes two arguments, the first being the
|
188
|
+
verification value, and the second being the default value.
|
189
|
+
|
190
|
+
# License
|
148
191
|
|
149
192
|
Copyright (c) 2011, Curtis McEnroe <programble@gmail.com>
|
150
193
|
|
data/lib/configru/confighash.rb
CHANGED
@@ -4,7 +4,7 @@ module Configru
|
|
4
4
|
super
|
5
5
|
merge!(hash)
|
6
6
|
end
|
7
|
-
|
7
|
+
|
8
8
|
def merge!(hash)
|
9
9
|
hash.each do |key, value|
|
10
10
|
if value.is_a?(Hash) && self[key].is_a?(ConfigHash)
|
@@ -16,7 +16,7 @@ module Configru
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
def [](key)
|
21
21
|
key = key.to_s if key.is_a?(Symbol)
|
22
22
|
# Allow for accessing keys with hypens using underscores
|
@@ -25,7 +25,7 @@ module Configru
|
|
25
25
|
# doesn't exist :\
|
26
26
|
super(key) if self.include?(key)
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
def method_missing(key, *args)
|
30
30
|
self[key]
|
31
31
|
end
|
data/lib/configru/dsl.rb
CHANGED
@@ -2,7 +2,7 @@ module Configru
|
|
2
2
|
module DSL
|
3
3
|
class LoadDSL
|
4
4
|
attr_reader :defaults_hash, :verify_hash, :files_array, :load_method
|
5
|
-
|
5
|
+
|
6
6
|
def initialize(block)
|
7
7
|
@defaults_hash = {}
|
8
8
|
@verify_hash = {}
|
@@ -11,18 +11,30 @@ module Configru
|
|
11
11
|
instance_eval(&block)
|
12
12
|
end
|
13
13
|
|
14
|
+
def files(*args)
|
15
|
+
if args[0].is_a?(String) && args[1].is_a?(Array)
|
16
|
+
@files_array = args[1].map {|x| File.join(x, args[0])}
|
17
|
+
else
|
18
|
+
@files_array = args
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def method(m)
|
23
|
+
@load_method = m
|
24
|
+
end
|
25
|
+
|
14
26
|
def first_of(*args)
|
15
|
-
|
16
|
-
|
27
|
+
method(:first)
|
28
|
+
files(*args)
|
17
29
|
end
|
18
30
|
|
19
31
|
alias :just :first_of
|
20
|
-
|
32
|
+
|
21
33
|
def cascade(*args)
|
22
|
-
|
23
|
-
|
34
|
+
method(:cascade)
|
35
|
+
files(*args)
|
24
36
|
end
|
25
|
-
|
37
|
+
|
26
38
|
def defaults(arg=nil, &block)
|
27
39
|
if arg.is_a? String
|
28
40
|
@defaults_hash = YAML.load_file(arg)
|
@@ -32,7 +44,7 @@ module Configru
|
|
32
44
|
@defaults_hash = HashDSL.new(block).hash
|
33
45
|
end
|
34
46
|
end
|
35
|
-
|
47
|
+
|
36
48
|
def verify(hash=nil, &block)
|
37
49
|
if hash
|
38
50
|
@verify_hash = hash
|
@@ -47,20 +59,22 @@ module Configru
|
|
47
59
|
@verify_hash = hashes.hash1
|
48
60
|
end
|
49
61
|
end
|
50
|
-
|
62
|
+
|
51
63
|
class HashDSL
|
52
64
|
attr_reader :hash
|
53
|
-
|
65
|
+
|
54
66
|
def initialize(block)
|
55
67
|
@hash = {}
|
56
68
|
instance_eval(&block)
|
57
69
|
end
|
58
|
-
|
70
|
+
|
59
71
|
def method_missing(method, *args, &block)
|
60
72
|
key = method.to_s.gsub('_', '-')
|
61
73
|
if block
|
62
74
|
@hash[key] = HashDSL.new(block).hash
|
63
75
|
else
|
76
|
+
# Simulate method requiring 1 argument
|
77
|
+
raise ArgumentError, "wrong number of arguments(#{args.length} for 1)" unless args.length == 1
|
64
78
|
@hash[key] = args[0]
|
65
79
|
end
|
66
80
|
end
|
data/lib/configru/version.rb
CHANGED
data/lib/configru.rb
CHANGED
@@ -23,11 +23,11 @@ module Configru
|
|
23
23
|
end
|
24
24
|
self.reload
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
def self.reload
|
28
28
|
@config = ConfigHash.new(@defaults)
|
29
29
|
@loaded_files = []
|
30
|
-
|
30
|
+
|
31
31
|
case @load_method
|
32
32
|
when :first
|
33
33
|
if file = @files.find {|file| File.file?(file)} # Intended
|
@@ -42,30 +42,28 @@ module Configru
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
self.verify(@config, @verify)
|
47
47
|
end
|
48
|
-
|
48
|
+
|
49
49
|
@verify_stack = []
|
50
50
|
def self.verify(hash, criteria)
|
51
51
|
hash.each do |key, value|
|
52
52
|
next unless criteria[key]
|
53
53
|
@verify_stack.unshift(key)
|
54
|
-
|
54
|
+
|
55
55
|
result = case criteria[key]
|
56
56
|
when Hash
|
57
57
|
self.verify(value, criteria[key])
|
58
|
-
true
|
59
|
-
when Class
|
60
|
-
value.is_a?(criteria[key])
|
58
|
+
true # If it failed, an exception will have been raised
|
61
59
|
when Array
|
62
60
|
criteria[key].include?(value)
|
63
61
|
else
|
64
62
|
criteria[key] === value
|
65
63
|
end
|
66
|
-
|
64
|
+
|
67
65
|
raise ConfigurationError, "configuration option '#{@verify_stack.reverse.join('.')}' is invalid" unless result
|
68
|
-
|
66
|
+
|
69
67
|
@verify_stack.shift
|
70
68
|
end
|
71
69
|
end
|
@@ -73,11 +71,11 @@ module Configru
|
|
73
71
|
def self.loaded_files
|
74
72
|
@loaded_files
|
75
73
|
end
|
76
|
-
|
74
|
+
|
77
75
|
def self.[](key)
|
78
76
|
@config[key]
|
79
77
|
end
|
80
|
-
|
78
|
+
|
81
79
|
def self.method_missing(key, *args)
|
82
80
|
# Simulate NoMethodError if it looks like they really wanted a method
|
83
81
|
raise NoMethodError, "undefined method `#{key.to_s}' for #{self.inspect}:#{self.class}" unless args.empty?
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'teststrap'
|
2
|
+
require 'configru/dsl'
|
3
|
+
|
4
|
+
context 'LoadDSL - Files' do
|
5
|
+
setup do
|
6
|
+
block = proc do
|
7
|
+
cascade 'a', 'b', 'c'
|
8
|
+
end
|
9
|
+
Configru::DSL::LoadDSL.new(block).files_array
|
10
|
+
end
|
11
|
+
|
12
|
+
asserts_topic.equals(['a', 'b', 'c'])
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'LoadDSL - File-directories' do
|
16
|
+
setup do
|
17
|
+
block = proc do
|
18
|
+
cascade 'a', ['b', 'c']
|
19
|
+
end
|
20
|
+
Configru::DSL::LoadDSL.new(block).files_array
|
21
|
+
end
|
22
|
+
|
23
|
+
asserts_topic.equals([File.join('b', 'a'), File.join('c', 'a')])
|
24
|
+
end
|
metadata
CHANGED
@@ -1,33 +1,23 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: configru
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 3
|
8
|
-
- 0
|
9
|
-
version: 0.3.0
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.0
|
5
|
+
prerelease:
|
10
6
|
platform: ruby
|
11
|
-
authors:
|
7
|
+
authors:
|
12
8
|
- Curtis McEnroe
|
13
9
|
autorequire:
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
|
-
|
17
|
-
date: 2011-07-30 00:00:00 -04:00
|
18
|
-
default_executable:
|
12
|
+
date: 2012-01-08 00:00:00.000000000 Z
|
19
13
|
dependencies: []
|
20
|
-
|
21
14
|
description: Versatile configuration file loader
|
22
|
-
email:
|
15
|
+
email:
|
23
16
|
- programble@gmail.com
|
24
17
|
executables: []
|
25
|
-
|
26
18
|
extensions: []
|
27
|
-
|
28
19
|
extra_rdoc_files: []
|
29
|
-
|
30
|
-
files:
|
20
|
+
files:
|
31
21
|
- .gitignore
|
32
22
|
- ChangeLog.md
|
33
23
|
- Gemfile
|
@@ -40,40 +30,34 @@ files:
|
|
40
30
|
- lib/configru/version.rb
|
41
31
|
- test/confighash_test.rb
|
42
32
|
- test/hashdsl_test.rb
|
33
|
+
- test/loaddsl_test.rb
|
43
34
|
- test/teststrap.rb
|
44
|
-
has_rdoc: true
|
45
35
|
homepage: https://github.com/programble/configru
|
46
36
|
licenses: []
|
47
|
-
|
48
37
|
post_install_message:
|
49
38
|
rdoc_options: []
|
50
|
-
|
51
|
-
require_paths:
|
39
|
+
require_paths:
|
52
40
|
- lib
|
53
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
42
|
none: false
|
55
|
-
requirements:
|
56
|
-
- -
|
57
|
-
- !ruby/object:Gem::Version
|
58
|
-
|
59
|
-
|
60
|
-
version: "0"
|
61
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
48
|
none: false
|
63
|
-
requirements:
|
64
|
-
- -
|
65
|
-
- !ruby/object:Gem::Version
|
66
|
-
|
67
|
-
- 0
|
68
|
-
version: "0"
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
69
53
|
requirements: []
|
70
|
-
|
71
54
|
rubyforge_project:
|
72
|
-
rubygems_version: 1.
|
55
|
+
rubygems_version: 1.8.11
|
73
56
|
signing_key:
|
74
57
|
specification_version: 3
|
75
58
|
summary: Versatile configuration file loader
|
76
|
-
test_files:
|
59
|
+
test_files:
|
77
60
|
- test/confighash_test.rb
|
78
61
|
- test/hashdsl_test.rb
|
62
|
+
- test/loaddsl_test.rb
|
79
63
|
- test/teststrap.rb
|