active_table 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,84 +4,109 @@ module ActiveTable
4
4
  @@insert_statements = []
5
5
  @@table_list = []
6
6
 
7
- def self.active_table(&block)
8
- @table = {}
9
- block.call
7
+ class << self
8
+ def active_table(&block)
9
+ @table = {}
10
+ block.call
11
+
12
+ unless model_loaded?
13
+ create_table!
14
+ model_loaded!
15
+ end
10
16
 
11
- unless model_loaded?
12
- create_table!
13
- model_loaded!
17
+ self.reset_column_information
18
+ self.define_attribute_methods
14
19
  end
15
20
 
16
- self.reset_column_information
17
- self.define_attribute_methods
18
- end
21
+ def model_loaded!
22
+ return unless defined?(Rails)
23
+ Rails.active_table_loaded_model_names << self.name
24
+ end
19
25
 
20
- def self.model_loaded!
21
- return unless defined?(Rails)
22
- Rails.active_table_loaded_model_names << self.name
23
- end
26
+ def model_loaded?
27
+ return false unless defined?(Rails)
28
+ Rails.active_table_loaded_model_names.include?(self.name)
29
+ end
24
30
 
25
- def self.model_loaded?
26
- return false unless defined?(Rails)
27
- Rails.active_table_loaded_model_names.include?(self.name)
28
- end
31
+ def create_table(name, options = {}, &block)
32
+ class_name = self.name
33
+ self.set_table_name name
34
+ @table = {:name => name, :options => options, :block => block, :rows => []}
35
+ end
29
36
 
30
- def self.create_table(name, options = {}, &block)
31
- class_name = self.name
32
- self.set_table_name name
33
- @table = {:name => name, :options => options, :block => block, :rows => []}
34
- end
37
+ def insert(params)
38
+ class_name = self.name
39
+ @table[:rows] << params
40
+ end
35
41
 
36
- def self.insert(params)
37
- class_name = self.name
38
- @table[:rows] << params
39
- end
42
+ def table_list
43
+ @@table_list
44
+ end
40
45
 
41
- def self.table_list
42
- @@table_list
43
- end
46
+ def add_detected_file(file)
47
+ @@detected_files << file
48
+ end
44
49
 
45
- def self.add_detected_file(file)
46
- @@detected_files << file
47
- end
50
+ def detected_files
51
+ @@detected_files
52
+ end
48
53
 
49
- def self.detected_files
50
- @@detected_files
51
- end
54
+ def create_table!
55
+ temp_table_name = "#{@table[:name]}__temp"
56
+
57
+ unless connection.table_exists?(temp_table_name)
58
+ db_drop_and_create_table(temp_table_name, @table[:options], @table[:block])
52
59
 
53
- def self.create_table!
54
- connection = ActiveRecord::Base.connection
55
- connection.execute "DROP TABLE IF EXISTS #{connection.quote_table_name(@table[:name])}"
60
+ unless tables_match?(@table[:name], temp_table_name)
61
+ db_drop_table(@table[:name])
62
+ connection.rename_table(temp_table_name, @table[:name])
56
63
 
57
- connection.create_table @table[:name], @table[:options] do |t|
58
- @table[:block].call(t)
64
+ @@insert_statements.reject!{|statement| statement[:table_name] == @table[:name]}
65
+ @@table_list << @table[:name] unless @@table_list.include?(@table[:name])
66
+
67
+ @table[:rows].each do |row|
68
+ insert_sql = generate_insert_sql_for_hash(connection, @table[:name], row)
69
+ connection.execute insert_sql
70
+ @@insert_statements << {:table_name => @table[:name], :sql => insert_sql}
71
+ end
72
+ else
73
+ db_drop_table(temp_table_name)
74
+ end
75
+ end
59
76
  end
60
77
 
61
- @@insert_statements.reject!{|statement| statement[:table_name] == @table[:name]}
62
- @@table_list << @table[:name] unless @@table_list.include?(@table[:name])
78
+ def tables_match?(table_1, table_2)
79
+ return false unless connection.table_exists?(table_1) and connection.table_exists?(table_2)
80
+ columns_1 = connection.columns(table_1)
81
+ columns_2 = connection.columns(table_2)
82
+ columns_1.to_json == columns_2.to_json
83
+ end
63
84
 
64
- @table[:rows].each do |row|
65
- insert_sql = generate_insert_sql_for_hash(connection, @table[:name], row)
66
- connection.execute insert_sql
67
- @@insert_statements << {:table_name => @table[:name], :sql => insert_sql}
85
+ def db_drop_and_create_table(name, options, block)
86
+ db_drop_table(name)
87
+ connection.create_table name, options do |t|
88
+ block.call(t)
89
+ end
90
+ end
91
+
92
+ def db_drop_table(name)
93
+ connection.execute "DROP TABLE IF EXISTS #{connection.quote_table_name(name)}"
68
94
  end
69
- end
70
95
 
71
- def self.seed!
72
- connection = ActiveRecord::Base.connection
73
- @@insert_statements.each do |insert|
74
- begin
75
- connection.execute(insert[:sql])
76
- rescue ActiveRecord::RecordNotUnique
96
+ def seed!
97
+ @@insert_statements.each do |insert|
98
+ begin
99
+ connection.execute(insert[:sql])
100
+ rescue ActiveRecord::RecordNotUnique
101
+ end
77
102
  end
78
103
  end
79
- end
80
104
 
81
- def self.generate_insert_sql_for_hash(connection, table_name, params)
82
- keys = params.keys.map {|k| connection.quote_column_name(k.to_s)}.join(", ")
83
- values = params.values.map {|v| connection.quote(v.to_s)}.join(", ")
84
- "INSERT INTO #{connection.quote_table_name(table_name.to_s)} (#{keys}) VALUES (#{values})"
105
+ def generate_insert_sql_for_hash(connection, table_name, params)
106
+ keys = params.keys.map {|k| connection.quote_column_name(k.to_s)}.join(", ")
107
+ values = params.values.map {|v| connection.quote(v.to_s)}.join(", ")
108
+ "INSERT INTO #{connection.quote_table_name(table_name.to_s)} (#{keys}) VALUES (#{values})"
109
+ end
85
110
  end
86
111
  end
87
112
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveTable
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -15,7 +15,6 @@ describe "a model created with active_table" do
15
15
  end
16
16
 
17
17
  it "should behave like an ActiveRecord model" do
18
-
19
18
  class AwesomeModel < ActiveTable::Base
20
19
  active_table do
21
20
  create_table :awesome_models do |t|
@@ -45,6 +44,56 @@ describe "a model created with active_table" do
45
44
  end
46
45
  }.should_not raise_error
47
46
  end
47
+
48
+ it "should not drop and recreate a table if the table definition is unchanged" do
49
+ class AwesomeModel < ActiveTable::Base
50
+ active_table do
51
+ self.table_name = "awesome_models"
52
+ create_table :awesome_models do |t|
53
+ t.string :name
54
+ end
55
+
56
+ insert :id => 1, :name => "foo"
57
+ end
58
+ end
59
+
60
+ class MoreAwesomeModel < ActiveTable::Base
61
+ active_table do
62
+ self.table_name = "awesome_models"
63
+ create_table :awesome_models do |t|
64
+ t.string :name
65
+ end
66
+ end
67
+ end
68
+
69
+ @connection.execute("SELECT * FROM awesome_models").size.should == 1
70
+ end
71
+
72
+ it "should drop and recreate a table if the table definition is changed" do
73
+ class AwesomeModel < ActiveTable::Base
74
+ active_table do
75
+ self.table_name = "awesome_models"
76
+ create_table :awesome_models do |t|
77
+ t.string :name
78
+ end
79
+
80
+ insert :id => 1, :name => "foo"
81
+ end
82
+ end
83
+
84
+ class MoreAwesomeModel < ActiveTable::Base
85
+ active_table do
86
+ self.table_name = "awesome_models"
87
+ create_table :awesome_models do |t|
88
+ t.string :different_name
89
+ end
90
+ end
91
+ end
92
+
93
+ @connection.execute("SELECT * FROM awesome_models").size.should == 0
94
+ lambda { @connection.execute("SELECT name FROM awesome_models") }.should raise_error
95
+ lambda { @connection.execute("SELECT different_name FROM awesome_models") }.should_not raise_error
96
+ end
48
97
  end
49
98
 
50
99
  context "#table_list" do
@@ -101,7 +150,7 @@ describe "a model created with active_table" do
101
150
  end
102
151
  end
103
152
 
104
- context "when a connection is opened" do
153
+ context "when an active_table block is encountered" do
105
154
  it "should create a table from its model definition" do
106
155
  class AwesomeModel < ActiveTable::Base
107
156
  active_table do
metadata CHANGED
@@ -1,56 +1,37 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: active_table
3
- version: !ruby/object:Gem::Version
4
- hash: 31
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 1
9
- - 2
10
- version: 0.1.2
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Case Commons LLC
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-07-12 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2011-08-15 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: activerecord
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &2165817320 !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 9
29
- segments:
30
- - 2
31
- - 3
32
- - 5
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
33
21
  version: 2.3.5
34
22
  - - <
35
- - !ruby/object:Gem::Version
36
- hash: 63
37
- segments:
38
- - 4
39
- - 0
40
- - 0
23
+ - !ruby/object:Gem::Version
41
24
  version: 4.0.0
42
25
  type: :runtime
43
- version_requirements: *id001
26
+ prerelease: false
27
+ version_requirements: *2165817320
44
28
  description: Dynamically-populated ActiveRecord models based on static data
45
- email:
29
+ email:
46
30
  - casecommons-dev@googlegroups.com
47
31
  executables: []
48
-
49
32
  extensions: []
50
-
51
33
  extra_rdoc_files: []
52
-
53
- files:
34
+ files:
54
35
  - .gitignore
55
36
  - Gemfile
56
37
  - README.md
@@ -68,37 +49,28 @@ files:
68
49
  - spec/spec_helper.rb
69
50
  homepage: https://github.com/Casecommons/active_table
70
51
  licenses: []
71
-
72
52
  post_install_message:
73
53
  rdoc_options: []
74
-
75
- require_paths:
54
+ require_paths:
76
55
  - lib
77
- required_ruby_version: !ruby/object:Gem::Requirement
56
+ required_ruby_version: !ruby/object:Gem::Requirement
78
57
  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
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
63
  none: false
88
- requirements:
89
- - - ">="
90
- - !ruby/object:Gem::Version
91
- hash: 3
92
- segments:
93
- - 0
94
- version: "0"
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
95
68
  requirements: []
96
-
97
69
  rubyforge_project: active_table
98
- rubygems_version: 1.8.5
70
+ rubygems_version: 1.8.6
99
71
  signing_key:
100
72
  specification_version: 3
101
73
  summary: Dynamically-populated ActiveRecord models based on static data
102
- test_files:
74
+ test_files:
103
75
  - spec/active_table_spec.rb
104
76
  - spec/spec_helper.rb