configurethis 1.0.4 → 1.0.5
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 +50 -2
- data/lib/configurethis.rb +1 -1
- data/lib/configurethis/mock_configuration.rb +3 -20
- data/lib/configurethis/value_container.rb +4 -0
- data/lib/configurethis/version.rb +1 -1
- data/spec/configurethis/configurethis_spec.rb +17 -2
- data/spec/configurethis/support/classes.rb +1 -0
- data/spec/configurethis/support/classes/keys_config.rb +3 -0
- data/spec/configurethis/support/config/keys_config.yml +15 -0
- metadata +17 -9
- checksums.yaml +0 -7
data/README.md
CHANGED
@@ -4,6 +4,28 @@
|
|
4
4
|
Clean up your configuration approach by using Configurethis. Configurethis allows you to access your config values using
|
5
5
|
method names instead of string literals to identify which config value you want to retrieve.
|
6
6
|
|
7
|
+
## Quick example
|
8
|
+
```ruby
|
9
|
+
# lib/password_settings.rb
|
10
|
+
class PasswordSettings
|
11
|
+
extend Configurethis
|
12
|
+
end
|
13
|
+
|
14
|
+
# some_ruby_file_in_my_app.rb
|
15
|
+
PasswordSettings.min_length #=> 10
|
16
|
+
PasswordSettings.require_uppercase #=> true
|
17
|
+
PasswordSettings.storage.keep_last #=> 3
|
18
|
+
```
|
19
|
+
```yml
|
20
|
+
---
|
21
|
+
# config/password_settings.yml
|
22
|
+
minLength: 10
|
23
|
+
require_uppercase: true
|
24
|
+
storage:
|
25
|
+
keep_last: 3
|
26
|
+
```
|
27
|
+
|
28
|
+
## Why?
|
7
29
|
Typical Ruby code has craziness like this all over the place:
|
8
30
|
```ruby
|
9
31
|
# config/initializers/load_config.rb
|
@@ -42,7 +64,7 @@ storage:
|
|
42
64
|
keep_last: 3
|
43
65
|
```
|
44
66
|
|
45
|
-
##
|
67
|
+
## Get started with Configurethis!
|
46
68
|
|
47
69
|
Add this line to your application's Gemfile:
|
48
70
|
|
@@ -56,7 +78,7 @@ Or install it yourself as:
|
|
56
78
|
|
57
79
|
$ gem install configurethis
|
58
80
|
|
59
|
-
##
|
81
|
+
## How to?
|
60
82
|
|
61
83
|
### Setup
|
62
84
|
To get started, specify where you want `Configurethis` to look for your .yml files at.
|
@@ -108,6 +130,32 @@ class IWantToBeDifferent
|
|
108
130
|
end
|
109
131
|
```
|
110
132
|
|
133
|
+
### Inspecting keys for a hash value
|
134
|
+
Sometimes you may want to iterate over the keys of a configured hash. You can do this by calling `#keys` on the hash to retrieve a list of the keys.
|
135
|
+
For example, if your YAML is represented as:
|
136
|
+
```yml
|
137
|
+
---
|
138
|
+
bourbon:
|
139
|
+
distillery:
|
140
|
+
woodford:
|
141
|
+
# more data on woodford
|
142
|
+
buffalo_trace:
|
143
|
+
# more stuff
|
144
|
+
makers_mark:
|
145
|
+
# makers stuff
|
146
|
+
```
|
147
|
+
And let's assume our configuration class is called `BourbonConfig`, we could retrieve a list of distilleries using `#keys`:
|
148
|
+
```ruby
|
149
|
+
BourbonConfig.bourbon.distillery.keys #=> ["woodford", "buffalo_trace", "makers_mark"]
|
150
|
+
```
|
151
|
+
|
152
|
+
### Validation
|
153
|
+
If you are having problems and everything "looks clear", try running your YAML through a [YAML lint test](http://yamllint.com).
|
154
|
+
Ruby has been known to be forgiving to poorly formatted YAML in some versions, but not others.
|
155
|
+
|
156
|
+
### Running the tests
|
157
|
+
Just run rspec from your prompt! The tests are also contain great examples about how to use Configurethis.
|
158
|
+
|
111
159
|
### Testing
|
112
160
|
While working with Configurethis in your specs/tests you can override the configuration
|
113
161
|
and avoid using the real configuration files to simulate behavior. This enables you to
|
data/lib/configurethis.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require "configurethis/version"
|
2
2
|
require "configurethis/value_container"
|
3
|
-
require "configurethis/mock_configuration"
|
4
3
|
require "configurethis/configuration"
|
4
|
+
require "configurethis/mock_configuration"
|
5
5
|
require "configurethis/configurethis_properties"
|
6
6
|
|
7
7
|
module Configurethis
|
@@ -1,31 +1,14 @@
|
|
1
|
-
|
2
|
-
class MockConfiguration
|
3
|
-
attr_reader :path
|
1
|
+
require "configurethis/configuration"
|
4
2
|
|
3
|
+
module Configurethis
|
4
|
+
class MockConfiguration < Configuration
|
5
5
|
def initialize(values)
|
6
6
|
@path = "<mocked_configuration>"
|
7
7
|
@mock_values = values
|
8
8
|
end
|
9
9
|
|
10
|
-
def root=(key)
|
11
|
-
@values = load_configuration.fetch(key)
|
12
|
-
rescue ::IndexError
|
13
|
-
raise "'#{key}' is not configured in #{path}"
|
14
|
-
end
|
15
|
-
|
16
|
-
def [](key)
|
17
|
-
@values ||= load_configuration
|
18
|
-
val = @values.fetch(key)
|
19
|
-
return ValueContainer.new(val, path) if val.is_a?(Hash)
|
20
|
-
val
|
21
|
-
rescue ::IndexError
|
22
|
-
raise "'#{key}' is not configured in #{path}"
|
23
|
-
end
|
24
|
-
|
25
10
|
def load_configuration
|
26
11
|
@mock_values
|
27
12
|
end
|
28
|
-
|
29
|
-
|
30
13
|
end
|
31
14
|
end
|
@@ -69,13 +69,28 @@ describe Configurethis do
|
|
69
69
|
Then { expect( config.alpha ).to eql( "a" ) }
|
70
70
|
end
|
71
71
|
end
|
72
|
-
|
73
|
-
|
74
72
|
end
|
75
73
|
|
76
74
|
describe "using configured values" do
|
77
75
|
Given { Configurethis.root_path = File.join(File.dirname(__FILE__), 'support/config') }
|
78
76
|
|
77
|
+
describe "and inspecting configuration keys" do
|
78
|
+
after(:each) { KeysConfig.reload_configuration }
|
79
|
+
Given (:config) { KeysConfig }
|
80
|
+
|
81
|
+
context "when value is a hash" do
|
82
|
+
Then { expect( config.beer.brewery.keys ).to match_array(["stone", "victory"]) }
|
83
|
+
end
|
84
|
+
|
85
|
+
context "when value is an array" do
|
86
|
+
Then { expect{ config.beer.brewery.stone.keys }.to raise_error(NoMethodError) }
|
87
|
+
end
|
88
|
+
|
89
|
+
context "when value is an unassigned value" do
|
90
|
+
Then { expect{ config.bourbon.distillery.woodford.keys }.to raise_error(NoMethodError) }
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
79
94
|
context "when the classes config file does not exist" do
|
80
95
|
Given (:config) { MissingConfiguration }
|
81
96
|
Then { expect{ config.some_value }.to raise_error(RuntimeError, "Could not locate configuration file: #{config.configuration_path}") }
|
metadata
CHANGED
@@ -1,27 +1,30 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: configurethis
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
7
|
authors:
|
7
8
|
- Matt Snyder
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date: 2013-08-
|
12
|
+
date: 2013-08-14 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: rspec-given
|
15
16
|
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
16
18
|
requirements:
|
17
|
-
- - '>='
|
19
|
+
- - ! '>='
|
18
20
|
- !ruby/object:Gem::Version
|
19
21
|
version: 3.0.0
|
20
22
|
type: :development
|
21
23
|
prerelease: false
|
22
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
23
26
|
requirements:
|
24
|
-
- - '>='
|
27
|
+
- - ! '>='
|
25
28
|
- !ruby/object:Gem::Version
|
26
29
|
version: 3.0.0
|
27
30
|
description: Clean up your configuration approach by using Configurethis. Configurethis
|
@@ -50,6 +53,7 @@ files:
|
|
50
53
|
- spec/configurethis/configurethis_spec.rb
|
51
54
|
- spec/configurethis/support/classes.rb
|
52
55
|
- spec/configurethis/support/classes/conventional_path.rb
|
56
|
+
- spec/configurethis/support/classes/keys_config.rb
|
53
57
|
- spec/configurethis/support/classes/missing_configuration.rb
|
54
58
|
- spec/configurethis/support/classes/my_custom_conf.rb
|
55
59
|
- spec/configurethis/support/classes/namespaced_conventional_path.rb
|
@@ -57,6 +61,7 @@ files:
|
|
57
61
|
- spec/configurethis/support/classes/override_path.rb
|
58
62
|
- spec/configurethis/support/classes/rails_app_config.rb
|
59
63
|
- spec/configurethis/support/classes/riak_config.rb
|
64
|
+
- spec/configurethis/support/config/keys_config.yml
|
60
65
|
- spec/configurethis/support/config/nested_config.yml
|
61
66
|
- spec/configurethis/support/config/rails_app_config.yml
|
62
67
|
- spec/configurethis/support/config/riak_config.yml
|
@@ -65,26 +70,27 @@ homepage: https://github.com/mattsnyder/configurethis
|
|
65
70
|
licenses:
|
66
71
|
- MIT
|
67
72
|
- GPL-2
|
68
|
-
metadata: {}
|
69
73
|
post_install_message:
|
70
74
|
rdoc_options: []
|
71
75
|
require_paths:
|
72
76
|
- lib
|
73
77
|
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
74
79
|
requirements:
|
75
|
-
- - '>='
|
80
|
+
- - ! '>='
|
76
81
|
- !ruby/object:Gem::Version
|
77
82
|
version: '0'
|
78
83
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
79
85
|
requirements:
|
80
|
-
- - '>='
|
86
|
+
- - ! '>='
|
81
87
|
- !ruby/object:Gem::Version
|
82
88
|
version: '0'
|
83
89
|
requirements: []
|
84
90
|
rubyforge_project:
|
85
|
-
rubygems_version:
|
91
|
+
rubygems_version: 1.8.25
|
86
92
|
signing_key:
|
87
|
-
specification_version:
|
93
|
+
specification_version: 3
|
88
94
|
summary: Clean up your configuration approach by using Configurethis. Configurethis
|
89
95
|
allows you to access your config values using method names instead of string literals
|
90
96
|
to identify which config value you want to retrieve.
|
@@ -92,6 +98,7 @@ test_files:
|
|
92
98
|
- spec/configurethis/configurethis_spec.rb
|
93
99
|
- spec/configurethis/support/classes.rb
|
94
100
|
- spec/configurethis/support/classes/conventional_path.rb
|
101
|
+
- spec/configurethis/support/classes/keys_config.rb
|
95
102
|
- spec/configurethis/support/classes/missing_configuration.rb
|
96
103
|
- spec/configurethis/support/classes/my_custom_conf.rb
|
97
104
|
- spec/configurethis/support/classes/namespaced_conventional_path.rb
|
@@ -99,6 +106,7 @@ test_files:
|
|
99
106
|
- spec/configurethis/support/classes/override_path.rb
|
100
107
|
- spec/configurethis/support/classes/rails_app_config.rb
|
101
108
|
- spec/configurethis/support/classes/riak_config.rb
|
109
|
+
- spec/configurethis/support/config/keys_config.yml
|
102
110
|
- spec/configurethis/support/config/nested_config.yml
|
103
111
|
- spec/configurethis/support/config/rails_app_config.yml
|
104
112
|
- spec/configurethis/support/config/riak_config.yml
|
checksums.yaml
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
---
|
2
|
-
SHA1:
|
3
|
-
metadata.gz: de51c7bf8ff307aa213f4f6431be1fdd79beb4c4
|
4
|
-
data.tar.gz: c12f30afd2ac5c311cb13d9974f1d1a3b2c28868
|
5
|
-
SHA512:
|
6
|
-
metadata.gz: ea2e4efb4bc08bccd90d4e529a3118731273df9599bea18f4a772b4412499f1dd15b0af26d794de8865f4385982c1b1d14ebc6f88217ce31a07c1ea3fdeb10cf
|
7
|
-
data.tar.gz: ae2b9a342b327bfd077e013ef825c36eba9d20f597123ac8155a5885c3d29ac812cf97f350efcb2d25952ddfc5e90b4b5f224b77ef1224b1c866427cbbe0a48c
|