arc 0.0.2.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/arc.gemspec +1 -0
- data/lib/arc.rb +1 -2
- data/lib/arc/casting.rb +19 -14
- data/lib/arc/data_stores/abstract/store.rb +35 -0
- data/lib/arc/data_stores/mysql/store.rb +16 -1
- data/lib/arc/data_stores/postgres/store.rb +22 -2
- data/lib/arc/data_stores/sqlite/object_definitions.rb +1 -0
- data/lib/arc/data_stores/sqlite/store.rb +6 -1
- data/lib/arc/quoting.rb +2 -0
- data/lib/arc/version.rb +1 -1
- data/spec/data_stores/abstract/store_spec.rb +105 -0
- data/spec/data_stores/integration/crud_spec.rb +23 -12
- data/spec/data_stores/mysql/store_spec.rb +41 -8
- data/spec/data_stores/postgres/store_spec.rb +23 -2
- data/spec/quoting_spec.rb +8 -4
- data/spec/spec_helper.rb +1 -1
- data/spec/support/resources/out/superman.gif +0 -0
- data/spec/support/schemas/mysql.sql +2 -2
- data/spec/support/seed.rb +22 -5
- metadata +29 -16
data/arc.gemspec
CHANGED
data/lib/arc.rb
CHANGED
data/lib/arc/casting.rb
CHANGED
@@ -3,20 +3,22 @@ module Arc
|
|
3
3
|
class CannotCastValueError < StandardError; end
|
4
4
|
|
5
5
|
TYPES = {
|
6
|
-
integer:
|
7
|
-
int:
|
8
|
-
char:
|
9
|
-
varchar:
|
10
|
-
binary:
|
11
|
-
date:
|
12
|
-
datetime:
|
13
|
-
time:
|
14
|
-
bool:
|
15
|
-
boolean:
|
16
|
-
bit:
|
17
|
-
float:
|
18
|
-
text:
|
19
|
-
bytea:
|
6
|
+
integer: 'integer',
|
7
|
+
int: 'integer',
|
8
|
+
char: 'string' ,
|
9
|
+
varchar: 'string' ,
|
10
|
+
binary: 'binary' ,
|
11
|
+
date: 'date' ,
|
12
|
+
datetime: 'time' ,
|
13
|
+
time: 'time' ,
|
14
|
+
bool: 'boolean',
|
15
|
+
boolean: 'boolean',
|
16
|
+
bit: 'boolean',
|
17
|
+
float: 'float' ,
|
18
|
+
text: 'string' ,
|
19
|
+
bytea: 'binary' ,
|
20
|
+
timestamp: 'timestamp',
|
21
|
+
blob: 'binary'
|
20
22
|
}
|
21
23
|
|
22
24
|
CAST_METHODS = Hash.new do |cast_methods, key|
|
@@ -41,9 +43,12 @@ module Arc
|
|
41
43
|
value.to_f
|
42
44
|
end
|
43
45
|
def cast_time value
|
46
|
+
return value if value.is_a? Time
|
44
47
|
Time.parse value
|
45
48
|
end
|
49
|
+
alias :cast_timestamp :cast_time
|
46
50
|
def cast_date value
|
51
|
+
return value if value.is_a? Date
|
47
52
|
Date.parse value
|
48
53
|
end
|
49
54
|
def cast_binary value
|
@@ -8,6 +8,7 @@ module Arc
|
|
8
8
|
class AbstractDataStore < ResourcePool
|
9
9
|
include ArelCompatibility
|
10
10
|
include Arc::Quoting
|
11
|
+
include Arc::Casting
|
11
12
|
|
12
13
|
def [] table
|
13
14
|
schema[table]
|
@@ -43,6 +44,40 @@ module Arc
|
|
43
44
|
end
|
44
45
|
alias :with_store :with_resource
|
45
46
|
alias :columns :[]
|
47
|
+
|
48
|
+
def type_mappings_for query
|
49
|
+
type_mappings = {}
|
50
|
+
query.instance_variable_get(:@ctx).projections.each do |projection|
|
51
|
+
is_alias = projection.respond_to?(:right)
|
52
|
+
relation = is_alias ? projection.left.relation : projection.relation
|
53
|
+
|
54
|
+
root_projection = is_alias ? projection.left : projection
|
55
|
+
relation_is_alias = root_projection.relation.respond_to?(:left)
|
56
|
+
|
57
|
+
root_relation = relation_is_alias ? relation.left : relation
|
58
|
+
|
59
|
+
table_name = root_relation.name
|
60
|
+
result_column_name = is_alias ? projection.right.to_sym : projection.name
|
61
|
+
table_column_name = root_projection.name
|
62
|
+
|
63
|
+
table = Arel::Table.engine[table_name.to_sym]
|
64
|
+
column = table[table_column_name.to_sym]
|
65
|
+
|
66
|
+
type_mappings[result_column_name] = column.type.to_sym
|
67
|
+
end
|
68
|
+
type_mappings
|
69
|
+
end
|
70
|
+
|
71
|
+
def result_for query
|
72
|
+
mappings = type_mappings_for query
|
73
|
+
rows = read(query.to_sql)
|
74
|
+
return Array.new(rows.size) do |index|
|
75
|
+
Hash.new do |hash, key|
|
76
|
+
hash[key] = cast rows[index][key], mappings[key]
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
46
81
|
private
|
47
82
|
#better semantics for a class that deals with 'connections' instead of 'resources'
|
48
83
|
def create_connection
|
@@ -9,7 +9,12 @@ module Arc::DataStores
|
|
9
9
|
class MysqlDataStore < AbstractDataStore
|
10
10
|
|
11
11
|
def read query
|
12
|
-
|
12
|
+
case query
|
13
|
+
when String
|
14
|
+
execute(query).entries
|
15
|
+
when Arel::SelectManager
|
16
|
+
result_for query
|
17
|
+
end
|
13
18
|
end
|
14
19
|
|
15
20
|
def create sql
|
@@ -41,6 +46,16 @@ module Arc::DataStores
|
|
41
46
|
@schema ||= ObjectDefinitions::MysqlSchema.new self
|
42
47
|
end
|
43
48
|
|
49
|
+
def cast_blob data
|
50
|
+
debugger
|
51
|
+
data
|
52
|
+
end
|
53
|
+
def quote_blob data
|
54
|
+
with_store do |store|
|
55
|
+
"'#{store.escape(data).force_encoding("BINARY")}'"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
44
59
|
private
|
45
60
|
def create_connection
|
46
61
|
Mysql2::Client.new @config
|
@@ -4,9 +4,16 @@ require 'arc/data_stores/postgres/object_definitions'
|
|
4
4
|
module Arc
|
5
5
|
module DataStores
|
6
6
|
class PostgresDataStore < AbstractDataStore
|
7
|
+
|
7
8
|
def read query
|
8
|
-
|
9
|
-
|
9
|
+
case query
|
10
|
+
when String
|
11
|
+
execute(query).to_a.symbolize_keys!
|
12
|
+
when Arel::SelectManager
|
13
|
+
result_for query
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
10
17
|
def create sql
|
11
18
|
table = sql.match(/\AINSERT into ([^ (]*)/i)[1]
|
12
19
|
sql[-1] = sql[-1] == ';' ? '' : sql[-1]
|
@@ -23,6 +30,19 @@ module Arc
|
|
23
30
|
def schema
|
24
31
|
@schema ||= ObjectDefinitions::PostgresSchema.new self
|
25
32
|
end
|
33
|
+
|
34
|
+
def quote_binary data
|
35
|
+
with_store do |store|
|
36
|
+
"'#{store.escape_bytea data}'"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def cast_binary data
|
41
|
+
with_store do |store|
|
42
|
+
store.unescape_bytea data
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
26
46
|
private
|
27
47
|
def create_connection
|
28
48
|
PGconn.connect({
|
data/lib/arc/quoting.rb
CHANGED
@@ -65,6 +65,7 @@ module Arc
|
|
65
65
|
return quote_string value if value.is_a? String
|
66
66
|
quote_string "#{value.strftime '%Y-%m-%d %H:%M:%S'}"
|
67
67
|
end
|
68
|
+
alias :quote_timestamp :quote_time
|
68
69
|
def quote_numeric value
|
69
70
|
value.to_s
|
70
71
|
end
|
@@ -79,6 +80,7 @@ module Arc
|
|
79
80
|
'NULL'
|
80
81
|
end
|
81
82
|
def quote_binary value
|
83
|
+
value.force_encoding 'BINARY'
|
82
84
|
value.gsub!(/\0|\%/n) do |byte|
|
83
85
|
case byte
|
84
86
|
when "\0" then "%00"
|
data/lib/arc/version.rb
CHANGED
@@ -6,6 +6,9 @@ module Arc
|
|
6
6
|
before :each do
|
7
7
|
@store = AbstractDataStore.new ArcTest.config[:empty]
|
8
8
|
@query = "omg"
|
9
|
+
@sstore = Arc::DataStores[:sqlite].new ArcTest.config[:sqlite]
|
10
|
+
@sstore.schema.execute_ddl File.read("spec/support/schemas/sqlite.sql")
|
11
|
+
Arel::Table.engine = @sstore
|
9
12
|
end
|
10
13
|
|
11
14
|
it 'proxies methods to the schema' do
|
@@ -52,6 +55,108 @@ module Arc
|
|
52
55
|
end
|
53
56
|
end
|
54
57
|
|
58
|
+
describe '#type_mappings_for' do
|
59
|
+
it '#converts data store data types (varchar, tinyint, etc) into ruby types (string, integer, etc)' do
|
60
|
+
superheros = Arel::Table.new :superheros
|
61
|
+
query = superheros.project(
|
62
|
+
superheros[:name],
|
63
|
+
superheros[:born_on],
|
64
|
+
superheros[:photo],
|
65
|
+
superheros[:created_at]
|
66
|
+
)
|
67
|
+
@sstore.type_mappings_for(query).should == {
|
68
|
+
:name => :varchar,
|
69
|
+
:born_on => :date,
|
70
|
+
:photo => :binary,
|
71
|
+
:created_at => :time
|
72
|
+
}
|
73
|
+
end
|
74
|
+
|
75
|
+
it '#handles aliased tables' do
|
76
|
+
superheros = Arel::Table.new :superheros
|
77
|
+
superheros_1 = superheros.alias
|
78
|
+
query = superheros.project(
|
79
|
+
superheros_1[:name],
|
80
|
+
superheros_1[:born_on],
|
81
|
+
superheros_1[:photo],
|
82
|
+
superheros_1[:created_at]
|
83
|
+
)
|
84
|
+
@sstore.type_mappings_for(query).should == {
|
85
|
+
:name => :varchar,
|
86
|
+
:born_on => :date,
|
87
|
+
:photo => :binary,
|
88
|
+
:created_at => :time
|
89
|
+
}
|
90
|
+
end
|
91
|
+
|
92
|
+
it '#handles aliased columns (attributes)' do
|
93
|
+
superheros = Arel::Table.new :superheros
|
94
|
+
superheros_1 = superheros.alias
|
95
|
+
query = superheros.project(
|
96
|
+
superheros_1[:name].as('superhero_name'),
|
97
|
+
superheros_1[:born_on],
|
98
|
+
superheros_1[:photo],
|
99
|
+
superheros_1[:created_at]
|
100
|
+
)
|
101
|
+
@sstore.type_mappings_for(query).should == {
|
102
|
+
:superhero_name => :varchar,
|
103
|
+
:born_on => :date,
|
104
|
+
:photo => :binary,
|
105
|
+
:created_at => :time
|
106
|
+
}
|
107
|
+
end
|
108
|
+
|
109
|
+
it '#handles complex joins' do
|
110
|
+
powers = Arel::Table.new :powers
|
111
|
+
superheros_powers = Arel::Table.new :superheros_powers
|
112
|
+
superheros = Arel::Table.new :superheros
|
113
|
+
|
114
|
+
superheros_powers = superheros_powers.alias
|
115
|
+
powers = powers.alias
|
116
|
+
|
117
|
+
stmt = superheros
|
118
|
+
.join(superheros_powers).on(
|
119
|
+
superheros_powers[:superhero_id].eq(superheros[:id])
|
120
|
+
)
|
121
|
+
.join(powers).on(
|
122
|
+
powers[:id].eq(superheros_powers[:power_id])
|
123
|
+
)
|
124
|
+
.project(
|
125
|
+
superheros[:name].as('superhero_name'),
|
126
|
+
superheros[:created_at].as("hello"),
|
127
|
+
powers[:name].as("power_name")
|
128
|
+
)
|
129
|
+
.where(
|
130
|
+
superheros[:id].eq(1)
|
131
|
+
)
|
132
|
+
|
133
|
+
@sstore.type_mappings_for(stmt).should == {
|
134
|
+
:superhero_name => :varchar,
|
135
|
+
:hello => :time,
|
136
|
+
:power_name => :varchar
|
137
|
+
}
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
describe '#result_for' do
|
142
|
+
it 'creates a lazy loaded array whose elements are lazy loaded hashes whose values are type-casted query results' do
|
143
|
+
Arel::Table.engine = @sstore
|
144
|
+
load "spec/support/seed.rb"
|
145
|
+
superheros = Arel::Table.new :superheros
|
146
|
+
query = superheros.project(
|
147
|
+
superheros[:name],
|
148
|
+
superheros[:born_on],
|
149
|
+
superheros[:photo],
|
150
|
+
superheros[:created_at]
|
151
|
+
)
|
152
|
+
result = @sstore.result_for query
|
153
|
+
result.should be_an(Array)
|
154
|
+
result[0].should be_a(Hash)
|
155
|
+
result[0][:born_on].should be_a(Date)
|
156
|
+
result[0][:created_at].should be_a(Time)
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
55
160
|
end
|
56
161
|
end
|
57
162
|
end
|
@@ -30,24 +30,35 @@ module Arc
|
|
30
30
|
|
31
31
|
describe '#create and #read' do
|
32
32
|
before :each do
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
33
|
+
Timecop.freeze Time.now do
|
34
|
+
@created_at = Time.parse('2011-12-27 11:52:56 -0700')
|
35
|
+
properties = {
|
36
|
+
:name => "green hornet",
|
37
|
+
:born_on => @created_at,
|
38
|
+
:photo => File.read('spec/support/resources/ironman.gif'),
|
39
|
+
:created_at => @created_at
|
40
|
+
}
|
41
|
+
im = Arel::InsertManager.new Arel::Table.engine
|
42
|
+
im.insert values_array(:superheros, properties)
|
43
|
+
@result = @store.create im.to_sql
|
44
|
+
end
|
42
45
|
end
|
43
46
|
|
44
47
|
it 'creates a new record' do
|
48
|
+
superheros = @tables[:superheros]
|
45
49
|
query = @tables[:superheros]
|
46
|
-
.project(
|
50
|
+
.project(
|
51
|
+
superheros[:name],
|
52
|
+
superheros[:born_on],
|
53
|
+
superheros[:created_at],
|
54
|
+
superheros[:photo]
|
55
|
+
)
|
47
56
|
.where(@tables[:superheros][:name].eq('green hornet'))
|
48
|
-
.to_sql
|
49
57
|
result = @store.read query
|
50
58
|
result[0][:name].should == 'green hornet'
|
59
|
+
result[0][:born_on].should == Date.today
|
60
|
+
result[0][:created_at].should == @created_at
|
61
|
+
result[0][:photo].should == File.read('spec/support/resources/ironman.gif').force_encoding("BINARY")
|
51
62
|
end
|
52
63
|
|
53
64
|
it 'returns the record with a populated primary key' do
|
@@ -88,7 +99,7 @@ module Arc
|
|
88
99
|
superman.size.should == 0
|
89
100
|
superman.size.should == 0
|
90
101
|
superman.should be_a(Enumerable)
|
91
|
-
end
|
102
|
+
end
|
92
103
|
end
|
93
104
|
|
94
105
|
end
|
@@ -2,19 +2,52 @@ require 'spec_helper'
|
|
2
2
|
require 'arc/data_stores/mysql/store'
|
3
3
|
|
4
4
|
describe Arc::DataStores::MysqlDataStore do
|
5
|
-
|
6
|
-
store = Arc::DataStores
|
7
|
-
store.quote_table_name('my-table').should == "`my-table`"
|
5
|
+
before :each do
|
6
|
+
@store = Arc::DataStores[:mysql].new ArcTest.config[:mysql]
|
8
7
|
end
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
|
9
|
+
it 'saves binary data', :wip => true do
|
10
|
+
Arel::Table.engine = @store
|
11
|
+
load "spec/support/seed.rb"
|
12
|
+
superheros = Arel::Table.new :superheros
|
13
|
+
result = @store.read superheros.project(superheros[:photo]).where(superheros[:name].eq("superman"))
|
14
|
+
result[0][:photo].should_not be_nil
|
15
|
+
result[0][:photo].should == File.read("spec/support/resources/superman.gif").force_encoding("BINARY")
|
16
|
+
File.open("spec/support/resources/out/superman.gif", "w+"){ |f| f.write result[0][:photo] }
|
12
17
|
end
|
13
18
|
|
14
19
|
describe "#schema" do
|
15
20
|
it 'is an instance of Arc::DataStores::MysqlSchema' do
|
16
|
-
store
|
17
|
-
|
21
|
+
@store.schema.should be_a(Arc::DataStores::ObjectDefinitions::MysqlSchema)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#quote' do
|
26
|
+
it 'quotes table names' do
|
27
|
+
@store.quote_table_name('my-table').should == "`my-table`"
|
28
|
+
end
|
29
|
+
it 'quotes column names' do
|
30
|
+
@store.quote_column_name('my-column').should == "`my-column`"
|
31
|
+
end
|
32
|
+
it 'quotes dates' do
|
33
|
+
date = Date.parse '2012-01-01'
|
34
|
+
quoted_date = @store.quote date
|
35
|
+
quoted_date.should == "'2012-01-01'"
|
36
|
+
end
|
37
|
+
it 'quotes a time' do
|
38
|
+
time = Time.parse '2012-01-01 12:30:21 -0700'
|
39
|
+
quoted_time = @store.quote time
|
40
|
+
quoted_time.should == "'2012-01-01 12:30:21'"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe '#cast' do
|
45
|
+
it 'casts a date' do
|
46
|
+
date = Date.parse '2012-01-01'
|
47
|
+
casted_date = @store.cast date, :date
|
48
|
+
casted_date.should be_a(Date)
|
49
|
+
casted_date.should == Date.parse('2012-01-01')
|
18
50
|
end
|
19
51
|
end
|
52
|
+
|
20
53
|
end
|
@@ -2,13 +2,25 @@ require 'spec_helper'
|
|
2
2
|
require 'arc/data_stores/postgres/store'
|
3
3
|
|
4
4
|
describe Arc::DataStores::PostgresDataStore do
|
5
|
+
before :each do
|
6
|
+
@store = Arc::DataStores[:postgres].new ArcTest.config[:postgres]
|
7
|
+
ddl = File.read "spec/support/schemas/postgres.sql"
|
8
|
+
@store.schema.execute_ddl ddl
|
9
|
+
end
|
10
|
+
|
11
|
+
after :each do
|
12
|
+
drop_ddl = File.read "spec/support/schemas/drop_postgres.sql"
|
13
|
+
@store.schema.execute_ddl @drop_ddl
|
14
|
+
@schema_loaded = false
|
15
|
+
end
|
16
|
+
|
5
17
|
describe "#schema" do
|
6
18
|
it 'is an instance of Arc::DataStores::PostgresSchema' do
|
7
|
-
store = Arc::DataStores
|
19
|
+
store = Arc::DataStores[:postgres].new ArcTest.config[:postgres]
|
8
20
|
store.schema.should be_a(Arc::DataStores::ObjectDefinitions::PostgresSchema)
|
9
21
|
end
|
10
22
|
end
|
11
|
-
describe Arc::DataStores::ObjectDefinitions::PostgresColumn
|
23
|
+
describe Arc::DataStores::ObjectDefinitions::PostgresColumn do
|
12
24
|
it 'returns a time type' do
|
13
25
|
c = Arc::DataStores::ObjectDefinitions::PostgresColumn.new(nil,
|
14
26
|
:type => "time without time zone"
|
@@ -18,4 +30,13 @@ describe Arc::DataStores::PostgresDataStore do
|
|
18
30
|
c.type.should == :time
|
19
31
|
end
|
20
32
|
end
|
33
|
+
it 'can save binary data' do
|
34
|
+
Arel::Table.engine = @store
|
35
|
+
load "spec/support/seed.rb"
|
36
|
+
superheros = Arel::Table.new :superheros
|
37
|
+
result = @store.read superheros.project(superheros[:photo]).where(superheros[:name].eq("superman"))
|
38
|
+
result[0][:photo].should_not be_nil
|
39
|
+
result[0][:photo].should == File.read("spec/support/resources/superman.gif").force_encoding("BINARY")
|
40
|
+
File.open("spec/support/resources/out/superman.gif", "w+"){ |f| f.write result[0][:photo] }
|
41
|
+
end
|
21
42
|
end
|
data/spec/quoting_spec.rb
CHANGED
@@ -36,10 +36,14 @@ module Arc
|
|
36
36
|
quoter.quote(date.strftime(fmt), Date).should == "'#{date.strftime fmt}'"
|
37
37
|
end
|
38
38
|
it 'quotes a time object' do
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
39
|
+
Timecop.freeze Time.now do
|
40
|
+
fmt = '%Y-%m-%d %H:%M:%S'
|
41
|
+
time = Time.parse(Time.now.strftime(fmt))
|
42
|
+
quoted_time = quoter.quote(time)
|
43
|
+
Time.parse(quoted_time).should == time
|
44
|
+
quoted_time = quoter.quote(time.strftime(fmt), Time)
|
45
|
+
Time.parse(quoted_time).should == time
|
46
|
+
end
|
43
47
|
end
|
44
48
|
it 'quotes a fixnum' do
|
45
49
|
fixnum = 10
|
data/spec/spec_helper.rb
CHANGED
Binary file
|
@@ -3,8 +3,8 @@ CREATE TABLE superheros (
|
|
3
3
|
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
4
4
|
name VARCHAR(256) NOT NULL,
|
5
5
|
born_on DATE NOT NULL,
|
6
|
-
photo
|
7
|
-
created_at
|
6
|
+
photo BLOB NULL,
|
7
|
+
created_at TIMESTAMP NOT NULL
|
8
8
|
);
|
9
9
|
|
10
10
|
DROP TABLE IF EXISTS powers;
|
data/spec/support/seed.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
|
2
1
|
tables = Hash.new do |hash, key|
|
3
2
|
hash[key] = Arel::Table.new key
|
4
3
|
end
|
@@ -9,19 +8,19 @@ data = {
|
|
9
8
|
{
|
10
9
|
:name => 'superman',
|
11
10
|
:born_on => Time.now,
|
12
|
-
:photo =>
|
11
|
+
:photo => File.read("#{file_root}/resources/superman.gif"),
|
13
12
|
:created_at => Time.now
|
14
13
|
},
|
15
14
|
{
|
16
15
|
:name => 'ironman',
|
17
16
|
:born_on => Time.now,
|
18
|
-
:photo =>
|
17
|
+
:photo => File.read("#{file_root}/resources/ironman.gif"),
|
19
18
|
:created_at => Time.now
|
20
19
|
},
|
21
20
|
{
|
22
21
|
:name => 'megaman',
|
23
22
|
:born_on => Time.now,
|
24
|
-
:photo =>
|
23
|
+
:photo => File.read("#{file_root}/resources/megaman.jpg"),
|
25
24
|
:created_at => Time.now
|
26
25
|
}
|
27
26
|
],
|
@@ -42,16 +41,34 @@ data = {
|
|
42
41
|
]
|
43
42
|
}
|
44
43
|
|
44
|
+
ids = {:superheros => {}, :powers => {}}
|
45
|
+
|
45
46
|
data.each_pair do |name, tuples|
|
46
47
|
table = tables[name]
|
47
48
|
delete = Arel::DeleteManager.new Arel::Table.engine
|
48
49
|
Arel::Table.engine.destroy delete.from(table).to_sql
|
50
|
+
|
49
51
|
tuples.each do |tuple|
|
50
52
|
stmt = Arel::InsertManager.new Arel::Table.engine
|
51
53
|
values = tuple.keys.map do |key|
|
52
54
|
[table[key], tuple[key]]
|
53
55
|
end
|
54
56
|
stmt.insert(values)
|
55
|
-
Arel::Table.engine.create
|
57
|
+
record = Arel::Table.engine.create(stmt.to_sql)
|
58
|
+
ids[name][record[:name]] = record[:id]
|
56
59
|
end
|
57
60
|
end
|
61
|
+
power_map = Arel::Table.new :superheros_powers
|
62
|
+
|
63
|
+
power_sets = [
|
64
|
+
['superman', 'flight'],
|
65
|
+
['ironman', 'money']
|
66
|
+
]
|
67
|
+
power_sets.each do |set|
|
68
|
+
im = Arel::InsertManager.new Arel::Table.engine
|
69
|
+
im.insert([
|
70
|
+
[power_map[:superhero_id], ids[:superheros][set[0]]],
|
71
|
+
[power_map[:power_id], ids[:powers][set[1]]]
|
72
|
+
])
|
73
|
+
Arel::Table.engine.create im.to_sql
|
74
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: arc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-12-
|
12
|
+
date: 2011-12-27 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: agent-q
|
16
|
-
requirement: &
|
16
|
+
requirement: &70144475547120 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70144475547120
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: arel
|
27
|
-
requirement: &
|
27
|
+
requirement: &70144475546620 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70144475546620
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: pg
|
38
|
-
requirement: &
|
38
|
+
requirement: &70144475546020 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70144475546020
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: sqlite3
|
49
|
-
requirement: &
|
49
|
+
requirement: &70144475545200 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70144475545200
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: mysql2
|
60
|
-
requirement: &
|
60
|
+
requirement: &70144475544780 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70144475544780
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec
|
71
|
-
requirement: &
|
71
|
+
requirement: &70144475544360 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70144475544360
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: simplecov
|
82
|
-
requirement: &
|
82
|
+
requirement: &70144475543720 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ! '>='
|
@@ -87,7 +87,18 @@ dependencies:
|
|
87
87
|
version: '0'
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *70144475543720
|
91
|
+
- !ruby/object:Gem::Dependency
|
92
|
+
name: timecop
|
93
|
+
requirement: &70144475543240 !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
type: :development
|
100
|
+
prerelease: false
|
101
|
+
version_requirements: *70144475543240
|
91
102
|
description: Compatible with mysql, sqlite, and postgres
|
92
103
|
email:
|
93
104
|
- jacob.s.morris@gmail.com
|
@@ -130,6 +141,7 @@ files:
|
|
130
141
|
- spec/support/config.yml
|
131
142
|
- spec/support/resources/ironman.gif
|
132
143
|
- spec/support/resources/megaman.jpg
|
144
|
+
- spec/support/resources/out/superman.gif
|
133
145
|
- spec/support/resources/rails.png
|
134
146
|
- spec/support/resources/superman.gif
|
135
147
|
- spec/support/schemas/drop_mysql.sql
|
@@ -179,6 +191,7 @@ test_files:
|
|
179
191
|
- spec/support/config.yml
|
180
192
|
- spec/support/resources/ironman.gif
|
181
193
|
- spec/support/resources/megaman.jpg
|
194
|
+
- spec/support/resources/out/superman.gif
|
182
195
|
- spec/support/resources/rails.png
|
183
196
|
- spec/support/resources/superman.gif
|
184
197
|
- spec/support/schemas/drop_mysql.sql
|