flying_table 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -0
- data/lib/flying_table.rb +2 -1
- data/lib/flying_table/table_maker.rb +27 -17
- data/lib/flying_table/version.rb +1 -1
- data/test/flying_table_test.rb +17 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 37ee66f37668c4802965e33678a40a0ac99d985e
|
4
|
+
data.tar.gz: 484cd783963333199e8bfcceb62a7f92e2a83519
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0c19cc0a2cf37920085493d189a94c3036244c6cda3c53a51f0ec5076545eab0d9f7283b33d1143212204fb58eb0a43155da25ba36d00d83255bbe6d1cde49fe
|
7
|
+
data.tar.gz: 7ea49bb51c6f5b604eb96e8aeb9b95b53e1468a2ea210d38d287992280fb8e3630e521f63bd05f7bb131be5fc346e6cb9bb4854d3a63eec63df0823dc34fa84f
|
data/README.md
CHANGED
@@ -22,6 +22,22 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
+
In your tests to create and destroy tables:
|
26
|
+
```ruby
|
27
|
+
$ @tables = FlyingTable.create(example: {name: :string, created: :date})
|
28
|
+
```
|
29
|
+
And when complete:
|
30
|
+
```ruby
|
31
|
+
$ @tables.teardown
|
32
|
+
```
|
33
|
+
|
34
|
+
To use with a block:
|
35
|
+
```ruby
|
36
|
+
$ FlyingTable.with(example: {name: :string, created: :date}) do
|
37
|
+
$ # do stuff with Example
|
38
|
+
$ end
|
39
|
+
```
|
40
|
+
|
25
41
|
In your tests to create a table with class name Example fields name: :string and created: :date
|
26
42
|
|
27
43
|
```ruby
|
data/lib/flying_table.rb
CHANGED
@@ -3,6 +3,7 @@ require "flying_table/table_maker"
|
|
3
3
|
require "active_record"
|
4
4
|
|
5
5
|
module FlyingTable
|
6
|
-
def self.create(
|
6
|
+
def self.create(args) TableMaker.create(args) end
|
7
7
|
def self.destroy(*args) TableMaker.destroy(args) end
|
8
|
+
def self.with(tables,&block) TableMaker.new(tables, &block) end
|
8
9
|
end
|
@@ -1,28 +1,38 @@
|
|
1
1
|
class FlyingTable::TableMaker
|
2
|
-
def
|
3
|
-
|
4
|
-
def initialize(tables)
|
2
|
+
def initialize(tables, delay_setup=false, &block)
|
3
|
+
@tables = tables.map{|name, cols| [name.to_s, cols]}
|
5
4
|
@AR = ActiveRecord
|
6
5
|
@base = @AR::Base
|
7
6
|
@migration = @AR::Migration
|
8
|
-
|
7
|
+
setup() unless delay_setup
|
8
|
+
if block_given?
|
9
|
+
yield(self, @AR)
|
10
|
+
teardown()
|
11
|
+
end
|
9
12
|
end
|
10
|
-
def
|
11
|
-
def
|
13
|
+
def self.create(tables) new(tables) end
|
14
|
+
def self.destroy(tables) new(tables,true).teardown end
|
12
15
|
|
16
|
+
def setup() @base.transaction{@migration.suppress_messages{create_tables}} end
|
17
|
+
def teardown() @base.transaction{@migration.suppress_messages{drop_tables}} end
|
18
|
+
|
19
|
+
# def info() "To be implemented..."
|
13
20
|
|
14
21
|
private
|
15
|
-
def create_columns(t,columns) columns.each{|name,type| t.send(type,name) } end
|
16
|
-
def create_tables() @tables.each{|table| seperate_table(table)} end
|
17
|
-
def drop_tables() @tables.each{|name| drop_table(name.to_s)} end
|
18
|
-
def seperate_table(table) table.each{|name,columns| create_table(name.to_s, columns)} end
|
19
22
|
|
20
|
-
def
|
21
|
-
|
22
|
-
|
23
|
+
def create_tables
|
24
|
+
@tables.each do |name, columns|
|
25
|
+
Object.const_set(name.classify, Class.new(ActiveRecord::Base))
|
26
|
+
@migration.create_table(name.pluralize){ |t| create_columns(t,columns)}
|
27
|
+
end
|
23
28
|
end
|
24
|
-
|
25
|
-
|
26
|
-
|
29
|
+
|
30
|
+
def create_columns(t,columns) columns.each{|name,type| t.send(type,name) } end
|
31
|
+
|
32
|
+
def drop_tables
|
33
|
+
@tables.each do |name, columns|
|
34
|
+
Object.send(:remove_const, name.classify)
|
35
|
+
@migration.drop_table(name.pluralize)
|
36
|
+
end
|
27
37
|
end
|
28
|
-
end
|
38
|
+
end
|
data/lib/flying_table/version.rb
CHANGED
data/test/flying_table_test.rb
CHANGED
@@ -25,4 +25,21 @@ class FuzzyRecordTest < TestCase
|
|
25
25
|
assert_raises(NameError) {Elbat.new()}
|
26
26
|
assert_raises(NameError) {Elbaf.new()}
|
27
27
|
end
|
28
|
+
|
29
|
+
def test_setup_and_teardown_instance
|
30
|
+
@tables = FlyingTable.create(sinstance: {name: :string, created: :date})
|
31
|
+
example = Sinstance.new(name: 'John', created: Date.today)
|
32
|
+
assert_equal true, example.save!
|
33
|
+
@tables.teardown
|
34
|
+
assert_raises(NameError) {Sinstance.new()}
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_block_works
|
38
|
+
FlyingTable.with(Sample: {name: :string, created: :date}) do
|
39
|
+
example = Sample.new(name: 'John', created: Date.today)
|
40
|
+
assert_equal true, example.save!
|
41
|
+
assert_equal example, Sample.where(name: 'John', created: Date.today).first
|
42
|
+
end
|
43
|
+
assert_raises(NameError) {Sample.new()}
|
44
|
+
end
|
28
45
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: flying_table
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Moody
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-05-
|
12
|
+
date: 2015-05-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|