connection_ninja 0.3.5 → 0.4.0

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/.autotest ADDED
@@ -0,0 +1 @@
1
+ Autotest.add_discovery { "rspec2" }
data/README.markdown CHANGED
@@ -6,18 +6,10 @@ Connection Ninja is a really simple gem to help connection to multiple databases
6
6
  Installation
7
7
  ============
8
8
 
9
- To install connection_ninja you need to be using gemcutter, so install it if you haven't:
9
+ Install the gem:
10
10
 
11
- sudo gem install gemcutter
11
+ gem install connection_ninja
12
12
 
13
- And enable the gem source
14
-
15
- gem tumble
16
-
17
- Then you can install connection_ninja
18
-
19
- sudo gem install connection_ninja
20
-
21
13
  Configuration
22
14
  =============
23
15
 
@@ -25,29 +17,28 @@ Configuration
25
17
  use_connection_ninja(:database)
26
18
  end
27
19
 
28
- Then in your after your normal configuration in database.yml:
29
-
30
- database_development:
31
- adapter: postgresql
32
- database: database_name
33
- user: username
20
+ After your normal configuration in database.yml add a new group:
34
21
 
35
- database_test:
36
- adapter: postgresql
37
- database: database_name
38
- user: username
22
+ database:
23
+ development:
24
+ adapter: postgresql
25
+ database: database_name
26
+ user: username
39
27
 
40
- database_production:
41
- adapter: postgresql
42
- database: database_name
43
- user: username
28
+ test:
29
+ adapter: postgresql
30
+ database: database_name
31
+ user: username
44
32
 
45
- Connection ninja takes the database name you pass in and appends the environment on the end so it is important that you name the entries in database.yml as "#{database}_#{RAILS_ENV}" or it won't work.
33
+ production:
34
+ adapter: postgresql
35
+ database: database_name
36
+ user: username
46
37
 
47
38
  WHY?
48
39
  ===
49
40
 
50
- If you look at the code you will probably wonder why I didn't just call establish_connection "database_#{RAILS_ENV}", well at first I went about it differently, and thought I needed it. Now I realised maybe I don't, but it was a great exercise in learning how to write a gem and I learned a few things about ActiveRecord along the way which was great. It also gave me IMO a nice clean way to connect a bunch of models to the right database regardless of which of my apps the code was sitting in.
41
+ Pure laziness. Rather than putting together a hash you can just call a group with use_connection_ninja(:group)
51
42
 
52
43
  How I used it
53
44
  =============
@@ -63,4 +54,4 @@ So I could:
63
54
 
64
55
  class ModelFromOtherDB < OtherConnection
65
56
 
66
- end
57
+ end
data/Rakefile CHANGED
@@ -10,35 +10,19 @@ begin
10
10
  gem.email = "chris.herring.iphone@gmail.com"
11
11
  gem.homepage = "http://github.com/cherring/connection_ninja"
12
12
  gem.authors = ["Chris Herring"]
13
- gem.add_development_dependency "rspec", ">= 1.2.9"
13
+ gem.add_development_dependency "rspec", ">= 2.0.0.beta19"
14
14
  end
15
15
  Jeweler::GemcutterTasks.new
16
16
  rescue LoadError
17
17
  puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
18
  end
19
19
 
20
- require 'spec/rake/spectask'
21
- Spec::Rake::SpecTask.new(:spec) do |spec|
22
- spec.libs << 'lib' << 'spec'
23
- spec.spec_files = FileList['spec/**/*_spec.rb']
24
- end
25
-
26
- Spec::Rake::SpecTask.new(:rcov) do |spec|
27
- spec.libs << 'lib' << 'spec'
28
- spec.pattern = 'spec/**/*_spec.rb'
29
- spec.rcov = true
30
- end
31
-
32
- task :spec => :check_dependencies
33
-
34
- task :default => :spec
35
-
36
20
  require 'rake/rdoctask'
37
21
  Rake::RDocTask.new do |rdoc|
38
22
  version = File.exist?('VERSION') ? File.read('VERSION') : ""
39
23
 
40
24
  rdoc.rdoc_dir = 'rdoc'
41
- rdoc.title = "rangetastic #{version}"
25
+ rdoc.title = "connection ninja #{version}"
42
26
  rdoc.rdoc_files.include('README*')
43
27
  rdoc.rdoc_files.include('lib/**/*.rb')
44
28
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.5
1
+ 0.4.0
@@ -5,56 +5,56 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{connection_ninja}
8
- s.version = "0.3.5"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Chris Herring"]
12
- s.date = %q{2009-11-24}
12
+ s.date = %q{2010-09-01}
13
13
  s.description = %q{Handle multiple Database conenctions with ActiveRecord Models}
14
14
  s.email = %q{chris.herring.iphone@gmail.com}
15
15
  s.extra_rdoc_files = [
16
16
  "README.markdown"
17
17
  ]
18
18
  s.files = [
19
- ".gitignore",
19
+ ".autotest",
20
+ ".gitignore",
20
21
  "CHANGELOG",
21
22
  "MIT-License",
22
23
  "Manifest",
23
24
  "README.markdown",
24
25
  "Rakefile",
25
26
  "VERSION",
26
- "config/database.example.yml",
27
27
  "connection_ninja.gemspec",
28
28
  "lib/connection_ninja.rb",
29
+ "lib/connection_ninja/orms/active_record.rb",
30
+ "lib/connection_ninja/railtie.rb",
29
31
  "spec/connection_ninja_spec.rb",
32
+ "spec/fixtures/database.yml",
30
33
  "spec/fixtures/models.rb",
31
- "spec/fixtures/structure.sql",
32
- "spec/spec_helper.rb",
33
- "spec/test_helper.rb"
34
+ "spec/spec_helper.rb"
34
35
  ]
