quandl-config 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +32 -2
- data/lib/quandl/{configurable.rb → config/configurable.rb} +0 -2
- data/lib/quandl/{project_root.rb → config/project_root.rb} +0 -0
- data/lib/quandl/config/version.rb +5 -0
- data/lib/quandl/config.rb +23 -10
- data/spec/quandl/quandl-configurable_spec.rb +52 -4
- data/spec/spec_helper.rb +0 -1
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9f99d2c151b092438f654afcdb16e59ad4f67522
|
4
|
+
data.tar.gz: a5e7c655ff56638c343113fb6619995e589ef0e5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e734ff03977f94c1302477277be280834315bfcf85960c0ef596c8bff09260558508c3bed7edd315ccc8fd09bac840d33eafb5a558d6791488f577ea5a946330
|
7
|
+
data.tar.gz: c8f1487d99ab0148a215d25b35f811e0b383e36c164777ec3c20b793d4a2827252c7f728b5e4c469f77a9083c60af8f986d2f756523a111b3dd22f08479b574a
|
data/README.md
CHANGED
@@ -58,9 +58,39 @@ end
|
|
58
58
|
|
59
59
|
### What if my project is not a Rails app?
|
60
60
|
|
61
|
-
That's ok. `Quandl::Config` will find your config file if it's in a `config` folder in the root of your app.
|
61
|
+
That's ok. `Quandl::Config` will find your config file if it's in a `config` folder in the root of your app. See also the [How do I override default values?](#how-do-i-override-default-values) for more information.
|
62
62
|
|
63
|
-
|
63
|
+
### How do I override default values?
|
64
|
+
|
65
|
+
You can override default values by adding a `configuration_options` method to your class/instance. The options available to be overwritten are:
|
66
|
+
|
67
|
+
* `root_path` - Override this when you want to change the default path to your configuration files root folder. (default: your project path)
|
68
|
+
* `environment` - Override this when you are not using a rails project and don't set the environment via `ENV['RAILS_ENV']`
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
class A::B::Special
|
72
|
+
extend Quandl::Configurable
|
73
|
+
|
74
|
+
def self.configuration_options
|
75
|
+
{
|
76
|
+
root_path: Pathname.new('~/configs/my_project'),
|
77
|
+
environment: ENV['MY_PROJECT']
|
78
|
+
}
|
79
|
+
end
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
## FAQ
|
84
|
+
|
85
|
+
### My config file won't reload in development mode (Rails)
|
86
|
+
|
87
|
+
quandl-config utilizes an internal caching schema to save previously loaded configurations so that they don't load twice. You may need to restart your server in certain cases depending on how quandl-config was used in your project.
|
88
|
+
|
89
|
+
Additionally if you have `included` via `include Quandl::Configurable` you can use the following method to reset the internal config. This however will not work when `extending` the module.
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
Quandl::Config.clear_internal_cache
|
93
|
+
```
|
64
94
|
|
65
95
|
## Contributing
|
66
96
|
|
File without changes
|
data/lib/quandl/config.rb
CHANGED
@@ -1,20 +1,29 @@
|
|
1
1
|
require 'ostruct'
|
2
2
|
require 'yaml'
|
3
|
-
require '
|
3
|
+
require 'active_support/inflector'
|
4
|
+
require 'pathname'
|
5
|
+
|
6
|
+
require_relative 'config/project_root'
|
7
|
+
require_relative 'config/configurable'
|
4
8
|
|
5
9
|
module Quandl
|
6
10
|
class Config < ::OpenStruct
|
7
|
-
|
11
|
+
# Optimize loading of configs multiple times by keeping a hash of already loaded configs.
|
12
|
+
def self.new(filename, options = {})
|
13
|
+
@_registered_configs ||= {}
|
14
|
+
return @_registered_configs[filename] if @_registered_configs.key?(filename)
|
15
|
+
@_registered_configs[filename] = super
|
16
|
+
end
|
8
17
|
|
9
|
-
def
|
10
|
-
|
11
|
-
|
12
|
-
config = YAML.load(erb_config)[project_environment]
|
18
|
+
def self.clear_internal_cache
|
19
|
+
@_registered_configs = {}
|
20
|
+
end
|
13
21
|
|
22
|
+
def initialize(file_name, options = {})
|
14
23
|
@_root = options.delete(:root_path)
|
15
24
|
@_environment = options.delete(:environment)
|
16
25
|
|
17
|
-
super(
|
26
|
+
super(read_config(file_name))
|
18
27
|
end
|
19
28
|
|
20
29
|
def configurable_attributes
|
@@ -25,8 +34,14 @@ module Quandl
|
|
25
34
|
|
26
35
|
private
|
27
36
|
|
37
|
+
def read_config(file_name)
|
38
|
+
raw_config = File.read(Pathname.new(project_root).join('config', "#{file_name}.yml"))
|
39
|
+
erb_config = ERB.new(raw_config).result
|
40
|
+
YAML.load(erb_config)[project_environment]
|
41
|
+
end
|
42
|
+
|
28
43
|
def project_root
|
29
|
-
@_root ||= defined?(Rails) ? ::Rails.root :
|
44
|
+
@_root ||= defined?(Rails) ? ::Rails.root : ProjectRoot.root
|
30
45
|
end
|
31
46
|
|
32
47
|
def project_environment
|
@@ -34,5 +49,3 @@ module Quandl
|
|
34
49
|
end
|
35
50
|
end
|
36
51
|
end
|
37
|
-
|
38
|
-
require 'quandl/configurable'
|
@@ -1,17 +1,65 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
+
module ConfigurationOverride
|
4
|
+
def configuration_options
|
5
|
+
{
|
6
|
+
root_path: Pathname.new(Dir.tmpdir),
|
7
|
+
environment: 'this_is_a_test'
|
8
|
+
}
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
3
12
|
describe Quandl::Configurable do
|
13
|
+
before(:each) do
|
14
|
+
Quandl::Config.clear_internal_cache
|
15
|
+
end
|
16
|
+
|
17
|
+
shared_examples 'override configuration' do
|
18
|
+
it 'should use the overwritten values' do
|
19
|
+
expect(File).to receive(:read).with(Pathname.new(Dir.tmpdir).join('config', 'fake.yml')).and_return('---\n:a: :b\n')
|
20
|
+
expect(subject.send(:project_environment)).to eq('this_is_a_test')
|
21
|
+
expect(subject.send(:project_root)).to eq(Pathname.new(Dir.tmpdir))
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
4
25
|
context 'when extending' do
|
5
|
-
|
26
|
+
subject { Fake.configuration }
|
27
|
+
|
28
|
+
before(:each) do
|
6
29
|
Fake.extend(Quandl::Configurable)
|
7
|
-
|
30
|
+
Fake.instance_variable_set(:@configuration, nil)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'adds a configuration class method' do
|
34
|
+
expect(subject).to be_kind_of(Quandl::Config)
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'when overriding configuration options' do
|
38
|
+
before(:each) do
|
39
|
+
Fake.extend(ConfigurationOverride)
|
40
|
+
end
|
41
|
+
|
42
|
+
include_examples 'override configuration'
|
8
43
|
end
|
9
44
|
end
|
10
45
|
|
11
46
|
context 'when including' do
|
12
|
-
|
47
|
+
subject { Fake.new.configuration }
|
48
|
+
|
49
|
+
before(:each) do
|
13
50
|
Fake.include(Quandl::Configurable)
|
14
|
-
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'adds a configuration instance method' do
|
54
|
+
expect(subject).to be_kind_of(Quandl::Config)
|
55
|
+
end
|
56
|
+
|
57
|
+
context 'when overriding configuration options' do
|
58
|
+
before(:each) do
|
59
|
+
Fake.include(ConfigurationOverride)
|
60
|
+
end
|
61
|
+
|
62
|
+
include_examples 'override configuration'
|
15
63
|
end
|
16
64
|
end
|
17
65
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: quandl-config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Najwa Azer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-08-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -152,7 +152,7 @@ dependencies:
|
|
152
152
|
version: '0'
|
153
153
|
description: Load ERB-based YML files into open OpenStruct objects for ease of use.
|
154
154
|
email:
|
155
|
-
-
|
155
|
+
- dev@quandl.com
|
156
156
|
executables: []
|
157
157
|
extensions: []
|
158
158
|
extra_rdoc_files: []
|
@@ -160,8 +160,9 @@ files:
|
|
160
160
|
- LICENSE.txt
|
161
161
|
- README.md
|
162
162
|
- lib/quandl/config.rb
|
163
|
-
- lib/quandl/configurable.rb
|
164
|
-
- lib/quandl/project_root.rb
|
163
|
+
- lib/quandl/config/configurable.rb
|
164
|
+
- lib/quandl/config/project_root.rb
|
165
|
+
- lib/quandl/config/version.rb
|
165
166
|
- spec/fixtures/dummy_rails_app/application.rb
|
166
167
|
- spec/fixtures/dummy_rails_app/config/fake.yml
|
167
168
|
- spec/fixtures/dummy_rails_app/fake.rb
|