configru 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +37 -27
- data/lib/configru/dsl.rb +5 -3
- data/lib/configru/version.rb +1 -1
- metadata +3 -3
data/README.md
CHANGED
@@ -23,13 +23,13 @@ Configru.load do
|
|
23
23
|
end
|
24
24
|
```
|
25
25
|
|
26
|
-
At the very least, the block passed to `Configru.load` must tell Configru
|
27
|
-
|
28
|
-
|
26
|
+
At the very least, the block passed to `Configru.load` must tell Configru which
|
27
|
+
files it should load. There are two different methods of loading configuration
|
28
|
+
files available.
|
29
29
|
|
30
30
|
#### Just load a file already!
|
31
31
|
|
32
|
-
This is the simplest method of loading. It just loads a file
|
32
|
+
This is the simplest method of loading. It just loads a file.
|
33
33
|
|
34
34
|
```ruby
|
35
35
|
Configru.load do
|
@@ -49,9 +49,9 @@ end
|
|
49
49
|
|
50
50
|
#### Cascading Loading
|
51
51
|
|
52
|
-
This method
|
53
|
-
|
54
|
-
|
52
|
+
This method loads every file that exists in reverse order. Files listed first
|
53
|
+
overwrite the values from files listed later. (Files are listed in high to low
|
54
|
+
cascade priority)
|
55
55
|
|
56
56
|
```ruby
|
57
57
|
Configru.load do
|
@@ -59,16 +59,10 @@ Configru.load do
|
|
59
59
|
end
|
60
60
|
```
|
61
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
62
|
### Accessing Options
|
68
63
|
|
69
|
-
|
70
|
-
|
71
|
-
`Configru`.
|
64
|
+
Configuration options can be accessed as methods of the `Configru` module, or
|
65
|
+
`Configru` can be used as a Hash.
|
72
66
|
|
73
67
|
##### foo.yml
|
74
68
|
```yaml
|
@@ -84,29 +78,28 @@ require 'configru'
|
|
84
78
|
require 'socket'
|
85
79
|
|
86
80
|
Configru.load do
|
87
|
-
|
81
|
+
just 'foo.yml'
|
88
82
|
end
|
89
83
|
|
90
|
-
s = TCPSocket.new(Configru.server.address, Configru
|
84
|
+
s = TCPSocket.new(Configru.server.address, Configru['server']['port'])
|
91
85
|
s.puts "Hello, I am #{Configru.nick}"
|
92
86
|
```
|
93
87
|
|
94
|
-
Configuration options can
|
95
|
-
|
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`).
|
88
|
+
Configuration options with hyphens can be accessed as methods by replacing the
|
89
|
+
hyphens with underscores.
|
100
90
|
|
101
91
|
### Defaults
|
102
92
|
|
103
|
-
Configru's load DSL allows for setting configuration defaults.
|
93
|
+
Configru's load DSL allows for setting configuration defaults using a block.
|
94
|
+
If no configuration files are found or if the configuration file omits an
|
95
|
+
option, the values in `defaults` will be used.
|
96
|
+
|
104
97
|
|
105
98
|
```ruby
|
106
99
|
require 'configru'
|
107
100
|
|
108
101
|
Configru.load do
|
109
|
-
|
102
|
+
just 'foo.yml'
|
110
103
|
defaults do
|
111
104
|
nick 'Dr. Nader'
|
112
105
|
server do
|
@@ -126,8 +119,25 @@ server:
|
|
126
119
|
port: 1111
|
127
120
|
```
|
128
121
|
|
129
|
-
|
130
|
-
|
122
|
+
Defaults can also be set using a Hash instead of a block.
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
Configru.load do
|
126
|
+
just 'foo.yml'
|
127
|
+
defaults 'nick' => 'Dr. Nader',
|
128
|
+
'server' => {'address' => 'abcd.com', 'port' => 1111}
|
129
|
+
end
|
130
|
+
```
|
131
|
+
|
132
|
+
Defaults can also be loaded from a YAML file by passing the filename to
|
133
|
+
`default`.
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
Configru.load do
|
137
|
+
just 'foo.yml'
|
138
|
+
defaults 'foo.yml.dist'
|
139
|
+
end
|
140
|
+
```
|
131
141
|
|
132
142
|
## License
|
133
143
|
|
data/lib/configru/dsl.rb
CHANGED
@@ -23,9 +23,11 @@ module Configru
|
|
23
23
|
@files_array = args
|
24
24
|
end
|
25
25
|
|
26
|
-
def defaults(
|
27
|
-
if
|
28
|
-
@defaults_hash =
|
26
|
+
def defaults(arg=nil, &block)
|
27
|
+
if arg.is_a? String
|
28
|
+
@defaults_hash = YAML.load_file(arg)
|
29
|
+
elsif arg
|
30
|
+
@defaults_hash = arg
|
29
31
|
elsif block
|
30
32
|
@defaults_hash = HashDSL.new(block).hash
|
31
33
|
end
|
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
|
-
-
|
7
|
+
- 2
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.2.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-19 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|