active_table 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ puts <<-MESSAGE
2
+ This project uses multiple Gemfiles in subdirectories of ./gemfiles.
3
+ The rake tasks automatically install these bundles as necessary. See rake -T.
4
+ MESSAGE
5
+ exit 1
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ environments = %w[rspec1 rspec2]
5
+ major, minor, revision = RUBY_VERSION.split(".").map{|str| str.to_i }
6
+
7
+ in_environment = lambda do |environment, command|
8
+ sh %Q{export BUNDLE_GEMFILE="gemfiles/#{environment}/Gemfile"; bundle --quiet && bundle exec #{command}}
9
+ end
10
+
11
+ in_all_environments = lambda do |command|
12
+ environments.each do |environment|
13
+ puts "\n---#{environment}---\n"
14
+ in_environment.call(environment, command)
15
+ end
16
+ end
17
+
18
+ autotest_styles = {
19
+ :rspec1 => 'rspec',
20
+ :rspec2 => 'rspec2'
21
+ }
22
+
23
+ desc "Run all specs against Rspec 1 and 2"
24
+ task "spec" do
25
+ in_environment.call('rspec1', 'spec spec') if major == 1 && minor < 9
26
+ in_environment.call('rspec2', 'rspec spec')
27
+ end
28
+
29
+ namespace "autotest" do
30
+ environments.each do |environment|
31
+ desc "Run autotest in #{environment}"
32
+ task environment do
33
+ in_environment.call(environment, "autotest -s #{autotest_styles[environment.to_sym]}")
34
+ end
35
+ end
36
+ end
37
+
38
+ namespace "doc" do
39
+ desc "Generate README and preview in browser"
40
+ task "readme" do
41
+ sh "rdoc -c utf8 README.rdoc && open doc/files/README_rdoc.html"
42
+ end
43
+ end
44
+
45
+ task :default => :spec
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "active_table/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "active_table"
7
+ s.version = ActiveTable::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Case Commons LLC"]
10
+ s.email = ["casecommons-dev@googlegroups.com"]
11
+ s.homepage = "https://github.com/Casecommons/active_table"
12
+ s.summary = %q{Dynamically-populated ActiveRecord models based on static data}
13
+ s.description = s.summary
14
+
15
+ s.rubyforge_project = "active_table"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency 'activerecord', '>=2.3.5', '<4.0.0'
23
+ end
@@ -0,0 +1,9 @@
1
+ source :rubygems
2
+
3
+ group :test do
4
+ gem "autotest"
5
+ gem "autotest-rails-pure"
6
+ gem "autotest-growl"
7
+ gem "autotest-fsevent"
8
+ gem "sqlite3"
9
+ end
@@ -0,0 +1,5 @@
1
+ filename = File.join(File.dirname(__FILE__), '..', 'Gemfile.common')
2
+ eval(File.read(filename), binding, filename, 1)
3
+
4
+ gem "rspec", "~>1.0"
5
+ gem "activerecord", "~>2.3.5"
@@ -0,0 +1,6 @@
1
+ filename = File.join(File.dirname(__FILE__), '..', 'Gemfile.common')
2
+ eval(File.read(filename), binding, filename, 1)
3
+
4
+ gem "rspec", "~>2.0"
5
+ gem "activerecord", "~>3.0.0"
6
+
@@ -0,0 +1,9 @@
1
+ ActiveRecord::ConnectionAdapters::ConnectionPool.class_eval do
2
+ private
3
+ def new_connection
4
+ connection = ActiveRecord::Base.send(spec.adapter_method, spec.config)
5
+ ActiveTable::Base.create_tables(connection)
6
+ connection
7
+ end
8
+ end
9
+
@@ -0,0 +1,32 @@
1
+ module ActiveTable
2
+ class Base < ActiveRecord::Base
3
+ @@tables = {}
4
+ def self.create_table(name, options = {}, &block)
5
+ class_name = self.name
6
+ @@tables[class_name] = {:name => name, :options => options, :block => block, :rows => []}
7
+ end
8
+
9
+ def self.insert(params)
10
+ class_name = self.name
11
+ @@tables[class_name][:rows] << params
12
+ end
13
+
14
+ def self.create_tables(connection)
15
+ @@tables.each_value do |table|
16
+ connection.create_table table[:name], table[:options].merge(:temporary => true) do |t|
17
+ table[:block].call(t)
18
+ end
19
+
20
+ table[:rows].each do |row|
21
+ connection.execute generate_insert_sql_for_hash(connection, table[:name], row)
22
+ end
23
+ end
24
+ end
25
+
26
+ def self.generate_insert_sql_for_hash(connection, table_name, params)
27
+ keys = params.keys.map {|k| connection.quote(k.to_s)}.join(", ")
28
+ values = params.values.map {|v| connection.quote(v.to_s)}.join(", ")
29
+ "INSERT INTO #{connection.quote(table_name.to_s)} (#{keys}) VALUES (#{values})"
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveTable
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ require 'active_record'
2
+ require 'active_record/connection_adapters/abstract_adapter'
3
+ require 'active_record/connection_adapters/abstract/connection_pool_extensions'
4
+ require 'active_table/base'
@@ -0,0 +1,107 @@
1
+ require 'spec_helper'
2
+
3
+ describe "a temporary ActiveRecord model created with with_model" do
4
+ before(:each) do
5
+ @connection_pool = ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ":memory:")
6
+ end
7
+
8
+ after(:each) do
9
+ @connection_pool.disconnect!
10
+ end
11
+
12
+ it "should inherit from ActiveRecord::Base" do
13
+ ActiveTable::Base.superclass.should == ActiveRecord::Base
14
+ end
15
+
16
+ context "when a connection is opened" do
17
+ it "should create a table from its model definition" do
18
+ class TemporaryModel < ActiveTable::Base
19
+ create_table :temporary_models do |t|
20
+ t.string :name
21
+ end
22
+ end
23
+
24
+ connection = @connection_pool.connection
25
+
26
+ lambda {
27
+ connection.execute("SELECT * FROM temporary_models")
28
+ }.should_not raise_error
29
+ end
30
+
31
+ it "should create multiple tables at the same time" do
32
+ class TemporaryModel < ActiveTable::Base
33
+ create_table :temporary_models do |t|
34
+ t.string :name
35
+ end
36
+ end
37
+
38
+ class TemporaryModelTwo < ActiveTable::Base
39
+ create_table :temporary_models_two do |t|
40
+ t.string :name
41
+ end
42
+ end
43
+
44
+ connection = @connection_pool.connection
45
+
46
+ lambda {
47
+ connection.execute("SELECT * FROM temporary_models")
48
+ connection.execute("SELECT * FROM temporary_models_two")
49
+ }.should_not raise_error
50
+ end
51
+
52
+ it "should insert rows of data specified within the model" do
53
+ class TemporaryModel < ActiveTable::Base
54
+ create_table :temporary_models do |t|
55
+ t.string :name
56
+ end
57
+
58
+ insert :id => 1, :name => "foo"
59
+ insert :id => 4, :name => "baz"
60
+ end
61
+
62
+ connection = @connection_pool.connection
63
+
64
+ connection.execute("SELECT * FROM temporary_models").size.should == 2
65
+ connection.execute("SELECT * FROM temporary_models WHERE id = 1").first["name"].should == "foo"
66
+ connection.execute("SELECT * FROM temporary_models WHERE name = 'baz'").first["id"].should == 4
67
+ end
68
+ end
69
+
70
+ context "when inserting data" do
71
+ it "should handle apostrophes without blowing up" do
72
+ class TemporaryModel < ActiveTable::Base
73
+ create_table :temporary_models do |t|
74
+ t.string :name
75
+ end
76
+
77
+ insert :id => 1, :name => "foo's the boss?"
78
+ end
79
+
80
+ connection = @connection_pool.connection
81
+
82
+ connection.execute("SELECT * FROM temporary_models WHERE id = 1").first["name"].should == "foo's the boss?"
83
+ end
84
+ end
85
+
86
+ context "with multiple connections" do
87
+ it "should create a temporary table for each connection" do
88
+ class TemporaryModel < ActiveTable::Base
89
+ create_table :temporary_models do |t|
90
+ t.string :name
91
+ end
92
+ end
93
+
94
+ connections = []
95
+ 3.times do
96
+ connections << @connection_pool.checkout
97
+ end
98
+
99
+ connections.first.should_not == connections.last
100
+ connections.each do |connection|
101
+ lambda {
102
+ connection.execute("SELECT * FROM temporary_models")
103
+ }.should_not raise_error
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,15 @@
1
+ require "active_record"
2
+ require "active_table"
3
+
4
+ if defined?(RSpec)
5
+ # For RSpec 2 users.
6
+ RSpec.configure do |config|
7
+ config.extend ActiveTable
8
+ end
9
+ else
10
+ # For RSpec 1 users.
11
+ Spec::Runner.configure do |config|
12
+ config.extend ActiveTable
13
+ end
14
+ end
15
+
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_table
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Case Commons LLC
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-08 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activerecord
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 9
30
+ segments:
31
+ - 2
32
+ - 3
33
+ - 5
34
+ version: 2.3.5
35
+ - - <
36
+ - !ruby/object:Gem::Version
37
+ hash: 63
38
+ segments:
39
+ - 4
40
+ - 0
41
+ - 0
42
+ version: 4.0.0
43
+ type: :runtime
44
+ version_requirements: *id001
45
+ description: Dynamically-populated ActiveRecord models based on static data
46
+ email:
47
+ - casecommons-dev@googlegroups.com
48
+ executables: []
49
+
50
+ extensions: []
51
+
52
+ extra_rdoc_files: []
53
+
54
+ files:
55
+ - .gitignore
56
+ - Gemfile
57
+ - Rakefile
58
+ - active_table.gemspec
59
+ - gemfiles/Gemfile.common
60
+ - gemfiles/rspec1/Gemfile
61
+ - gemfiles/rspec2/Gemfile
62
+ - lib/active_record/connection_adapters/abstract/connection_pool_extensions.rb
63
+ - lib/active_table.rb
64
+ - lib/active_table/base.rb
65
+ - lib/active_table/version.rb
66
+ - spec/active_table_spec.rb
67
+ - spec/spec_helper.rb
68
+ has_rdoc: true
69
+ homepage: https://github.com/Casecommons/active_table
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options: []
74
+
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ hash: 3
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ hash: 3
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ requirements: []
96
+
97
+ rubyforge_project: active_table
98
+ rubygems_version: 1.3.7
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: Dynamically-populated ActiveRecord models based on static data
102
+ test_files:
103
+ - spec/active_table_spec.rb
104
+ - spec/spec_helper.rb