active_table 0.1.2 → 0.1.3
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/lib/active_table/base.rb +83 -58
- data/lib/active_table/version.rb +1 -1
- data/spec/active_table_spec.rb +51 -2
- metadata +29 -57
data/lib/active_table/base.rb
CHANGED
@@ -4,84 +4,109 @@ module ActiveTable
|
|
4
4
|
@@insert_statements = []
|
5
5
|
@@table_list = []
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
-
|
12
|
-
|
13
|
-
model_loaded!
|
17
|
+
self.reset_column_information
|
18
|
+
self.define_attribute_methods
|
14
19
|
end
|
15
20
|
|
16
|
-
|
17
|
-
|
18
|
-
|
21
|
+
def model_loaded!
|
22
|
+
return unless defined?(Rails)
|
23
|
+
Rails.active_table_loaded_model_names << self.name
|
24
|
+
end
|
19
25
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
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
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
end
|
37
|
+
def insert(params)
|
38
|
+
class_name = self.name
|
39
|
+
@table[:rows] << params
|
40
|
+
end
|
35
41
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
end
|
42
|
+
def table_list
|
43
|
+
@@table_list
|
44
|
+
end
|
40
45
|
|
41
|
-
|
42
|
-
|
43
|
-
|
46
|
+
def add_detected_file(file)
|
47
|
+
@@detected_files << file
|
48
|
+
end
|
44
49
|
|
45
|
-
|
46
|
-
|
47
|
-
|
50
|
+
def detected_files
|
51
|
+
@@detected_files
|
52
|
+
end
|
48
53
|
|
49
|
-
|
50
|
-
|
51
|
-
|
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
|
-
|
54
|
-
|
55
|
-
|
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
|
-
|
58
|
-
|
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
|
-
|
62
|
-
|
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
|
-
|
65
|
-
|
66
|
-
connection.
|
67
|
-
|
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
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
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
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
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
|
data/lib/active_table/version.rb
CHANGED
data/spec/active_table_spec.rb
CHANGED
@@ -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
|
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
|
-
|
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
|
-
|
19
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
83
|
-
|
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
|
-
|
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.
|
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
|