temping 2.0.2 → 2.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/temping.rb +73 -0
  2. data/lib/version.rb +3 -0
  3. metadata +13 -11
@@ -0,0 +1,73 @@
1
+ require "active_record"
2
+ require "active_support/core_ext/string"
3
+
4
+ class Temping
5
+ class << self
6
+ def create(model_name, &block)
7
+ connect unless ActiveRecord::Base.connected?
8
+
9
+ model_class_name = model_name.to_s.classify
10
+
11
+ if eval("defined?(#{model_class_name})")
12
+ model_class_name.constantize
13
+ else
14
+ factory = ModelFactory.new(model_class_name, &block)
15
+ factory.klass
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def connect
22
+ ActiveRecord::Base.configurations["temping"] = database_options
23
+ ActiveRecord::Base.establish_connection("temping")
24
+ end
25
+
26
+ def database_options
27
+ { :adapter => "sqlite3", :database => ":memory:" }
28
+ end
29
+ end
30
+
31
+ class ModelFactory
32
+ attr_reader :klass
33
+
34
+ def initialize(model_name, &block)
35
+ @klass = Class.new(ActiveRecord::Base)
36
+
37
+ Object.const_set(model_name, @klass)
38
+ create_table
39
+ add_methods
40
+ @klass.class_eval(&block) if block_given?
41
+ end
42
+
43
+ private
44
+
45
+ def create_table
46
+ connection.create_table(table_name, :temporary => true) do |table|
47
+ table.integer :id
48
+ end
49
+ end
50
+
51
+ def add_methods
52
+ class << @klass
53
+ def with_columns
54
+ connection.change_table(table_name) do |table|
55
+ yield(table)
56
+ end
57
+ end
58
+
59
+ def table_exists?
60
+ true
61
+ end
62
+ end
63
+ end
64
+
65
+ def connection
66
+ @klass.connection
67
+ end
68
+
69
+ def table_name
70
+ @klass.table_name
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,3 @@
1
+ class Temping
2
+ VERSION = "2.0.4"
3
+ 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: 2.0.2
4
+ version: 2.0.4
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-11-13 00:00:00.000000000Z
12
+ date: 2011-11-14 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &70219927501000 !ruby/object:Gem::Requirement
16
+ requirement: &70232219712840 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.1.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70219927501000
24
+ version_requirements: *70232219712840
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activesupport
27
- requirement: &70219927500500 !ruby/object:Gem::Requirement
27
+ requirement: &70232219712060 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 3.1.1
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70219927500500
35
+ version_requirements: *70232219712060
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sqlite3
38
- requirement: &70219927500040 !ruby/object:Gem::Requirement
38
+ requirement: &70232219711460 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.3.4
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70219927500040
46
+ version_requirements: *70232219711460
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &70219927499580 !ruby/object:Gem::Requirement
49
+ requirement: &70232219710760 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,13 +54,15 @@ dependencies:
54
54
  version: 2.7.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70219927499580
57
+ version_requirements: *70232219710760
58
58
  description:
59
59
  email: john@pignata.com
60
60
  executables: []
61
61
  extensions: []
62
62
  extra_rdoc_files: []
63
- files: []
63
+ files:
64
+ - lib/temping.rb
65
+ - lib/version.rb
64
66
  homepage: http://github.com/jpignata/temping
65
67
  licenses: []
66
68
  post_install_message: