config_module 0.0.1 → 0.0.2
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.markdown +112 -3
- data/lib/config_module/version.rb +1 -1
- data/lib/config_module.rb +4 -2
- data/test/config/false_nil.yml +3 -0
- data/test/test.rb +22 -1
- metadata +4 -2
data/README.markdown
CHANGED
@@ -3,10 +3,105 @@ ConfigModule
|
|
3
3
|
|
4
4
|
Load important configuration files into their own modules!
|
5
5
|
|
6
|
+
Installation
|
7
|
+
------------
|
8
|
+
|
9
|
+
Install the [gem](http://rubygems.org/gems/config_module), preferably using [Bundler](http://gembundler.com/):
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'config_module' # in your Gemfile
|
13
|
+
```
|
14
|
+
|
15
|
+
```bash
|
16
|
+
bundle install # on the command line
|
17
|
+
```
|
18
|
+
|
19
|
+
You may need to tell Ruby that you want to use it (depending how you're using Bundler):
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
require 'config_module' # in your file
|
23
|
+
```
|
24
|
+
|
25
|
+
Setup
|
26
|
+
-----
|
27
|
+
|
28
|
+
You only need to add two lines inside any module definition to make it a ConfigModule.
|
29
|
+
|
30
|
+
1. Add the ConfigModule functionality to your module:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
extend ConfigModule
|
34
|
+
```
|
35
|
+
|
36
|
+
2. Specify the name of your configuration file:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
config_file './some_config.yml'
|
40
|
+
```
|
41
|
+
|
42
|
+
Done!
|
43
|
+
|
44
|
+
You're set up, and you can add any other functionality, aliases, or derived values to your module
|
45
|
+
like any other Ruby module.
|
46
|
+
|
6
47
|
Usage
|
7
48
|
-----
|
8
49
|
|
9
|
-
|
50
|
+
Now give it a try, any [valid](https://github.com/acook/config_module/edit/master/README.markdown#caveats)
|
51
|
+
key in your configuration file will now be a method:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
SomeConfig.my_key
|
55
|
+
```
|
56
|
+
|
57
|
+
You can even chain them! Try it:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
SomeConfig.my_key.my_subkey
|
61
|
+
```
|
62
|
+
|
63
|
+
How cool is that?
|
64
|
+
|
65
|
+
Extras
|
66
|
+
------
|
67
|
+
|
68
|
+
In addition to the basics, ConfigModule also supplies a couple of helpers you might find useful.
|
69
|
+
|
70
|
+
1. You can also set the "namespace" you want to use, this is great for apps with multiple environments:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
namespace ENV['my_environment']
|
74
|
+
```
|
75
|
+
|
76
|
+
This will set the root of the tree to whichever branch you specify, so you don't have to.
|
77
|
+
|
78
|
+
2. There's also a new method available in your module that points directly to the raw configuration data:
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
config
|
82
|
+
```
|
83
|
+
|
84
|
+
Don't overwrite this method!
|
85
|
+
|
86
|
+
3. You can still access raw data from outside the module too, if you want:
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
MyConfig[:some_key].is_a? Hash #=> true
|
90
|
+
```
|
91
|
+
|
92
|
+
Example
|
93
|
+
-------
|
94
|
+
|
95
|
+
Given a YAML file `./config/example.yml':
|
96
|
+
|
97
|
+
```yaml
|
98
|
+
---
|
99
|
+
:production:
|
100
|
+
:foo: bar
|
101
|
+
:noodle: boom!
|
102
|
+
```
|
103
|
+
|
104
|
+
And you set up your module:
|
10
105
|
|
11
106
|
```ruby
|
12
107
|
require 'config_module'
|
@@ -25,11 +120,25 @@ module ExampleConfig
|
|
25
120
|
end
|
26
121
|
```
|
27
122
|
|
28
|
-
Then use it:
|
123
|
+
Then you can use it like this:
|
29
124
|
|
30
125
|
```ruby
|
31
126
|
ExampleConfig.foo #=> 'bar'
|
32
127
|
ExampleConfig.kanoodle #=> 'kaboom!'
|
33
128
|
```
|
34
129
|
|
35
|
-
|
130
|
+
Pretty nifty, huh?
|
131
|
+
|
132
|
+
Caveats
|
133
|
+
-------
|
134
|
+
|
135
|
+
- **Q:** You mention "valid key". What's a valid key?
|
136
|
+
- **A:** It's any object that you can call `.to_sym` on!
|
137
|
+
|
138
|
+
Who made this anyway?
|
139
|
+
---------------------
|
140
|
+
|
141
|
+
I'm glad you asked!
|
142
|
+
|
143
|
+
Anthony M. Cook 2013
|
144
|
+
|
data/lib/config_module.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
require_relative 'config_module/version'
|
2
2
|
require 'ostruct'
|
3
3
|
require 'yaml'
|
4
4
|
|
@@ -35,7 +35,9 @@ protected
|
|
35
35
|
end
|
36
36
|
|
37
37
|
def method_missing name, *args, &block
|
38
|
-
wrap
|
38
|
+
wrap config.send name, *args, &block
|
39
|
+
rescue
|
40
|
+
raise NoMethodError, "undefined method `#{name}' for #{self}", caller(1)
|
39
41
|
end
|
40
42
|
|
41
43
|
class ConfigOption < OpenStruct
|
data/test/test.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require_relative 'mutest'
|
2
2
|
extend Mutest
|
3
3
|
|
4
|
-
require_relative '../config_module'
|
4
|
+
require_relative '../lib/config_module'
|
5
5
|
module Rails; def self.env; 'production'; end; end
|
6
6
|
require_relative 'example_config'
|
7
7
|
|
@@ -18,3 +18,24 @@ end
|
|
18
18
|
spec 'nested hash values are properly wrapped' do
|
19
19
|
ExampleConfig.dictionary.class == ConfigModule::ConfigOption
|
20
20
|
end
|
21
|
+
|
22
|
+
spec 'config modules have [] methods' do
|
23
|
+
ExampleConfig[:dictionary].keys.include? :configuration
|
24
|
+
end
|
25
|
+
|
26
|
+
spec 'subkeys are accessible with methods' do
|
27
|
+
ExampleConfig.dictionary.configuration == 'An arrangement of elements in a particular form, figure, or combination.'
|
28
|
+
end
|
29
|
+
|
30
|
+
module FalseNil
|
31
|
+
extend ConfigModule
|
32
|
+
config_file './config/false_nil.yml'
|
33
|
+
end
|
34
|
+
|
35
|
+
spec 'false values are returned' do
|
36
|
+
FalseNil.f == false
|
37
|
+
end
|
38
|
+
|
39
|
+
spec 'nil values are preserved' do
|
40
|
+
FalseNil.n == nil
|
41
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: config_module
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-02-
|
12
|
+
date: 2013-02-15 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Wrap a configuration file in a module for easy use throughout your application.
|
15
15
|
Inspired by Rails.
|
@@ -27,6 +27,7 @@ files:
|
|
27
27
|
- lib/config_module.rb
|
28
28
|
- lib/config_module/version.rb
|
29
29
|
- test/config/example.yml
|
30
|
+
- test/config/false_nil.yml
|
30
31
|
- test/example_config.rb
|
31
32
|
- test/mutest.rb
|
32
33
|
- test/test.rb
|
@@ -56,6 +57,7 @@ specification_version: 3
|
|
56
57
|
summary: Load important configuration files into their own modules!
|
57
58
|
test_files:
|
58
59
|
- test/config/example.yml
|
60
|
+
- test/config/false_nil.yml
|
59
61
|
- test/example_config.rb
|
60
62
|
- test/mutest.rb
|
61
63
|
- test/test.rb
|