dynamic_configuration 0.1.1

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/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'rake'
4
+ require 'jeweler'
5
+ require 'rspec/core/rake_task'
6
+
7
+ desc 'Default: Run all specs for a specific rails version.'
8
+ task :default => :spec
9
+
10
+ desc "Run all specs for a specific rails version"
11
+ RSpec::Core::RakeTask.new(:spec) do |t|
12
+ t.pattern = '**/*_spec.rb'
13
+ end
14
+
15
+ Jeweler::Tasks.new do |gem|
16
+ gem.name = "dynamic_configuration"
17
+ gem.summary = "Flexible configuration library for Ruby and Rails applications."
18
+ gem.description = "Flexible configuration library for Ruby and Rails applications."
19
+ gem.email = "jrzeszotko@gmail.com"
20
+ gem.homepage = "http://github.com/jaroslawr/dynamic_configuration"
21
+ gem.authors = ["Jarosław Rzeszótko"]
22
+ end
23
+
24
+ Jeweler::RubygemsDotOrgTasks.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,41 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{dynamic_configuration}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = [%q{Jarosław Rzeszótko}]
12
+ s.date = %q{2012-01-26}
13
+ s.description = %q{Flexible configuration library for Ruby and Rails applications.}
14
+ s.email = %q{jrzeszotko@gmail.com}
15
+ s.files = [
16
+ "Rakefile",
17
+ "VERSION",
18
+ "dynamic_configuration.gemspec",
19
+ "lib/dynamic_configuration.rb",
20
+ "spec/dynamic_configuration_spec.rb",
21
+ "spec/options/local/main.rb",
22
+ "spec/options/main.rb",
23
+ "spec/options/production/main.rb",
24
+ "spec/options/test/main.rb",
25
+ "spec_helper.rb"
26
+ ]
27
+ s.homepage = %q{http://github.com/jaroslawr/dynamic_configuration}
28
+ s.require_paths = [%q{lib}]
29
+ s.rubygems_version = %q{1.8.5}
30
+ s.summary = %q{Flexible configuration library for Ruby and Rails applications.}
31
+
32
+ if s.respond_to? :specification_version then
33
+ s.specification_version = 3
34
+
35
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
36
+ else
37
+ end
38
+ else
39
+ end
40
+ end
41
+
@@ -0,0 +1,123 @@
1
+ require 'blankslate'
2
+
3
+ module DynamicConfiguration
4
+ def self.create(const_name, config_file_name)
5
+ ConfigFactory.new.create_config(const_name, config_file_name)
6
+ end
7
+
8
+ private
9
+
10
+ class ConfigFactory
11
+ def create_config(const_name, config_file_name)
12
+ setup_config(const_name, config_file_name)
13
+ load_main_configuration_files
14
+ load_per_environment_configuration_files if Object.const_defined?(:Rails)
15
+ load_local_configuration_files
16
+
17
+ return @config
18
+ end
19
+
20
+ private
21
+
22
+ def setup_config(const_name, config_path)
23
+ @const_name = const_name
24
+ @config_path = Pathname.new(config_path)
25
+ @config = Config.new(@const_name, @config_path)
26
+
27
+ Object.const_set @const_name, @config
28
+ end
29
+
30
+ def load_main_configuration_files
31
+ @config_path.entries.each do |mod_file|
32
+ next if ["..", "."].include?(mod_file.basename.to_s)
33
+ mod_file = @config_path + mod_file
34
+ next unless mod_file.file?
35
+ @config.load_module(mod_file)
36
+ end
37
+ end
38
+
39
+ def load_per_environment_configuration_files
40
+ @config_path.entries.each do |directory|
41
+ next if ["..", "."].include?(directory.basename.to_s)
42
+ directory = @config_path + directory
43
+ next unless directory.directory? && Rails.env == directory.basename.to_s
44
+
45
+ directory.entries.each do |mod_file|
46
+ mod_file = directory + mod_file
47
+ next unless mod_file.file?
48
+ @config.load_module(mod_file)
49
+ end
50
+ end
51
+ end
52
+
53
+ def load_local_configuration_files
54
+ local_settings_exist = FileTest.directory?(@config_path.to_s + "/local")
55
+ rails_test_env = Object.const_defined?(:Rails) && Rails.env == 'test'
56
+ return if !local_settings_exist || rails_test_env
57
+
58
+ local_mod_files_dir = @config_path + Pathname.new("local/")
59
+ local_mod_files_dir.entries.each do |mod_file|
60
+ next if ["..", "."].include?(mod_file.basename.to_s)
61
+ mod_file = local_mod_files_dir + mod_file
62
+ next unless mod_file.file?
63
+ @config.load_module(mod_file)
64
+ end
65
+ end
66
+ end
67
+
68
+ class Config < ::BlankSlate
69
+ def initialize(const_name, config_path)
70
+ @const_name, @config_path = const_name, config_path
71
+ end
72
+
73
+ def load_module(file_pathname)
74
+ mod_name = file_pathname.basename.to_s[0..-4]
75
+
76
+ @modules ||= {}
77
+ @modules[mod_name.intern] ||= Submodule.new
78
+ @modules[mod_name.intern].instance_eval(::IO.read(file_pathname.to_s))
79
+
80
+ @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
89
+ end
90
+
91
+ def method_missing(name, *args, &block)
92
+ unless @settings && @settings[name]
93
+ raise MissingSubmoduleException.new("No configuration defined for a '#{name}' submodule")
94
+ end
95
+
96
+ @settings[name]
97
+ end
98
+ end
99
+
100
+ class Submodule < ::BlankSlate
101
+ attr_accessor :settings
102
+
103
+ def initialize
104
+ @settings = {}
105
+ end
106
+
107
+ def method_missing(name, value)
108
+ @settings[name] = value
109
+ end
110
+ end
111
+
112
+ class Settings < ::BlankSlate
113
+ def initialize(settings)
114
+ @settings = settings
115
+ end
116
+
117
+ def method_missing(name, *args)
118
+ @settings[name]
119
+ end
120
+ end
121
+
122
+ class MissingSubmoduleException < StandardError; end
123
+ end
@@ -0,0 +1,33 @@
1
+ require_relative '../spec_helper'
2
+ require_relative '../lib/dynamic_configuration'
3
+
4
+ describe DynamicConfiguration do
5
+ let(:path) { "#{File.dirname(__FILE__)}/options" }
6
+
7
+ before(:each) do
8
+ Object.instance_eval { remove_const :Settings if const_defined?(:Settings) }
9
+ end
10
+
11
+ it "should create valid configurations" do
12
+ DynamicConfiguration::create(:Settings, path)
13
+
14
+ Settings.main.setting_one.should == 'Some string'
15
+ Settings.main.setting_two.should == 123456
16
+ end
17
+
18
+ it "should make per-environment settings take precedence over main configuration" do
19
+ Rails = mock(:env => 'test')
20
+
21
+ DynamicConfiguration::create(:Settings, path)
22
+
23
+ Settings.main.setting_three.should == [3, 2, 1]
24
+
25
+ Object.instance_eval { remove_const :Rails }
26
+ end
27
+
28
+ it "should make local settings take precedence even over per-environment settings" do
29
+ DynamicConfiguration::create(:Settings, path)
30
+
31
+ Settings.main.setting_four.should == 'overwrite-again'
32
+ end
33
+ end
@@ -0,0 +1 @@
1
+ setting_four 'overwrite-again'
@@ -0,0 +1,4 @@
1
+ setting_one 'Some string'
2
+ setting_two 123456
3
+ setting_three [1, 2, 3]
4
+ setting_four 'original'
@@ -0,0 +1 @@
1
+ setting_four 'Should be ignored'
@@ -0,0 +1,2 @@
1
+ setting_three [3, 2, 1]
2
+ setting_four 'overwrite'
data/spec_helper.rb ADDED
@@ -0,0 +1 @@
1
+ RSpec.configure
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dynamic_configuration
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jarosław Rzeszótko
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-26 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Flexible configuration library for Ruby and Rails applications.
15
+ email: jrzeszotko@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - Rakefile
21
+ - VERSION
22
+ - dynamic_configuration.gemspec
23
+ - lib/dynamic_configuration.rb
24
+ - spec/dynamic_configuration_spec.rb
25
+ - spec/options/local/main.rb
26
+ - spec/options/main.rb
27
+ - spec/options/production/main.rb
28
+ - spec/options/test/main.rb
29
+ - spec_helper.rb
30
+ homepage: http://github.com/jaroslawr/dynamic_configuration
31
+ licenses: []
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 1.8.5
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: Flexible configuration library for Ruby and Rails applications.
54
+ test_files: []