multi_config 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -16,5 +16,5 @@ spec/reports
16
16
  test/tmp
17
17
  test/version_tmp
18
18
  tmp
19
- spec/support/*db*
19
+ spec/support/db/*db*
20
20
  .rbenv*
@@ -3,7 +3,7 @@ module MultiConfig
3
3
  module ActiveRecord
4
4
  def self.included(mod)
5
5
  mod.extend ClassMethods
6
- mod.send(:class_variable_set, :'@@db_configs', [])
6
+ mod.send(:class_variable_set, :'@@db_configs', Hash.new { |h, k| h[k] = [] })
7
7
  end
8
8
 
9
9
  module ClassMethods
@@ -11,36 +11,37 @@ module MultiConfig
11
11
  file_name += '.yml' unless File.extname(file_name).eql? '.yml'
12
12
  unless file_name == 'database.yml'
13
13
  namespace = File.basename(file_name, '.yml')
14
- add_to_db_configs(file_name, namespace)
15
- raise "Configuration for #{::Rails.env} environment not defined in #{config_path file_name}" unless
16
- configurations.include? "#{namespace}_#{::Rails.env}"
14
+ add_db_config(file_name, namespace)
15
+ raise "Configuration for #{::Rails.env} environment not defined in #{Config.path file_name}" unless configurations.include? "#{namespace}_#{::Rails.env}"
17
16
  establish_connection "#{namespace}_#{::Rails.env}"
18
17
  end
19
18
  end
20
19
 
21
20
  private
22
-
23
- def add_to_db_configs(file_name, namespace)
21
+ def add_db_config(file_name, namespace)
24
22
  db_configs = class_variable_get(:'@@db_configs')
25
23
  unless db_configs.include?(namespace)
26
- configurations.merge!(config(file_name, namespace))
27
- db_configs << namespace
24
+ configurations.merge!(Config.load(file_name, namespace))
25
+ db_configs[namespace] << name
28
26
  end
29
27
  end
28
+ end
30
29
 
31
- def config_path(file_name)
30
+ module Config
31
+ def self.path(file_name)
32
32
  File.join(Rails.root, 'config', file_name)
33
33
  end
34
34
 
35
- def config(file_name, namespace)
35
+ def self.load(file_name, namespace)
36
36
  begin
37
- YAML.load(ERB.new(File.read(config_path(file_name))).result).inject({}) do |hash,(k,v)|
37
+ require 'erb'
38
+ YAML.load(ERB.new(IO.read(path(file_name))).result).inject({}) do |hash, (k, v)|
38
39
  hash["#{namespace}_#{k}"]=v
39
40
  hash
40
41
  end
41
42
  rescue Exception => exc
42
- raise "File #{config_path file_name} does not exist in config" unless File.exists?(config_path(file_name))
43
- raise "Invalid config file #{config_path file_name}"
43
+ raise "File #{path file_name} does not exist in config" unless File.exists?(path(file_name))
44
+ raise "Invalid config file #{path file_name}"
44
45
  end
45
46
  end
46
47
  end
@@ -1,3 +1,3 @@
1
1
  module MultiConfig
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -14,7 +14,7 @@ describe ActiveRecord::Base, "methods" do
14
14
  end
15
15
 
16
16
  it "should have @@db_config class variable set" do
17
- ActiveRecord::Base.send(:class_variable_get, '@@db_configs').should == []
17
+ ActiveRecord::Base.send(:class_variable_get, '@@db_configs').should == {}
18
18
  end
19
19
 
20
20
  end
@@ -9,4 +9,8 @@ describe UsesOtherYml do
9
9
  it "should be connected to the default database" do
10
10
  UsesOtherYml.connection.instance_variable_get(:@config)[:database].split('/').last.should == "other_db"
11
11
  end
12
+
13
+ it "the @@db_configs should have entry for this class" do
14
+ UsesOtherYml.send(:class_variable_get,:'@@db_configs').should == {"other" => ["UsesOtherYml"]}
15
+ end
12
16
  end
@@ -5,6 +5,7 @@ describe MultiConfig::ORMs::ActiveRecord do
5
5
  let!(:other_config){ load_namespaced_config('other.yml') }
6
6
  before(:each) do
7
7
  @test_class = Class.new
8
+ @test_class.stub_chain(:name).and_return('test_class')
8
9
  @test_class.stub(:configurations).and_return({})
9
10
  @test_class.stub(:establish_connection)
10
11
  @test_class.send(:include, MultiConfig::ORMs::ActiveRecord)
@@ -32,11 +33,11 @@ describe MultiConfig::ORMs::ActiveRecord do
32
33
 
33
34
  describe "should modify class variable @@db_configs" do
34
35
  before do
35
- test_class.send(:class_variable_get, '@@db_configs').should == []
36
+ test_class.send(:class_variable_get, '@@db_configs').should == {}
36
37
  test_class.config_file = 'other'
37
38
  end
38
39
  subject {test_class.send(:class_variable_get, '@@db_configs')}
39
- it{should == ['other']}
40
+ it{should == {"other" => ["test_class"]}}
40
41
  end
41
42
 
42
43
  describe "should modify .configurations" do
@@ -86,9 +87,9 @@ describe MultiConfig::ORMs::ActiveRecord do
86
87
  describe "should not do anything if database.yml specified" do
87
88
  before do
88
89
  test_class.should_not_receive(:establish_connection)
89
- test_class.should_not_receive(:add_to_db_configs)
90
- test_class.should_not_receive(:config_path)
91
- test_class.should_not_receive(:config)
90
+ test_class.should_not_receive(:add_db_config)
91
+ MultiConfig::ORMs::ActiveRecord::Config.should_not_receive(:path)
92
+ MultiConfig::ORMs::ActiveRecord::Config.should_not_receive(:load)
92
93
  expect {
93
94
  test_class.config_file = 'database'
94
95
  }.not_to change(test_class, :configurations)
@@ -1,5 +1,5 @@
1
1
  development: &development
2
- database: db
2
+ database: db/db
3
3
  host: localhost
4
4
  adapter: sqlite3
5
5
 
@@ -1,5 +1,5 @@
1
1
  development: &development
2
- database: other_db
2
+ database: db/other_db
3
3
  host: localhost
4
4
  adapter: sqlite3
5
5
 
@@ -1,5 +1,5 @@
1
1
  def load_namespaced_config(file)
2
2
  namespace = File.basename(file, File.extname(file))
3
- config = YAML.load(ERB.new(File.read(File.expand_path("../config/#{file}", __FILE__))).result)
3
+ config = YAML.load(ERB.new(IO.read(File.expand_path("../config/#{file}", __FILE__))).result)
4
4
  config.inject({}){|h,(k,v)| h["#{namespace}_#{k}"]=v;h}
5
5
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multi_config
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 0
9
8
  - 1
10
- version: 0.0.1
9
+ - 0
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Shadab Ahmed
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-09-08 00:00:00 Z
18
+ date: 2012-09-09 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: bundler
@@ -134,7 +134,6 @@ files:
134
134
  - spec/spec_helper.rb
135
135
  - spec/support/active_record_config.rb
136
136
  - spec/support/config/database.yml
137
- - spec/support/config/db
138
137
  - spec/support/config/invalid.yml
139
138
  - spec/support/config/other.yml
140
139
  - spec/support/config_helper.rb
@@ -181,7 +180,6 @@ test_files:
181
180
  - spec/spec_helper.rb
182
181
  - spec/support/active_record_config.rb
183
182
  - spec/support/config/database.yml
184
- - spec/support/config/db
185
183
  - spec/support/config/invalid.yml
186
184
  - spec/support/config/other.yml
187
185
  - spec/support/config_helper.rb
File without changes