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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 403c3cbaabc16054de24a43fc59bffc6e3a3c13c
4
- data.tar.gz: c88b13a483cd007997e25a4c40c861f9333911ff
3
+ metadata.gz: 9f99d2c151b092438f654afcdb16e59ad4f67522
4
+ data.tar.gz: a5e7c655ff56638c343113fb6619995e589ef0e5
5
5
  SHA512:
6
- metadata.gz: 09ae2b94937905c73ecd90c05acb8bdd2987cac21faa7b612b5dc0422bdcff800ad6cccdb243ebdc08f44b6a03ba985a7a8a2c4ac37df9bd866d9829352c0a9f
7
- data.tar.gz: 8abac3e2cd3051478a54d6c78ee1e7231e6961869f0f2ce26c301505554e3750495594458c78d4b2f061175a9381dc8c0b27e6c092943233f6d8a2370b6ea1cf
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
- If you want to provide the environment, use `ENV['RAILS_ENV']` or `ENV['RAKE_ENV']`. If you don't, the `default` environment will be assumed.
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
 
@@ -1,5 +1,3 @@
1
- require 'active_support/inflector'
2
-
3
1
  module Quandl
4
2
  module Configurable
5
3
  def configuration
@@ -0,0 +1,5 @@
1
+ module Quandl
2
+ module Configurable
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
data/lib/quandl/config.rb CHANGED
@@ -1,20 +1,29 @@
1
1
  require 'ostruct'
2
2
  require 'yaml'
3
- require 'quandl/project_root'
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
- VERSION = '0.0.4'
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 initialize(file_name, options = {})
10
- raw_config = File.read(project_root.join('config', "#{file_name}.yml"))
11
- erb_config = ERB.new(raw_config).result
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(config)
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 : Pathname.new(ProjectRoot.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
- it 'adds a configuration class method' do
26
+ subject { Fake.configuration }
27
+
28
+ before(:each) do
6
29
  Fake.extend(Quandl::Configurable)
7
- expect(Fake.configuration).to be_kind_of(Quandl::Config)
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
- it 'adds a configuration instance method' do
47
+ subject { Fake.new.configuration }
48
+
49
+ before(:each) do
13
50
  Fake.include(Quandl::Configurable)
14
- expect(Fake.new.configuration).to be_kind_of(Quandl::Config)
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
@@ -1,3 +1,2 @@
1
- require 'pry'
2
1
  require 'quandl/config'
3
2
  require 'fixtures/dummy_rails_app/application'
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
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-07-15 00:00:00.000000000 Z
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
- - najwa.azer@gmail.com
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