flying_table 0.0.2 → 0.0.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7e97484463275a348c44a9b40ede2845072ddba0
4
- data.tar.gz: 56758a6cda5c9f49f64847a5bfb6374ee6215ac4
3
+ metadata.gz: 37ee66f37668c4802965e33678a40a0ac99d985e
4
+ data.tar.gz: 484cd783963333199e8bfcceb62a7f92e2a83519
5
5
  SHA512:
6
- metadata.gz: ed7ed4c08a2de5d6efdb26faf5cd169b1841ab1b4bf355c7ba8d5727f617f62a1b18df848e30dafc993d5d348370094d0bce064d35ca17cb7702cdf58e5cb52d
7
- data.tar.gz: 99b62d601651783a23e027bfc0efaaff1921fb951cce272de702b07c367096b5bc1f06a10c582bb42113ce199a2a1fe5e4d1d10120679b4cc2f4873c4afb254b
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(*args) TableMaker.create(args) end
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 self.create(tables) new(tables).setup end
3
- def self.destroy(tables) new(tables).teardown end
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
- @tables = tables
7
+ setup() unless delay_setup
8
+ if block_given?
9
+ yield(self, @AR)
10
+ teardown()
11
+ end
9
12
  end
10
- def setup() @base.transaction{@migration.suppress_messages{create_tables}} end
11
- def teardown() @base.transaction{@migration.suppress_messages{drop_tables}} end
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 create_table(name,columns)
21
- Object.const_set(name.classify, Class.new(ActiveRecord::Base))
22
- @migration.create_table(name.pluralize){ |t| create_columns(t,columns)}
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
- def drop_table(name)
25
- Object.send(:remove_const, name.classify)
26
- @migration.drop_table(name.pluralize)
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
@@ -1,3 +1,3 @@
1
1
  module FlyingTable
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -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.2
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-15 00:00:00.000000000 Z
12
+ date: 2015-05-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails