magic_multi_connections 1.0.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/CHANGELOG.txt +0 -0
- data/History.txt +3 -0
- data/Manifest.txt +32 -0
- data/README.txt +3 -0
- data/Rakefile +103 -0
- data/lib/ext_active_record/connection_specification.rb +24 -0
- data/lib/magic_multi_connections/connected.rb +29 -0
- data/lib/magic_multi_connections/module.rb +26 -0
- data/lib/magic_multi_connections/version.rb +9 -0
- data/lib/magic_multi_connections.rb +21 -0
- data/scripts/txt2html +67 -0
- data/scripts/txt2js +59 -0
- data/setup.rb +1585 -0
- data/test/connections/native_postgresql/connection.rb +20 -0
- data/test/fixtures/contact_repository.rb +3 -0
- data/test/fixtures/db_definitions/postgresql.sql +7 -0
- data/test/fixtures/people.yml +5 -0
- data/test/fixtures/person.rb +2 -0
- data/test/test_helper.rb +71 -0
- data/test/test_magic_multi_connections.rb +26 -0
- data/test/test_parent_module.rb +18 -0
- data/test/test_preexisting_module.rb +39 -0
- data/website/index.html +340 -0
- data/website/index.txt +223 -0
- data/website/javascripts/rounded_corners_lite.inc.js +285 -0
- data/website/stylesheets/screen.css +138 -0
- data/website/template.js +3 -0
- data/website/template.rhtml +53 -0
- data/website/version-raw.js +3 -0
- data/website/version-raw.txt +2 -0
- data/website/version.js +4 -0
- data/website/version.txt +3 -0
- metadata +80 -0
data/CHANGELOG.txt
ADDED
File without changes
|
data/History.txt
ADDED
data/Manifest.txt
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
CHANGELOG.txt
|
2
|
+
History.txt
|
3
|
+
Manifest.txt
|
4
|
+
README.txt
|
5
|
+
Rakefile
|
6
|
+
lib/ext_active_record/connection_specification.rb
|
7
|
+
lib/magic_multi_connections.rb
|
8
|
+
lib/magic_multi_connections/connected.rb
|
9
|
+
lib/magic_multi_connections/module.rb
|
10
|
+
lib/magic_multi_connections/version.rb
|
11
|
+
scripts/txt2html
|
12
|
+
scripts/txt2js
|
13
|
+
setup.rb
|
14
|
+
test/connections/native_postgresql/connection.rb
|
15
|
+
test/fixtures/contact_repository.rb
|
16
|
+
test/fixtures/db_definitions/postgresql.sql
|
17
|
+
test/fixtures/people.yml
|
18
|
+
test/fixtures/person.rb
|
19
|
+
test/test_helper.rb
|
20
|
+
test/test_magic_multi_connections.rb
|
21
|
+
test/test_parent_module.rb
|
22
|
+
test/test_preexisting_module.rb
|
23
|
+
website/index.html
|
24
|
+
website/index.txt
|
25
|
+
website/javascripts/rounded_corners_lite.inc.js
|
26
|
+
website/stylesheets/screen.css
|
27
|
+
website/template.js
|
28
|
+
website/template.rhtml
|
29
|
+
website/version-raw.js
|
30
|
+
website/version-raw.txt
|
31
|
+
website/version.js
|
32
|
+
website/version.txt
|
data/README.txt
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/clean'
|
4
|
+
require 'rake/testtask'
|
5
|
+
require 'rake/packagetask'
|
6
|
+
require 'rake/gempackagetask'
|
7
|
+
require 'rake/rdoctask'
|
8
|
+
require 'rake/contrib/rubyforgepublisher'
|
9
|
+
require 'fileutils'
|
10
|
+
require 'hoe'
|
11
|
+
include FileUtils
|
12
|
+
require File.join(File.dirname(__FILE__), 'lib', 'magic_multi_connections', 'version')
|
13
|
+
|
14
|
+
AUTHOR = "nicwilliams" # can also be an array of Authors
|
15
|
+
EMAIL = "drnicwilliams@gmail.com"
|
16
|
+
DESCRIPTION = "Your ActiveRecord classes can be accessed within multiple modules, each which targets a different DB connection"
|
17
|
+
GEM_NAME = "magic_multi_connections" # what ppl will type to install your gem
|
18
|
+
RUBYFORGE_PROJECT = "magicmodels" # The unix name for your project
|
19
|
+
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
|
20
|
+
|
21
|
+
|
22
|
+
NAME = "magic_multi_connections"
|
23
|
+
REV = nil # UNCOMMENT IF REQUIRED: File.read(".svn/entries")[/committed-rev="(d+)"/, 1] rescue nil
|
24
|
+
VERS = ENV['VERSION'] || (MagicMultiConnection::VERSION::STRING + (REV ? ".#{REV}" : ""))
|
25
|
+
CLEAN.include ['**/.*.sw?', '*.gem', '.config']
|
26
|
+
RDOC_OPTS = ['--quiet', '--title', "magic_multi_connections documentation",
|
27
|
+
"--opname", "index.html",
|
28
|
+
"--line-numbers",
|
29
|
+
"--main", "README",
|
30
|
+
"--inline-source"]
|
31
|
+
|
32
|
+
class Hoe
|
33
|
+
def extra_deps
|
34
|
+
@extra_deps.reject { |x| Array(x).first == 'hoe' }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# Generate all the Rake tasks
|
39
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
40
|
+
hoe = Hoe.new(GEM_NAME, VERS) do |p|
|
41
|
+
p.author = AUTHOR
|
42
|
+
p.description = DESCRIPTION
|
43
|
+
p.email = EMAIL
|
44
|
+
p.summary = DESCRIPTION
|
45
|
+
p.url = HOMEPATH
|
46
|
+
p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
|
47
|
+
p.test_globs = ["test/**/test_*.rb"]
|
48
|
+
p.clean_globs = CLEAN #An array of file patterns to delete on clean.
|
49
|
+
|
50
|
+
# == Optional
|
51
|
+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
52
|
+
#p.extra_deps - An array of rubygem dependencies.
|
53
|
+
#p.spec_extras - A hash of extra values to set in the gemspec.
|
54
|
+
end
|
55
|
+
|
56
|
+
for adapter in %w( mysql sqlite oracle postgresql ) # UNTESTED - firebird sqlserver sqlserver_odbc db2 sybase openbase )
|
57
|
+
Rake::TestTask.new("test_#{adapter}") { |t|
|
58
|
+
t.libs << "test" << "test/connections/native_#{adapter}"
|
59
|
+
t.pattern = "test/test_*.rb"
|
60
|
+
t.verbose = true
|
61
|
+
}
|
62
|
+
end
|
63
|
+
|
64
|
+
SCHEMA_PATH = File.join(File.dirname(__FILE__), *%w(test fixtures db_definitions))
|
65
|
+
|
66
|
+
|
67
|
+
desc 'Build the PostgreSQL test databases'
|
68
|
+
task :build_postgresql_databases do
|
69
|
+
sh %{ createdb "#{GEM_NAME}_unittest" }
|
70
|
+
sh %{ psql "#{GEM_NAME}_unittest" -f #{File.join(SCHEMA_PATH, 'postgresql.sql')} }
|
71
|
+
sh %{ createdb "#{GEM_NAME}_extra_unittest" }
|
72
|
+
sh %{ psql "#{GEM_NAME}_extra_unittest" -f #{File.join(SCHEMA_PATH, 'postgresql.sql')} }
|
73
|
+
end
|
74
|
+
|
75
|
+
desc 'Drop the PostgreSQL test databases'
|
76
|
+
task :drop_postgresql_databases do
|
77
|
+
sh %{ dropdb "#{GEM_NAME}_unittest" }
|
78
|
+
sh %{ dropdb "#{GEM_NAME}_extra_unittest" }
|
79
|
+
end
|
80
|
+
|
81
|
+
desc 'Rebuild the PostgreSQL test databases'
|
82
|
+
task :rebuild_postgresql_databases => [:drop_postgresql_databases, :build_postgresql_databases]
|
83
|
+
|
84
|
+
|
85
|
+
desc 'Generate website files'
|
86
|
+
task :website_generate do
|
87
|
+
sh %{ ruby scripts/txt2html website/index.txt > website/index.html }
|
88
|
+
sh %{ ruby scripts/txt2js website/version.txt > website/version.js }
|
89
|
+
sh %{ ruby scripts/txt2js website/version-raw.txt > website/version-raw.js }
|
90
|
+
end
|
91
|
+
|
92
|
+
desc 'Upload website files to rubyforge'
|
93
|
+
task :website_upload do
|
94
|
+
config = YAML.load(File.read(File.expand_path("~/.rubyforge/user-config.yml")))
|
95
|
+
host = "#{config["username"]}@rubyforge.org"
|
96
|
+
remote_dir = "/var/www/gforge-projects/#{RUBYFORGE_PROJECT}/#{GEM_NAME}/"
|
97
|
+
local_dir = 'website'
|
98
|
+
sh %{rsync -av --delete #{local_dir}/ #{host}:#{remote_dir}}
|
99
|
+
end
|
100
|
+
|
101
|
+
desc 'Generate and upload website files'
|
102
|
+
task :website => [:website_generate, :website_upload] do
|
103
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
class Base
|
3
|
+
|
4
|
+
class << self
|
5
|
+
|
6
|
+
def active_connection_name #:nodoc:
|
7
|
+
@active_connection_name ||=
|
8
|
+
if active_connections[name] || @@defined_connections[name]
|
9
|
+
name
|
10
|
+
elsif self == ActiveRecord::Base
|
11
|
+
nil
|
12
|
+
elsif self.parent_module.ancestors.include?(MagicMultiConnection::Connected)
|
13
|
+
self.parent_module.establish_connection_on self
|
14
|
+
self.active_connection_name
|
15
|
+
else
|
16
|
+
superclass.active_connection_name
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module MagicMultiConnection::Connected
|
2
|
+
def self.included(base)
|
3
|
+
base.instance_eval <<-EOS
|
4
|
+
alias :pre_connected_const_missing :const_missing
|
5
|
+
|
6
|
+
def const_missing(const_id)
|
7
|
+
return pre_connected_const_missing(const_id) rescue nil
|
8
|
+
target_class = "::\#{const_id}".constantize rescue nil
|
9
|
+
raise NameError.new("uninitialized constant \#{const_id}") unless target_class
|
10
|
+
klass = create_class const_id, target_class
|
11
|
+
klass.establish_connection self.connection_spec
|
12
|
+
klass
|
13
|
+
end
|
14
|
+
|
15
|
+
def create_class(class_name, superclass = Object, &block)
|
16
|
+
klass = Class.new superclass, &block
|
17
|
+
self.const_set class_name, klass
|
18
|
+
end
|
19
|
+
EOS
|
20
|
+
base.extend(ClassMethods)
|
21
|
+
end
|
22
|
+
|
23
|
+
module ClassMethods
|
24
|
+
def establish_connection_on(klass)
|
25
|
+
klass.establish_connection self.connection_spec
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class Module
|
2
|
+
attr_accessor :connection_spec
|
3
|
+
|
4
|
+
def establish_connection(connection_spec)
|
5
|
+
include MagicMultiConnection::Connected
|
6
|
+
instance_variable_set '@connection_spec', connection_spec
|
7
|
+
update_active_records
|
8
|
+
end
|
9
|
+
|
10
|
+
def parent_module
|
11
|
+
parent = self.name.split('::')[0..-2].join('::')
|
12
|
+
parent = 'Object' if parent.blank?
|
13
|
+
parent.constantize
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
def update_active_records
|
18
|
+
self.constants.each do |c_str|
|
19
|
+
c = "#{self.name}::#{c_str}".constantize
|
20
|
+
next unless c.is_a? Class
|
21
|
+
next unless c.new.is_a? ActiveRecord::Base
|
22
|
+
c.establish_connection connection_spec
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
unless defined?(ActiveSupport) && defined?(ActiveRecord)
|
5
|
+
begin
|
6
|
+
require 'active_support'
|
7
|
+
require 'active_record'
|
8
|
+
rescue LoadError
|
9
|
+
require 'rubygems'
|
10
|
+
require 'active_support'
|
11
|
+
require 'active_record'
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module MagicMultiConnection
|
16
|
+
end
|
17
|
+
|
18
|
+
require 'magic_multi_connections/version'
|
19
|
+
require 'magic_multi_connections/connected'
|
20
|
+
require 'magic_multi_connections/module'
|
21
|
+
require 'ext_active_record/connection_specification'
|
data/scripts/txt2html
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'redcloth'
|
5
|
+
require 'syntax/convertors/html'
|
6
|
+
require 'erb'
|
7
|
+
require File.dirname(__FILE__) + '/../lib/magic_multi_connections/version.rb'
|
8
|
+
|
9
|
+
version = MagicMultiConnection::VERSION::STRING
|
10
|
+
download = 'http://rubyforge.org/projects/magicmodels'
|
11
|
+
|
12
|
+
class Fixnum
|
13
|
+
def ordinal
|
14
|
+
# teens
|
15
|
+
return 'th' if (10..19).include?(self % 100)
|
16
|
+
# others
|
17
|
+
case self % 10
|
18
|
+
when 1: return 'st'
|
19
|
+
when 2: return 'nd'
|
20
|
+
when 3: return 'rd'
|
21
|
+
else return 'th'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Time
|
27
|
+
def pretty
|
28
|
+
return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def convert_syntax(syntax, source)
|
33
|
+
return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
|
34
|
+
end
|
35
|
+
|
36
|
+
if ARGV.length >= 1
|
37
|
+
src, template = ARGV
|
38
|
+
template ||= File.dirname(__FILE__) + '/../website/template.rhtml'
|
39
|
+
|
40
|
+
else
|
41
|
+
puts("Usage: #{File.split($0).last} source.txt [template.rhtml] > output.html")
|
42
|
+
exit!
|
43
|
+
end
|
44
|
+
|
45
|
+
template = ERB.new(File.open(template).read)
|
46
|
+
|
47
|
+
title = nil
|
48
|
+
body = nil
|
49
|
+
File.open(src) do |fsrc|
|
50
|
+
title_text = fsrc.readline
|
51
|
+
body_text = fsrc.read
|
52
|
+
syntax_items = []
|
53
|
+
body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</\1>!m){
|
54
|
+
ident = syntax_items.length
|
55
|
+
element, syntax, source = $1, $2, $3
|
56
|
+
syntax_items << "<#{element} class=\"syntax\">#{convert_syntax(syntax, source)}</#{element}>"
|
57
|
+
"syntax-temp-#{ident}"
|
58
|
+
}
|
59
|
+
title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
|
60
|
+
body = RedCloth.new(body_text).to_html
|
61
|
+
body.gsub!(%r!(?:<pre><code>)?syntax-temp-(\d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
|
62
|
+
end
|
63
|
+
stat = File.stat(src)
|
64
|
+
created = stat.ctime
|
65
|
+
modified = stat.mtime
|
66
|
+
|
67
|
+
$stdout << template.result(binding)
|
data/scripts/txt2js
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'redcloth'
|
5
|
+
require 'syntax/convertors/html'
|
6
|
+
require 'erb'
|
7
|
+
require 'active_support'
|
8
|
+
require File.dirname(__FILE__) + '/../lib/magic_multi_connections/version.rb'
|
9
|
+
|
10
|
+
version = MagicMultiConnection::VERSION::STRING
|
11
|
+
download = 'http://rubyforge.org/projects/compositekeys'
|
12
|
+
|
13
|
+
class Fixnum
|
14
|
+
def ordinal
|
15
|
+
# teens
|
16
|
+
return 'th' if (10..19).include?(self % 100)
|
17
|
+
# others
|
18
|
+
case self % 10
|
19
|
+
when 1: return 'st'
|
20
|
+
when 2: return 'nd'
|
21
|
+
when 3: return 'rd'
|
22
|
+
else return 'th'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Time
|
28
|
+
def pretty
|
29
|
+
return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def convert_syntax(syntax, source)
|
34
|
+
return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
|
35
|
+
end
|
36
|
+
|
37
|
+
if ARGV.length >= 1
|
38
|
+
src, template = ARGV
|
39
|
+
template ||= File.dirname(__FILE__) + '/../website/template.js'
|
40
|
+
else
|
41
|
+
puts("Usage: #{File.split($0).last} source.txt [template.js] > output.html")
|
42
|
+
exit!
|
43
|
+
end
|
44
|
+
|
45
|
+
template = ERB.new(File.open(template).read)
|
46
|
+
|
47
|
+
title = nil
|
48
|
+
body = nil
|
49
|
+
File.open(src) do |fsrc|
|
50
|
+
title_text = fsrc.readline
|
51
|
+
body_text = fsrc.read
|
52
|
+
title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
|
53
|
+
body = RedCloth.new(body_text)
|
54
|
+
end
|
55
|
+
stat = File.stat(src)
|
56
|
+
created = stat.ctime
|
57
|
+
modified = stat.mtime
|
58
|
+
|
59
|
+
$stdout << template.result(binding)
|