config_module 1.0.0 → 1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a93ee42bdd50f65f7ae6b14b8b76cb7ea3dfe3a7
4
- data.tar.gz: 0052b145d6b42b8d7e6662a9d196e22907e94568
3
+ metadata.gz: d6792656b2d9b4c12032dbdc18f8cd23616d7e30
4
+ data.tar.gz: 0783bade48f7706ed7c98250955c0fb2e2f6f521
5
5
  SHA512:
6
- metadata.gz: 0f347735ec0e70695643960023c16128f58ea943992ef3c5f71af2daf96df5854b221f1448d635f8544cde4b4555c061e8b70844d864f90066b7ad1e49b25a92
7
- data.tar.gz: 5535e018b88f7112b83e3015bfe7f2b58d02e76cfad063d4f0309ce963756919eb72baf8f5b45bb7c5150765175475378b1297e893b4b2a1e4ccf96baada5cdc
6
+ metadata.gz: d0898b4cf1494a0691235b4620bd76c2a829a1c8e268ac1804f02bbdb8b438cc4cb7278eb29ca0b6c0260c7306f10463957af74b22e4aa518f7e645866653da1
7
+ data.tar.gz: d44917bb9a1a70b59d03453ced028c8e7d05b64d7ce1173336c14dfacc040e18f0d5f4c432fdffcade9933dc0bf5530fddc4cb172340de872084599c83fa83c6
data/.travis.yml CHANGED
@@ -1,9 +1,10 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 2.0.0
3
4
  - 1.9.3
4
5
  - 1.9.2
5
- - rbx-19mode
6
6
  - ruby-head
7
+ - rbx-19mode
7
8
  script: bundle exec ruby spec/run_all_specs.rb
8
9
  before_install:
9
10
  - gem update bundler
data/README.markdown CHANGED
@@ -3,6 +3,9 @@ ConfigModule
3
3
 
4
4
  Load important configuration files into their own modules!
5
5
 
6
+ Reference documentation for the [Latest Released](http://rubydoc.info/gems/config_module/file/README.markdown) and [Edge Version](https://github.com/acook/config_module#readme) is available.
7
+
8
+ [![Build Status](https://travis-ci.org/acook/config_module.png?branch=master)](https://travis-ci.org/acook/config_module)
6
9
  [![Code Climate](https://codeclimate.com/github/acook/config_module.png)](https://codeclimate.com/github/acook/config_module)
7
10
  [![Dependency Status](https://gemnasium.com/acook/config_module.png)](https://gemnasium.com/acook/config_module)
8
11
 
@@ -42,7 +45,7 @@ You only need to add two lines inside any module definition to make it a ConfigM
42
45
  config_file './some_config.yml'
43
46
  ```
44
47
 
45
- Done!
48
+ Done!
46
49
 
47
50
  You're set up, and you can add any other functionality, aliases, or derived values to your module
48
51
  like any other Ruby module.
@@ -50,7 +53,7 @@ like any other Ruby module.
50
53
  Usage
51
54
  -----
52
55
 
53
- Now give it a try, any [valid](https://github.com/acook/config_module/edit/master/README.markdown#caveats)
56
+ Now give it a try, any [valid](#caveats)
54
57
  key in your configuration file will now be a method:
55
58
 
56
59
  ```ruby
@@ -70,26 +73,80 @@ Extras
70
73
 
71
74
  In addition to the basics, ConfigModule also supplies a couple of helpers you might find useful.
72
75
 
73
- 1. You can also set the "namespace" you want to use, this is great for apps with multiple environments:
76
+ ### Namespaces
77
+
78
+ You can also set the "namespace" you want to use, this is great for apps with different configurations per environment:
79
+
80
+ ```ruby
81
+ namespace ENV['my_environment']
82
+ ```
83
+
84
+ This will set the root of the configuration tree to whichever branch you specify, so you don't have to.
85
+
86
+ Depending on your configuration file's structure, it might be useful to pull out a deeper subtree, in that case you can include multiple keys separated by commas, or even give it an array.
87
+
88
+ Check out the [example section](#example) below to see how it's used.
89
+
90
+ ### The `config` Method
91
+
92
+ There's also a new method available in your module that points to the root of your configuration data:
93
+
94
+ ```ruby
95
+ def foo
96
+ config.foo
97
+ end
98
+ ```
99
+
100
+ ### Check for Presence of Configuration Keys with `has_key?`
101
+
102
+ There are times you want to provde defaults for values, and in typical Ruby fashion you would probably do this:
103
+
104
+ ```ruby
105
+ MyConfig.some_option || 'my default value'
106
+ ```
107
+
108
+ Occassionally though, that won't be good enough because the value could intentionally be `false` or even `nil`. In those situations, you might want to check to see if the key exists (especially useful along with namespaces). Much like a Hash, you can use the `has_key?` method and do something like:
109
+
110
+ ```ruby
111
+ if MyConfig.has_key? :some_option then
112
+ MyConfig.some_option
113
+ else
114
+ 'my default value'
115
+ end
116
+ ```
117
+
118
+ ### Hash-like Access
119
+
120
+ You can access config options like a hash too, if you want:
74
121
 
75
122
  ```ruby
76
- namespace ENV['my_environment']
123
+ MyConfig[:some_key].is_a? Hash #=> true
77
124
  ```
78
125
 
79
- This will set the root of the tree to whichever branch you specify, so you don't have to.
80
-
81
- 2. There's also a new method available in your module that points directly to the raw configuration data:
82
-
126
+ This is useful mainly when you'd rather get a `nil` instead of raising an error for nonexistant keys:
127
+
83
128
  ```ruby
84
- config
129
+ MyConfig[:nonexistant_key] #=> nil
130
+ MyConfig.nonexistant_key #=> raises ConfigModule::ConfigOption::NotFoundError
85
131
  ```
86
-
87
- Don't overwrite this method!
88
132
 
89
- 3. You can still access raw data from outside the module too, if you want:
90
-
133
+ It'll also avoid any naming conflicts that might arise between methods defined on ConfigModule or ConfigOption and your key names. You can use it in concert with the above `config` method instead of `self` to enhance readability:
134
+
91
135
  ```ruby
92
- MyConfig[:some_key].is_a? Hash #=> true
136
+ def bar
137
+ config[:namespace]
138
+ end
139
+ ```
140
+
141
+
142
+ ### Enumerable
143
+
144
+ `ConfigOption` is the way ConfigModule packages up subtrees, and unlike `OpenStruct`, it is `Enumerable`:
145
+
146
+ ```ruby
147
+ MyConfig.some_key_with_subkeys.each do |subkey|
148
+ puts subkey
149
+ end
93
150
  ```
94
151
 
95
152
  Example
@@ -99,9 +156,10 @@ Given a YAML file `./config/example.yml':
99
156
 
100
157
  ```yaml
101
158
  ---
102
- :production:
103
- :foo: bar
104
- :noodle: boom!
159
+ :example:
160
+ :production:
161
+ :foo: bar
162
+ :noodle: boom!
105
163
  ```
106
164
 
107
165
  And you set up your module:
@@ -113,12 +171,18 @@ module ExampleConfig
113
171
  extend ConfigModule
114
172
 
115
173
  config_file './config/example.yml'
116
- namespace Rails.env
174
+ namespace :example, Rails.env
117
175
 
118
176
  module_function
119
177
 
120
178
  def kanoodle
121
- 'ka' + config.noodle
179
+ 'ka' + noodle
180
+ end
181
+
182
+ def all_keys
183
+ config.map do |key, _|
184
+ key
185
+ end
122
186
  end
123
187
  end
124
188
  ```
@@ -127,7 +191,10 @@ Then you can use it like this:
127
191
 
128
192
  ```ruby
129
193
  ExampleConfig.foo #=> 'bar'
194
+ ExampleConfig[:foo] #=> 'bar'
195
+ ExampleConfig[:notakey] #=> nil
130
196
  ExampleConfig.kanoodle #=> 'kaboom!'
197
+ ExampleConfig.all_keys #=> [:foo, :noodle]
131
198
  ```
132
199
 
133
200
  Pretty nifty, huh?
@@ -135,8 +202,8 @@ Pretty nifty, huh?
135
202
  Caveats
136
203
  -------
137
204
 
138
- - **Q:** You mention "valid key". What's a valid key?
139
- - **A:** It's any object that you can call `.to_sym` on!
205
+ - **Q:** You mention "valid key". What's a valid key?
206
+ - **A:** It's any object that you can call `.to_sym` on (same as `OpenStruct`)!
140
207
 
141
208
  Who made this anyway?
142
209
  ---------------------
@@ -144,4 +211,4 @@ Who made this anyway?
144
211
  I'm glad you asked!
145
212
 
146
213
  Anthony M. Cook 2013
147
-
214
+
@@ -19,7 +19,10 @@ module ConfigModule
19
19
  raise
20
20
  end
21
21
  end
22
- alias_method :field_lookup_handler, :method_missing_handler
22
+
23
+ def field_lookup_handler name, source, *args, &block
24
+ config[name]
25
+ end
23
26
 
24
27
  def load_config
25
28
  @raw_config = YAML.load_file config_file
@@ -19,10 +19,14 @@ module ConfigModule
19
19
  @table[name.to_sym]
20
20
  end
21
21
 
22
+ def has_key? key
23
+ @table.has_key? key
24
+ end
25
+
22
26
  def method_missing name, *args, &block
23
27
  result = super
24
28
 
25
- if result || @table.include?(name) then
29
+ if result || @table.has_key?(name) then
26
30
  self.class.wrap result
27
31
  else
28
32
  raise(
@@ -1,3 +1,3 @@
1
1
  module ConfigModule
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
data/lib/config_module.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require 'ostruct'
2
2
  require 'yaml'
3
- require 'pry'
4
3
 
5
4
  require_relative 'config_module/version'
6
5
  require_relative 'config_module/exceptions'
@@ -16,6 +15,10 @@ module ConfigModule
16
15
  __config_module_helper.config
17
16
  end
18
17
 
18
+ def has_key? key
19
+ __config_module_helper.config.has_key? key
20
+ end
21
+
19
22
  protected
20
23
 
21
24
  def config_file new_config_file
@@ -17,6 +17,10 @@ spec 'config modules return correct data using []' do
17
17
  ExampleConfig[:foo] == 'bar'
18
18
  end
19
19
 
20
+ spec 'when using [], return nil for nonexistant keys' do
21
+ ExampleConfig[:nonexistant] == nil
22
+ end
23
+
20
24
  spec 'nested hash values are properly wrapped' do
21
25
  ExampleConfig.dictionary.class == ConfigModule::ConfigOption
22
26
  end
@@ -15,6 +15,18 @@ spec 'ConfigOptions are Enumerable' do
15
15
  opt.map{|k,v| v[:b]} == [5]
16
16
  end
17
17
 
18
+ spec 'identifies the presence of keys' do
19
+ opt.has_key? :a
20
+ end
21
+
22
+ spec 'identifies the lack of keys' do
23
+ opt.has_key?('nonexistant') == false
24
+ end
25
+
26
+ spec 'identifies the presence of nested keys' do
27
+ opt.a.has_key? :b
28
+ end
29
+
18
30
  spec 'to_ary' do
19
31
  begin
20
32
  opt.to_ary
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: config_module
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anthony Cook
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-14 00:00:00.000000000 Z
11
+ date: 2013-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: uspec