35
36
  s.homepage = %q{http://github.com/cherring/connection_ninja}
36
37
  s.rdoc_options = ["--charset=UTF-8"]
37
38
  s.require_paths = ["lib"]
38
- s.rubygems_version = %q{1.3.5}
39
+ s.rubygems_version = %q{1.3.7}
39
40
  s.summary = %q{Handle multiple Database conenctions with ActiveRecord Models}
40
41
  s.test_files = [
41
42
  "spec/connection_ninja_spec.rb",
42
43
  "spec/fixtures/models.rb",
43
- "spec/spec_helper.rb",
44
- "spec/test_helper.rb"
44
+ "spec/spec_helper.rb"
45
45
  ]
46
46
 
47
47
  if s.respond_to? :specification_version then
48
48
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
49
49
  s.specification_version = 3
50
50
 
51
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
52
- s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
51
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
52
+ s.add_development_dependency(%q<rspec>, [">= 2.0.0.beta19"])
53
53
  else
54
- s.add_dependency(%q<rspec>, [">= 1.2.9"])
54
+ s.add_dependency(%q<rspec>, [">= 2.0.0.beta19"])
55
55
  end
56
56
  else
57
- s.add_dependency(%q<rspec>, [">= 1.2.9"])
57
+ s.add_dependency(%q<rspec>, [">= 2.0.0.beta19"])
58
58
  end
59
59
  end
60
60
 
@@ -0,0 +1,21 @@
1
+ require 'active_record'
2
+ require 'connection_ninja'
3
+
4
+ module ConnectionNinja
5
+ module Orms
6
+ module ActiveRecord
7
+ def use_connection_ninja(config_group)
8
+ establish_connection ninja_config(config_group)
9
+ end
10
+
11
+ protected
12
+ def ninja_config(config_group)
13
+ begin
14
+ configurations[config_group.to_s][Rails.env]
15
+ rescue
16
+ raise ::ActiveRecord::AdapterNotFound, "connection ninja could not find the #{Rails.env} configuration for group \"#{config_group.to_s}\""
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ require 'connection_ninja'
2
+
3
+ module ConnectionNinja
4
+ class Railtie < Rails::Railtie
5
+ initializer "connection_ninja.active_record" do |app|
6
+ if defined? ::ActiveRecord
7
+ require 'connection_ninja/orms/active_record'
8
+ ActiveRecord::Base.send(:extend, ConnectionNinja::Orms::ActiveRecord)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,35 +1,6 @@
1
1
  module ConnectionNinja
2
-
3
- def self.included(base)
4
- base.extend ClassMethods
5
- end
6
-
7
- module ClassMethods
8
- #These are just here to use to run my specs, will work out how to fake these later
9
- # RAILS_ENV = "development"
10
- # RAILS_ROOT = File.dirname(__FILE__) + "/.."
11
- # ActiveRecord::Base.configurations = { 'alternate_development' => {
12
- # 'adapter' => 'postgresql',
13
- # 'database' => 'connection_ninja_alternate',
14
- # 'host' => 'localhost'
15
- # }
16
- # }
17
-
18
- def use_connection_ninja(config_name)
19
- connect_to_db(config(config_name))
20
- end
21
-
22
- def config(config)
23
- ActiveRecord::Base.configurations["#{config.to_s}_#{RAILS_ENV}"]
24
- end
25
-
26
- def connect_to_db(config)
27
- establish_connection(config)
28
- end
29
-
30
- end
31
2
  end
32
3
 
33
- ActiveRecord::Base.class_eval do
34
- include ConnectionNinja
35
- end
4
+ if defined?(::Rails::Railtie)
5
+ require 'connection_ninja/railtie'
6
+ end
@@ -1,46 +1,48 @@
1
1
  require 'spec/spec_helper'
2
- require 'connection_ninja'
3
- include ConnectionNinja
4
2
 
5
- describe Order do
6
- it "should be connected to the default database" do
7
- Order.connection.current_database.should == "connection_ninja"
3
+ describe ActiveRecord::Base, "methods" do
4
+ before do
5
+ ActiveRecord::Base.send(:extend, ConnectionNinja::Orms::ActiveRecord)
6
+ end
7
+
8
+ it "should have configurtions" do
9
+ ActiveRecord::Base.configurations.should_not == {}
10
+ end
11
+
12
+ it "should have connection ninja methods" do
13
+ ActiveRecord::Base.should respond_to(:use_connection_ninja)
8
14
  end
9
-
10
15
  end
11
16
 
12
- describe Customer do
13
- it "should be connected to the alternate database" do
14
- Customer.connection.current_database.should == "connection_ninja_alternate"
17
+ describe ConnectionNinja::Orms::ActiveRecord, "exception" do
18
+ it "should raise an error if connection group not in database.yml" do
19
+ lambda{ActiveRecord::Base.use_connection_ninja(:fial)}.should raise_error(::ActiveRecord::AdapterNotFound)
15
20
  end
16
-
17
- it "should respond to use_connection_ninja" do
18
- Customer.respond_to?(:use_connection_ninja).should == true
19
- end
20
21
  end
21
22
 
22
- describe Customer,"config" do
23
+ describe Customer do
24
+ before do
25
+ ActiveRecord::Base.send(:extend, ConnectionNinja::Orms::ActiveRecord)
26
+ @connection = Customer.establish_connection
27
+ end
23
28
 
24
- it "should return the database config" do
25
- Customer.config(:alternate).should == ActiveRecord::Base.configurations['alternate_development']
29
+ it "should be connected to the alternate database" do
30
+ Customer.connection.current_database.should == "ninja_one"
26
31
  end
27
32
  end
28
33
 
34
+ describe Order do
35
+ before do
36
+ ActiveRecord::Base.send(:extend, ConnectionNinja::Orms::ActiveRecord)
37
+ @connection = Order.send(:use_connection_ninja, :other)
38
+ end
29
39
 
30
- describe Customer,"use_connection_ninja" do
40
+ it "should return correct configuration" do
41
+ Order.send(:ninja_config, :other).should == {"username"=>"rails", "adapter"=>"postgresql", "database"=>"ninja_two", "host"=>"localhost", "password"=>"rails"}
42
+ end
31
43
 
32
- it "should call connect_to_db" do
33
- Customer.stub!(:config).and_return(ActiveRecord::Base.configurations['alternate_development'])
34
- Customer.should_receive(:connect_to_db).with(Customer.config)
35
- Customer.use_connection_ninja(:alternate)
44
+ it "should be connected to the default database" do
45
+ Order.connection.current_database.should == "ninja_two"
36
46
  end
37
-
38
47
  end
39
48
 
40
- describe Customer,"connect_to_db" do
41
-
42
- it "should call establish_connection" do
43
- Customer.should_receive(:establish_connection)
44
- Customer.connect_to_db('alternate_development')
45
- end
46
- end
@@ -0,0 +1,20 @@
1
+ development: &development
2
+ database: ninja_one
3
+ host: localhost
4
+ adapter: postgresql
5
+ username: rails
6
+ password: rails
7
+
8
+ test:
9
+ <<: *development
10
+
11
+ other:
12
+ development: &development
13
+ database: ninja_two
14
+ host: localhost
15
+ adapter: postgresql
16
+ username: rails
17
+ password: rails
18
+
19
+ test:
20
+ <<: *development
@@ -1,7 +1,2 @@
1
- class Order < ActiveRecord::Base
2
-
3
- end
4
-
5
- class Customer < ActiveRecord::Base
6
- use_connection_ninja(:alternate)
7
- end
1
+ class Order < ActiveRecord::Base; end
2
+ class Customer < ActiveRecord::Base; end
data/spec/spec_helper.rb CHANGED
@@ -1,28 +1,14 @@
1
- # Inspiration gained from Thinking Sphinx's test suite via Ryan Bigg's by_star test suite.
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rspec'
2
4
 
3
- $:.unshift File.dirname(__FILE__) + '/../lib'
5
+ require 'rails'
6
+ require 'active_record'
7
+ require 'connection_ninja/orms/active_record'
4
8
 
5
- require 'rubygems'
6
- require 'activerecord'
7
- require 'connection_ninja'
8
9
  require 'spec/fixtures/models'
9
- require 'spec/test_helper'
10
10
 
11
11
  FileUtils.mkdir_p "#{Dir.pwd}/tmp"
12
+ ActiveRecord::Base.logger = Logger.new(StringIO.new)
13
+ ActiveRecord::Base.configurations = YAML.load_file(File.join("spec", "fixtures", "database.yml"))
12
14
 
13
- ActiveRecord::Base.logger = Logger.new(StringIO.new)
14
-
15
- Spec::Runner.configure do |config|
16
- test = TestHelper.new
17
- test.setup_postgresql
18
- RAILS_ENV = "development"
19
- RAILS_ROOT = File.dirname(__FILE__) + "/.."
20
-
21
- # This is to simulate what would be loaded out of datbase.yml for ActiveRecord
22
- ActiveRecord::Base.configurations = { 'alternate_development' => {
23
- 'adapter' => 'postgresql',
24
- 'database' => 'connection_ninja_alternate',
25
- 'host' => 'localhost'
26
- }
27
- }
28
- end
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: connection_ninja
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ hash: 15
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 4
9
+ - 0
10
+ version: 0.4.0
5
11
  platform: ruby
6
12
  authors:
7
13
  - Chris Herring
@@ -9,19 +15,26 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2009-11-24 00:00:00 +11:00
18
+ date: 2010-09-01 00:00:00 +10:00
13
19
  default_executable:
14
20
  dependencies:
15
21
  - !ruby/object:Gem::Dependency
16
22
  name: rspec
17
- type: :development
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
20
26
  requirements:
21
27
  - - ">="
22
28
  - !ruby/object:Gem::Version
23
- version: 1.2.9
24
- version:
29
+ hash: -1967355497
30
+ segments:
31
+ - 2
32
+ - 0
33
+ - 0
34
+ - beta19
35
+ version: 2.0.0.beta19
36
+ type: :development
37
+ version_requirements: *id001
25
38
  description: Handle multiple Database conenctions with ActiveRecord Models
26
39
  email: chris.herring.iphone@gmail.com
27
40
  executables: []
@@ -31,6 +44,7 @@ extensions: []
31
44
  extra_rdoc_files:
32
45
  - README.markdown
33
46
  files:
47
+ - .autotest
34
48
  - .gitignore
35
49
  - CHANGELOG
36
50
  - MIT-License
@@ -38,14 +52,14 @@ files:
38
52
  - README.markdown
39
53
  - Rakefile
40
54
  - VERSION
41
- - config/database.example.yml
42
55
  - connection_ninja.gemspec
43
56
  - lib/connection_ninja.rb
57
+ - lib/connection_ninja/orms/active_record.rb
58
+ - lib/connection_ninja/railtie.rb
44
59
  - spec/connection_ninja_spec.rb
60
+ - spec/fixtures/database.yml
45
61
  - spec/fixtures/models.rb
46
- - spec/fixtures/structure.sql
47
62
  - spec/spec_helper.rb
48
- - spec/test_helper.rb
49
63
  has_rdoc: true
50
64
  homepage: http://github.com/cherring/connection_ninja
51
65
  licenses: []
@@ -56,21 +70,27 @@ rdoc_options:
56
70
  require_paths:
57
71
  - lib
58
72
  required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
59
74
  requirements:
60
75
  - - ">="
61
76
  - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
62
80
  version: "0"
63
- version:
64
81
  required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
65
83
  requirements:
66
84
  - - ">="
67
85
  - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
68
89
  version: "0"
69
- version:
70
90
  requirements: []
71
91
 
72
92
  rubyforge_project:
73
- rubygems_version: 1.3.5
93
+ rubygems_version: 1.3.7
74
94
  signing_key:
75
95
  specification_version: 3
76
96
  summary: Handle multiple Database conenctions with ActiveRecord Models
@@ -78,4 +98,3 @@ test_files:
78
98
  - spec/connection_ninja_spec.rb
79
99
  - spec/fixtures/models.rb
80
100
  - spec/spec_helper.rb
81
- - spec/test_helper.rb
@@ -1,6 +0,0 @@
1
- alternate_development:
2
- adapter: postgresql
3
- database: connection_ninja_alternate
4
- user: chris
5
- password:
6
- host: localhost
@@ -1,39 +0,0 @@
1
- DROP TABLE IF EXISTS orders;
2
-
3
- DROP SEQUENCE IF EXISTS orders_id_seq;
4
-
5
- CREATE SEQUENCE orders_id_seq
6
- START WITH 1
7
- INCREMENT BY 1
8
- NO MAXVALUE
9
- NO MINVALUE
10
- CACHE 1;
11
-
12
- CREATE TABLE orders (
13
- id integer NOT NULL default nextval('orders_id_seq'),
14
- ordered_on timestamp,
15
- fulfilled_on timestamp,
16
- shipped_on timestamp,
17
- created_at timestamp without time zone,
18
- updated_at timestamp without time zone
19
- );
20
-
21
- DROP TABLE IF EXISTS customers;
22
-
23
- DROP SEQUENCE IF EXISTS customers_id_seq;
24
-
25
- CREATE SEQUENCE customers_id_seq
26
- START WITH 1
27
- INCREMENT BY 1
28
- NO MAXVALUE
29
- NO MINVALUE
30
- CACHE 1;
31
-
32
- CREATE TABLE customers (
33
- id integer NOT NULL default nextval('customers_id_seq'),
34
- given_name char(255),
35
- family_name char(255),
36
- created_at timestamp without time zone,
37
- updated_at timestamp without time zone
38
- );
39
-
data/spec/test_helper.rb DELETED
@@ -1,42 +0,0 @@
1
- class TestHelper
2
- attr_accessor :host, :username, :password
3
- attr_reader :path
4
-
5
- def initialize
6
- @host = "localhost"
7
- @username = "chris" #Insert username for db here
8
- @password = ""
9
-
10
- @path = File.expand_path(File.dirname(__FILE__))
11
- end
12
-
13
- def setup_postgresql
14
- ActiveRecord::Base.establish_connection(
15
- :adapter => 'postgresql',
16
- :database => 'connection_ninja',
17
- :username => @username,
18
- :password => @password,
19
- :host => @host
20
- )
21
- ActiveRecord::Base.logger = Logger.new(File.open("tmp/activerecord.log", "a"))
22
-
23
- structure = File.open("spec/fixtures/structure.sql") { |f| f.read.chomp }
24
- structure.split(';').each { |table|
25
- ActiveRecord::Base.connection.execute table
26
- }
27
-
28
- 10.times do
29
- Order.create(:ordered_on => 2.weeks.ago)
30
- end
31
-
32
- 10.times do
33
- Order.create(:ordered_on => 3.weeks.ago, :fulfilled_on => 1.day.ago)
34
- end
35
-
36
- 5.times do
37
- Order.create(:ordered_on => 1.weeks.ago)
38
- end
39
-
40
- end
41
-
42
- end