dynamic_configuration 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{dynamic_configuration}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Jarosław Rzeszótko}]
@@ -7,12 +7,15 @@ module DynamicConfiguration
7
7
 
8
8
  private
9
9
 
10
- class ConfigFactory
10
+ class ConfigFactory
11
11
  def create_config(const_name, config_file_name)
12
- setup_config(const_name, config_file_name)
12
+ @rails_loaded = Object.const_defined?(:Rails)
13
+
14
+ config = setup_config(const_name, config_file_name)
13
15
  load_main_configuration_files
14
- load_per_environment_configuration_files if Object.const_defined?(:Rails)
16
+ load_per_environment_configuration_files if @rails_loaded
15
17
  load_local_configuration_files
18
+ config.freeze
16
19
 
17
20
  return @config
18
21
  end
@@ -52,7 +55,7 @@ module DynamicConfiguration
52
55
 
53
56
  def load_local_configuration_files
54
57
  local_settings_exist = FileTest.directory?(@config_path.to_s + "/local")
55
- rails_test_env = Object.const_defined?(:Rails) && Rails.env == 'test'
58
+ rails_test_env = @rails_loaded && Rails.env == 'test'
56
59
  return if !local_settings_exist || rails_test_env
57
60
 
58
61
  local_mod_files_dir = @config_path + Pathname.new("local/")
@@ -65,7 +68,7 @@ module DynamicConfiguration
65
68
  end
66
69
  end
67
70
 
68
- class Config < ::BlankSlate
71
+ class Config < ::BlankSlate
69
72
  def initialize(const_name, config_path)
70
73
  @const_name, @config_path = const_name, config_path
71
74
  end
@@ -78,14 +81,7 @@ module DynamicConfiguration
78
81
  @modules[mod_name.intern].instance_eval(::IO.read(file_pathname.to_s))
79
82
 
80
83
  @settings ||= {}
81
- @settings[mod_name.intern] ||= Settings.new(@modules[mod_name.intern].settings)
82
- end
83
-
84
- def finalize
85
- ::ActiveSupport::Dependencies.autoload_paths << @config_path.to_s
86
- ::ActiveSupport::Dependencies.explicitly_unloadable_constants << @const_name.to_s
87
-
88
- self.freeze
84
+ @settings[mod_name.intern] ||= Settings.new(@const_name, mod_name, @modules[mod_name.intern].settings)
89
85
  end
90
86
 
91
87
  def method_missing(name, *args, &block)
@@ -106,18 +102,22 @@ module DynamicConfiguration
106
102
 
107
103
  def method_missing(name, value)
108
104
  @settings[name] = value
105
+ @settings[name].freeze
109
106
  end
110
107
  end
111
108
 
112
109
  class Settings < ::BlankSlate
113
- def initialize(settings)
110
+ def initialize(const_name, module_name, settings)
111
+ @const_name = const_name
112
+ @module_name = module_name
114
113
  @settings = settings
115
114
  end
116
115
 
117
116
  def method_missing(name, *args)
118
- @settings[name]
117
+ @settings.fetch(name) { raise MissingSettingException.new("Setting '#{@const_name}.#{@module_name}.#{name}' is not defined") }
119
118
  end
120
119
  end
121
120
 
122
121
  class MissingSubmoduleException < StandardError; end
122
+ class MissingSettingException < StandardError; end
123
123
  end
@@ -1,33 +1,49 @@
1
1
  require_relative '../spec_helper'
2
2
  require_relative '../lib/dynamic_configuration'
3
3
 
4
- describe DynamicConfiguration do
5
- let(:path) { "#{File.dirname(__FILE__)}/options" }
6
-
4
+ describe DynamicConfiguration do
7
5
  before(:each) do
8
- Object.instance_eval { remove_const :Settings if const_defined?(:Settings) }
9
- end
6
+ Object.instance_eval { remove_const :Settings if const_defined?(:Settings) }
7
+ end
8
+
9
+ let(:path) { "#{File.dirname(__FILE__)}/options" }
10
10
 
11
11
  it "should create valid configurations" do
12
12
  DynamicConfiguration::create(:Settings, path)
13
-
14
13
  Settings.main.setting_one.should == 'Some string'
15
14
  Settings.main.setting_two.should == 123456
16
15
  end
17
16
 
18
17
  it "should make per-environment settings take precedence over main configuration" do
19
- Rails = mock(:env => 'test')
20
-
18
+ Rails = mock(:env => 'test').as_null_object
21
19
  DynamicConfiguration::create(:Settings, path)
22
-
23
20
  Settings.main.setting_three.should == [3, 2, 1]
24
-
25
21
  Object.instance_eval { remove_const :Rails }
26
22
  end
27
23
 
28
24
  it "should make local settings take precedence even over per-environment settings" do
29
25
  DynamicConfiguration::create(:Settings, path)
30
-
31
26
  Settings.main.setting_four.should == 'overwrite-again'
32
27
  end
28
+
29
+ it "should freeze the created configuration" do
30
+ lambda {
31
+ DynamicConfiguration::create(:Settings, path)
32
+ Settings.main.setting_three << 4
33
+ }.should raise_error(RuntimeError)
34
+ end
35
+
36
+ it "should raise an exception if trying to use a submodule that is not defined" do
37
+ lambda {
38
+ DynamicConfiguration::create(:Settings, path)
39
+ Settings.xyz.setting_three
40
+ }.should raise_error(DynamicConfiguration::MissingSubmoduleException)
41
+ end
42
+
43
+ it "should raise an exception if trying to use a setting that is not defined" do
44
+ lambda {
45
+ DynamicConfiguration::create(:Settings, path)
46
+ Settings.main.xyz
47
+ }.should raise_error(DynamicConfiguration::MissingSettingException)
48
+ end
33
49
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamic_configuration
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: