temping 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.md +6 -3
  2. data/VERSION +1 -1
  3. data/lib/temping.rb +15 -8
  4. data/spec/temping_spec.rb +20 -10
  5. metadata +2 -2
data/README.md CHANGED
@@ -17,17 +17,19 @@ As we're using temporary tables all data will be dropped when the database conne
17
17
 
18
18
  The basic setup of a model involves calling _create_model_ with a symbol that represents the plural table name of the model you wish to create. By default, this will create a temporary table with an _id_ column.
19
19
 
20
+ require 'temping'
20
21
  include Temping
21
22
 
22
- create_model :dogs
23
+ create_model :dog
23
24
 
24
25
  Dog.create => #<Dog id: 1>
25
26
 
26
27
  Additional database columns can be specified via the _with_columns_ method which uses Rails migration syntax:
27
28
 
29
+ require 'temping'
28
30
  include Temping
29
31
 
30
- create_model :dogs do
32
+ create_model :dog do
31
33
  with_columns do |t|
32
34
  t.string :name
33
35
  t.integer :age, :weight
@@ -38,9 +40,10 @@ Additional database columns can be specified via the _with_columns_ method which
38
40
 
39
41
  When a block is passed to _create_model_, it is evaluated in the context of the ActiveRecord class. This means anything you do in an ActiveRecord model can be accomplished in the block including method definitions, validations, module includes, etc.
40
42
 
43
+ require 'temping'
41
44
  include Temping
42
45
 
43
- create_model :dogs do
46
+ create_model :dog do
44
47
  include Duckish
45
48
 
46
49
  with_columns do |t|
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.0
1
+ 1.3.0
@@ -1,16 +1,19 @@
1
1
  require 'active_record'
2
2
 
3
3
  module Temping
4
- ModelAlreadyDefined = Class.new(StandardError)
5
-
6
4
  def self.included(base)
7
- ActiveRecord::Base.configurations['temping'] = { :adapter => 'sqlite3', :database => ':memory:' }
8
- ActiveRecord::Base.establish_connection 'temping' unless ActiveRecord::Base.connected?
5
+ unless ActiveRecord::Base.connected?
6
+ ActiveRecord::Base.configurations['temping'] = {
7
+ :adapter => 'sqlite3', :database => ':memory:'
8
+ }
9
+ ActiveRecord::Base.establish_connection 'temping'
10
+ end
9
11
  end
10
12
 
11
13
  def create_model(model_name, &block)
12
- unless eval("defined?(#{model_name.to_s.classify})")
13
- factory = ModelFactory.new(model_name, &block)
14
+ model_class = model_name.to_s.classify
15
+ unless eval("defined?(#{model_class})")
16
+ factory = ModelFactory.new(model_class, &block)
14
17
  factory.klass
15
18
  end
16
19
  end
@@ -18,9 +21,9 @@ module Temping
18
21
  class ModelFactory
19
22
  attr_accessor :klass
20
23
 
21
- def initialize(model_name, &block)
24
+ def initialize(model_class, &block)
22
25
  @klass = Class.new(ActiveRecord::Base)
23
- Object.const_set(model_name.to_s.classify, @klass)
26
+ Object.const_set(model_class, @klass)
24
27
  create_table
25
28
  add_methods
26
29
  @klass.class_eval(&block) if block_given?
@@ -39,6 +42,10 @@ module Temping
39
42
  def with_columns
40
43
  connection.change_table(table_name) { |table| yield(table) }
41
44
  end
45
+
46
+ def table_exists?
47
+ true
48
+ end
42
49
  end
43
50
  end
44
51
 
@@ -5,12 +5,14 @@ describe Temping do
5
5
 
6
6
  describe ".create_model" do
7
7
  it "creates and returns an AR::Base-derived model" do
8
- posts = create_model :posts
9
- posts.ancestors.should include(ActiveRecord::Base)
8
+ post_class = create_model :post
9
+ post_class.ancestors.should include(ActiveRecord::Base)
10
+ post_class.should == Post
11
+ post_class.table_name.should == "posts"
10
12
  end
11
13
 
12
14
  it "evals all statements passed in through a block" do
13
- create_model :votes do
15
+ create_model :vote do
14
16
  with_columns do |table|
15
17
  table.string :voter
16
18
  end
@@ -21,17 +23,22 @@ describe Temping do
21
23
  vote = Vote.new
22
24
  vote.should_not be_valid
23
25
 
24
- vote.voter = "John Pignata"
26
+ vote.voter = "Foo Bar"
25
27
  vote.should be_valid
26
28
  end
27
29
 
28
- it "silently skip initialization if a const is already defined" do
29
- lambda { 2.times { create_model :dogs }}.should_not raise_error(Temping::ModelAlreadyDefined)
30
+ it "silently skips initialization if a const is already defined" do
31
+ lambda { 2.times { create_model :dog } }.should_not raise_error
32
+ end
33
+
34
+ it "returns nil if a const is already defined" do
35
+ create_model :cat
36
+ create_model(:cat).should be_nil
30
37
  end
31
38
 
32
39
  describe ".with_columns" do
33
40
  it "creates columns passed in through a block" do
34
- create_model :comments do
41
+ create_model :comment do
35
42
  with_columns do |table|
36
43
  table.integer :count
37
44
  table.string :headline
@@ -47,17 +54,20 @@ describe Temping do
47
54
  describe "database agnostism" do
48
55
  it "supports Sqlite3" do
49
56
  ActiveRecord::Base.establish_connection 'temping'
50
- create_model(:oranges).should == Orange
57
+ create_model(:orange).should == Orange
58
+ Orange.table_name.should == "oranges"
51
59
  end
52
60
 
53
61
  it "supports MySQL" do
54
62
  ActiveRecord::Base.establish_connection 'mysql'
55
- create_model(:apples).should == Apple
63
+ create_model(:apple).should == Apple
64
+ Apple.table_name.should == "apples"
56
65
  end
57
66
 
58
67
  it "supports PostgreSQL" do
59
68
  ActiveRecord::Base.establish_connection 'postgres'
60
- create_model(:cherries).should == Cherry
69
+ create_model(:cherry).should == Cherry
70
+ Cherry.table_name.should == "cherries"
61
71
  end
62
72
  end
63
73
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: temping
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Pignata
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-30 00:00:00 -05:00
12
+ date: 2010-01-03 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency