basic_config 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/.travis.yml +7 -0
- data/README.md +19 -6
- data/basic_config.gemspec +1 -1
- data/spec/basic_config_spec.rb +10 -4
- metadata +3 -2
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# BasicConfig
|
2
2
|
|
3
|
+
[](http://travis-ci.org/stephan778/basic_config)
|
4
|
+
|
3
5
|
Friendly configuration wrapper. If you find yourself using things like:
|
4
6
|
|
5
7
|
```ruby
|
@@ -25,7 +27,12 @@ Or install it via RubyGems as:
|
|
25
27
|
## Usage
|
26
28
|
|
27
29
|
```ruby
|
30
|
+
# Loading config is simple
|
28
31
|
settings = BasicConfig.load_file('config.yml')
|
32
|
+
# It's a simple variable, you can create as many as you like.
|
33
|
+
# You can use constants for convinience:
|
34
|
+
AppConfig = BasicConfig.load_file('config/app.yml')
|
35
|
+
DatabaseConfig = BasicConfig.load_file('config/database.yml')
|
29
36
|
|
30
37
|
# Access your configuration with simple method calls
|
31
38
|
settings.some_param
|
@@ -45,7 +52,7 @@ If your file has sections for different environments:
|
|
45
52
|
development:
|
46
53
|
host: localhost
|
47
54
|
port: 123
|
48
|
-
test
|
55
|
+
test:
|
49
56
|
host: localhost
|
50
57
|
port: 456
|
51
58
|
```
|
@@ -56,14 +63,14 @@ AppConfig = BasicConfig.load_env('config.yml', Rails.env)
|
|
56
63
|
|
57
64
|
## Why should I use it instead of plain Hash variables?
|
58
65
|
|
59
|
-
### It raises errors when you unintentionally read non-existent keys
|
66
|
+
### It raises errors when you unintentionally read non-existent keys
|
60
67
|
|
61
68
|
If you are using a `Hash`:
|
62
69
|
```ruby
|
63
70
|
secret_token = AppConfig[:something]
|
64
71
|
```
|
65
|
-
and for some reason your configuration does not have `:something` in it
|
66
|
-
get a `nil`. Worst case: this `nil` will live inside your system compromising
|
72
|
+
and for some reason your configuration does not have `:something` in it (or you
|
73
|
+
make a typo) - you'll get a `nil`. Worst case: this `nil` will live inside your system compromising
|
67
74
|
or corrupting some data until you finally notice and track it down back to this line.
|
68
75
|
|
69
76
|
If you are using a `BasicConfig`:
|
@@ -78,7 +85,7 @@ particular key exist in your config - `AppConfig.include?(:something)`.
|
|
78
85
|
Additionaly, for some keys it makes sense to get a `nil` when they do not exist and for this
|
79
86
|
purpose there is a `[]` method which is delegated to underlying hash.
|
80
87
|
|
81
|
-
### Works recursively
|
88
|
+
### Works recursively
|
82
89
|
|
83
90
|
If your YAML is more than 1 level deep then simple `symbolize_keys` is not going to be enough:
|
84
91
|
```ruby
|
@@ -90,10 +97,16 @@ With BasicConfig above would look like this:
|
|
90
97
|
AppConfig.something.have_to_use_string_here
|
91
98
|
```
|
92
99
|
|
93
|
-
### Easier to test
|
100
|
+
### Easier to test
|
94
101
|
|
95
102
|
You can stub out any config variable just like a normal method in your tests.
|
96
103
|
|
97
104
|
```ruby
|
98
105
|
AppConfig.stub(:something).and_return('anything')
|
99
106
|
```
|
107
|
+
|
108
|
+
## Gotchas
|
109
|
+
|
110
|
+
The only thing that I can think of is be aware that you can not use Ruby Object
|
111
|
+
method names for your configuration variable names (`puts`, `print`, `raise`,
|
112
|
+
`display`, etc, you can see the full list with `BasicConfig.methods`).
|
data/basic_config.gemspec
CHANGED
data/spec/basic_config_spec.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
require_relative '../lib/basic_config'
|
2
|
-
require 'active_support/core_ext/hash'
|
3
2
|
|
4
3
|
describe BasicConfig do
|
5
4
|
let(:hash) do
|
@@ -11,6 +10,15 @@ describe BasicConfig do
|
|
11
10
|
}
|
12
11
|
}
|
13
12
|
end
|
13
|
+
let(:symbolized_hash) do
|
14
|
+
{
|
15
|
+
:one => 'something',
|
16
|
+
:two => 'other_value',
|
17
|
+
:three => {
|
18
|
+
:nested => '123'
|
19
|
+
}
|
20
|
+
}
|
21
|
+
end
|
14
22
|
|
15
23
|
subject { BasicConfig.new(hash) }
|
16
24
|
|
@@ -43,9 +51,7 @@ describe BasicConfig do
|
|
43
51
|
end
|
44
52
|
|
45
53
|
it 'can be converted back to hash' do
|
46
|
-
|
47
|
-
converted['three'] = converted['three'].stringify_keys
|
48
|
-
converted.should == hash
|
54
|
+
subject.to_hash.should == symbolized_hash
|
49
55
|
end
|
50
56
|
|
51
57
|
describe '::load_file' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: basic_config
|
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: 2012-
|
12
|
+
date: 2012-06-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -36,6 +36,7 @@ extensions: []
|
|
36
36
|
extra_rdoc_files: []
|
37
37
|
files:
|
38
38
|
- .gitignore
|
39
|
+
- .travis.yml
|
39
40
|
- Gemfile
|
40
41
|
- LICENSE
|
41
42
|
- README.md
|