confstruct 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +58 -17
- data/lib/confstruct.rb +1 -1
- data/lib/confstruct/configuration.rb +1 -1
- data/spec/confstruct/configuration_spec.rb +10 -0
- metadata +5 -5
data/README.md
CHANGED
@@ -5,6 +5,7 @@ hash, struct, or block, confstruct aims to provide the flexibility to do things
|
|
5
5
|
way, while keeping things simple and intuitive.
|
6
6
|
|
7
7
|
[![Build Status](https://secure.travis-ci.org/mbklein/confstruct.png)](http://travis-ci.org/mbklein/confstruct)
|
8
|
+
[![Dependency Status](https://gemnasium.com/mbklein/confstruct.png)](https://gemnasium.com/mbklein/confstruct)
|
8
9
|
|
9
10
|
## Usage
|
10
11
|
|
@@ -74,7 +75,7 @@ You can even
|
|
74
75
|
config.project = 'other-project'
|
75
76
|
config.github = { :url => 'http://www.github.com/somefork/other-project', :branch => 'pre-1.0' }
|
76
77
|
|
77
|
-
The configure method will even a deep merge for you if you pass it a hash or hash-like object
|
78
|
+
The configure method will even perform a deep merge for you if you pass it a hash or hash-like object
|
78
79
|
(anything that responds to `each_pair`)
|
79
80
|
|
80
81
|
config.configure({:project => 'other-project', :github => {:url => 'http://www.github.com/somefork/other-project', :branch => 'pre-1.0'}})
|
@@ -98,28 +99,32 @@ hash or a struct:
|
|
98
99
|
config[:github]
|
99
100
|
=> {:url=>"http://www.github.com/somefork/other-project", :branch=>"pre-1.0"}
|
100
101
|
|
101
|
-
###
|
102
|
+
### Other Features
|
103
|
+
|
104
|
+
#### Deferred evaluation
|
102
105
|
|
103
106
|
Any configuration value of class `Confstruct::Deferred` will be evaluated on access, allowing you to
|
104
107
|
define read-only, dynamic configuration attributes
|
105
108
|
|
106
|
-
config
|
107
|
-
|
108
|
-
config.
|
109
|
-
=>
|
110
|
-
config.
|
111
|
-
=> "
|
112
|
-
config.
|
113
|
-
=>
|
109
|
+
config.app_name = "iWidgetCloud"
|
110
|
+
config.msgs.welcome = Confstruct::Deferred.new {|c| "Welcome to #{c.app_name}!"}
|
111
|
+
config.msgs.welcome
|
112
|
+
=> "Welcome to iWidgetCloud!"
|
113
|
+
config.app_name = "Enterprisey-Webscale"
|
114
|
+
=> "Enterprisey-Webscale"
|
115
|
+
config.welcome_msg
|
116
|
+
=> "Welcome to Enterprisey-Webscale"
|
114
117
|
|
115
118
|
As a convenience, `Confstruct.deferred(&block)` and `Confstruct::HashWithStructAccess#deferred!(&block)`
|
116
|
-
|
119
|
+
will create a Confstruct::Deferred for you, making the following two assignments equivalent to the above:
|
117
120
|
|
118
|
-
config.
|
119
|
-
config
|
120
|
-
|
121
|
+
config.welcome_msg = Confstruct.deferred { |c| "Welcome to #{c.app_name}!" }
|
122
|
+
config do
|
123
|
+
welcome_msg deferred! { |c| RestClient::Resource.new(c.url) }
|
121
124
|
end
|
122
125
|
|
126
|
+
#### Push/Pop configurations
|
127
|
+
|
123
128
|
`push!` and `pop!` methods allow you to temporarily override some or all of your configuration values. This can be
|
124
129
|
useful in spec tests where you need to change values but don't want to worry about messing up tests that depend
|
125
130
|
on the same global configuration object.
|
@@ -134,9 +139,45 @@ on the same global configuration object.
|
|
134
139
|
=> {:project=>"confstruct", :github=>{:branch=>"master", :url=>"http://www.github.com/mbklein/confstruct"}}
|
135
140
|
config.github.url
|
136
141
|
=> "http://www.github.com/mbklein/confstruct"
|
142
|
+
|
143
|
+
#### lookup!
|
144
|
+
|
145
|
+
`lookup!` can be used to look up down a hieararchy without raising on missing values; and/or
|
146
|
+
to look up with default value.
|
147
|
+
|
148
|
+
config = Confstruct::Configuration.new do
|
149
|
+
project 'confstruct'
|
150
|
+
github do
|
151
|
+
url 'http://www.github.com/mbklein/confstruct'
|
152
|
+
branch 'master'
|
153
|
+
end
|
154
|
+
end
|
155
|
+
config.lookup!("github.url")
|
156
|
+
=> "http://www.github.com/mbklein/confstruct"
|
157
|
+
config.lookup!("github.no_key")
|
158
|
+
=> nil # no raising
|
159
|
+
config.lookup!("not_there.really.not.there")
|
160
|
+
=> nil
|
161
|
+
config.lookup!("github.not_there", "default_value")
|
162
|
+
=> "default_value"
|
163
|
+
|
164
|
+
#### lists
|
165
|
+
|
166
|
+
The pattern `add_$key!` can be used to add to or create an array.
|
167
|
+
|
168
|
+
config = Confstruct::Configuration.new
|
169
|
+
config.add_color! "green"
|
170
|
+
=> ["green"]
|
171
|
+
config
|
172
|
+
=> {:color=>["green"]}
|
173
|
+
config.add_color! "red"
|
174
|
+
config.color
|
175
|
+
=> ["green", "red"]
|
176
|
+
|
137
177
|
|
138
178
|
### Notes
|
139
179
|
|
180
|
+
|
140
181
|
* Confstruct will attempt to use ordered hashes internally when available.
|
141
182
|
* In Ruby 1.9 and above, this is automatic.
|
142
183
|
* In Rubies earlier than 1.9, Confstruct will try to require and use ActiveSupport::OrderedHash,
|
@@ -150,6 +191,7 @@ on the same global configuration object.
|
|
150
191
|
|
151
192
|
- <b>v0.1.0</b> - Initial release
|
152
193
|
- <b>v0.2.0</b> - Add fallback value to HashWithStructAccess#lookup!, native support for Rails I18n.
|
194
|
+
- <b>v0.2.1</b> - Initialize properly from a nested hash with string (non-symbol) keys
|
153
195
|
|
154
196
|
## Contributing to confstruct
|
155
197
|
|
@@ -161,7 +203,6 @@ on the same global configuration object.
|
|
161
203
|
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
162
204
|
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
163
205
|
|
164
|
-
##
|
165
|
-
|
166
|
-
Copyright (c) 2011 Michael B. Klein. See LICENSE.txt for further details.
|
206
|
+
## License
|
167
207
|
|
208
|
+
confstruct is released under the [MIT License](http://www.opensource.org/licenses/MIT).
|
data/lib/confstruct.rb
CHANGED
@@ -6,7 +6,7 @@ module Confstruct
|
|
6
6
|
|
7
7
|
def initialize hash=@@hash_class.new, &block
|
8
8
|
super({})
|
9
|
-
@default_values = hash.is_a?(HashWithStructAccess) ? hash : HashWithStructAccess.
|
9
|
+
@default_values = hash.is_a?(HashWithStructAccess) ? hash : HashWithStructAccess.from_hash(hash)
|
10
10
|
eval_or_yield @default_values, &block
|
11
11
|
reset_defaults!
|
12
12
|
end
|
@@ -8,6 +8,16 @@ describe Confstruct::Configuration do
|
|
8
8
|
conf.is_a?(Confstruct::Configuration).should be_true
|
9
9
|
conf.should == {}
|
10
10
|
end
|
11
|
+
|
12
|
+
it "should initialize properly from a nested hash with string keys" do
|
13
|
+
x = { 'a' => { 'b' => 'c' } }
|
14
|
+
conf = Confstruct::Configuration.new(x)
|
15
|
+
conf.is_a?(Hash).should be_true
|
16
|
+
conf.is_a?(Confstruct::Configuration).should be_true
|
17
|
+
conf[:a][:b].should == 'c'
|
18
|
+
conf['a']['b'].should == 'c'
|
19
|
+
conf.a.b.should == 'c'
|
20
|
+
end
|
11
21
|
|
12
22
|
context "default values" do
|
13
23
|
before :all do
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: confstruct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 1
|
10
|
+
version: 0.2.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Michael Klein
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2012-01-23 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rake
|
@@ -157,7 +157,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
157
157
|
requirements: []
|
158
158
|
|
159
159
|
rubyforge_project:
|
160
|
-
rubygems_version: 1.8.
|
160
|
+
rubygems_version: 1.8.15
|
161
161
|
signing_key:
|
162
162
|
specification_version: 3
|
163
163
|
summary: A simple, hash/struct-based configuration object
|