configru 0.0.2 → 0.1.0
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.
- data/README.md +113 -20
- data/lib/configru/dsl.rb +3 -1
- data/lib/configru/version.rb +1 -1
- metadata +3 -3
data/README.md
CHANGED
@@ -2,40 +2,133 @@
|
|
2
2
|
|
3
3
|
Versatile configuration file loader for Ruby
|
4
4
|
|
5
|
-
##
|
5
|
+
## Installation
|
6
6
|
|
7
|
+
gem install configru
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
Configru loads YAML configuration files and provides a simple way to access
|
12
|
+
configuration options.
|
13
|
+
|
14
|
+
### Loading Configuration Files
|
15
|
+
|
16
|
+
Configru provides a DSL for loading configuration files.
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
require 'configru'
|
20
|
+
|
21
|
+
Configru.load do
|
22
|
+
# Things
|
23
|
+
end
|
24
|
+
```
|
25
|
+
|
26
|
+
At the very least, the block passed to `Configru.load` must tell Configru
|
27
|
+
which files it should load. There are two different methods of loading
|
28
|
+
configuration files available.
|
29
|
+
|
30
|
+
#### Just load a file already!
|
31
|
+
|
32
|
+
This is the simplest method of loading. It just loads a file, already!
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
Configru.load do
|
36
|
+
just 'foo.yml'
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
#### First-of Loading
|
41
|
+
|
42
|
+
This method loads the first file that exists, ignoring all other files.
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
Configru.load do
|
46
|
+
first_of 'foo.yml', '~/foo.yml', '/etc/foo.yml'
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
#### Cascading Loading
|
51
|
+
|
52
|
+
This method of loading loads each file given (if it exists) and cascades
|
53
|
+
their values. The values in the first given file have highest priority,
|
54
|
+
and the values in the last file have lowest priority.
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
Configru.load do
|
58
|
+
cascade '~/foo.yml', '/etc/foo.yml'
|
59
|
+
end
|
60
|
+
```
|
61
|
+
|
62
|
+
This will load `/etc/foo.yml` first, then `~/foo.yml`. The values in
|
63
|
+
`~/foo.yml` will overwrite the values in `/etc/foo.yml`. If a configuration
|
64
|
+
option is omitted in `~/foo.yml`, it will default to the value in
|
65
|
+
`/etc/foo.yml`.
|
66
|
+
|
67
|
+
### Accessing Options
|
68
|
+
|
69
|
+
Configru aims to make accessing configuration values as simple as possible.
|
70
|
+
All configuration options can be accessed as methods on the module
|
71
|
+
`Configru`.
|
72
|
+
|
73
|
+
##### foo.yml
|
7
74
|
```yaml
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
awesome: omgyes
|
75
|
+
nick: bob
|
76
|
+
server:
|
77
|
+
address: foo.net
|
78
|
+
port: 6782
|
13
79
|
```
|
14
80
|
|
81
|
+
##### foo.rb
|
15
82
|
```ruby
|
16
83
|
require 'configru'
|
84
|
+
require 'socket'
|
17
85
|
|
18
86
|
Configru.load do
|
19
|
-
|
87
|
+
first_of 'foo.yml', '~/foo.yml'
|
88
|
+
end
|
89
|
+
|
90
|
+
s = TCPSocket.new(Configru.server.address, Configru.server.port)
|
91
|
+
s.puts "Hello, I am #{Configru.nick}"
|
92
|
+
```
|
93
|
+
|
94
|
+
Configuration options can also be accessed the old-fashioned way like a
|
95
|
+
Hash. `Configru['server']['port']` is equivalent to `Configru.server.port`.
|
96
|
+
|
97
|
+
Configuration optiosn with hyphens (ie. `foo-bar`) can be accessed either
|
98
|
+
using the old-fashioned way (ie. `Configru['foo-bar']`), or by replacing
|
99
|
+
the hyphens with underscores for the method way (ie. `Configru.foo_bar`).
|
100
|
+
|
101
|
+
### Defaults
|
102
|
+
|
103
|
+
Configru's load DSL allows for setting configuration defaults.
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
require 'configru'
|
107
|
+
|
108
|
+
Configru.load do
|
109
|
+
first_of 'foo.yml', '~/foo.yml'
|
20
110
|
defaults do
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
end
|
27
|
-
verify do
|
28
|
-
server /\S+/ # Must match regex
|
29
|
-
nick /\S+/
|
30
|
-
port Fixnum # Must be an instance of
|
31
|
-
powerlevel 1..10 # Must be in range
|
32
|
-
awesome ['not at all', 'omgyes', 'meh'] # Must be one of
|
111
|
+
nick 'Dr. Nader'
|
112
|
+
server do
|
113
|
+
address 'abcd.com'
|
114
|
+
port 1111
|
115
|
+
end
|
33
116
|
end
|
34
117
|
end
|
118
|
+
```
|
35
119
|
|
36
|
-
|
120
|
+
The above `defaults` block is equivalent to the following YAML:
|
121
|
+
|
122
|
+
```yaml
|
123
|
+
nick: Dr. Nader
|
124
|
+
server:
|
125
|
+
address: abcd.com
|
126
|
+
port: 1111
|
37
127
|
```
|
38
128
|
|
129
|
+
If no configuration files are found or if the configuration file omits an
|
130
|
+
option, the values in `defaults` will be used.
|
131
|
+
|
39
132
|
## License
|
40
133
|
|
41
134
|
Copyright (c) 2011, Curtis McEnroe <programble@gmail.com>
|
data/lib/configru/dsl.rb
CHANGED
data/lib/configru/version.rb
CHANGED
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
+
- 1
|
7
8
|
- 0
|
8
|
-
|
9
|
-
version: 0.0.2
|
9
|
+
version: 0.1.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Curtis McEnroe
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-07-
|
17
|
+
date: 2011-07-17 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|