fixturies 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/fixturies.rb +30 -14
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 02d91937e41679e4804b8d8d7d626c431e988edd
|
4
|
+
data.tar.gz: d1cb12c8c6eb33db75519055c0ff31d72a33a7a4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aaa46d44632beec2b9f110974ed3eeedbb7b861786818949857db4b34a23f4239ab75960063e94c956d3faa03d05305eae6056c01ee7f4be10b82ec914af80bb
|
7
|
+
data.tar.gz: d6382d8700c90d14612536b1d8ac27265193347fbb38545a14976368daf549d7eb6ea1ed8e449df0327cd0a76df71bbc326efa55d72483849caac271729e68d9
|
data/lib/fixturies.rb
CHANGED
@@ -2,6 +2,21 @@ class Fixturies
|
|
2
2
|
|
3
3
|
class << self
|
4
4
|
|
5
|
+
attr_reader :fixtures_directory
|
6
|
+
|
7
|
+
def build(*things, &proc)
|
8
|
+
meth_name = :"builder_#{rand}"
|
9
|
+
define_method meth_name, &proc
|
10
|
+
things.each do |thing|
|
11
|
+
thing.is_a?(String) ? table(thing) : model(thing)
|
12
|
+
end
|
13
|
+
builders << meth_name
|
14
|
+
end
|
15
|
+
|
16
|
+
def set_fixtures_directory(dir)
|
17
|
+
@fixtures_directory = dir
|
18
|
+
end
|
19
|
+
|
5
20
|
def create_fixtures
|
6
21
|
self.new.create_fixtures
|
7
22
|
end
|
@@ -12,14 +27,7 @@ class Fixturies
|
|
12
27
|
|
13
28
|
def model(model_klass)
|
14
29
|
self.table_names << model_klass.table_name
|
15
|
-
end
|
16
|
-
|
17
|
-
def build(*things, &proc)
|
18
|
-
things.each do |thing|
|
19
|
-
thing.is_a?(String) ? table(thing) : model(thing)
|
20
|
-
end
|
21
|
-
builders << proc
|
22
|
-
end
|
30
|
+
end
|
23
31
|
|
24
32
|
def table_names
|
25
33
|
unless defined? @table_names
|
@@ -39,7 +47,7 @@ class Fixturies
|
|
39
47
|
|
40
48
|
attr_reader :record_identifiers
|
41
49
|
|
42
|
-
def
|
50
|
+
def initialize
|
43
51
|
@record_identifiers = {}
|
44
52
|
end
|
45
53
|
|
@@ -51,11 +59,14 @@ class Fixturies
|
|
51
59
|
|
52
60
|
def build_all_records
|
53
61
|
self.class.builders.each do |builder|
|
54
|
-
|
62
|
+
send(builder)
|
55
63
|
end
|
56
64
|
end
|
57
65
|
|
58
66
|
def identify(record, name)
|
67
|
+
if record.id.nil?
|
68
|
+
raise ArgumentError.new("No id for record. Must be saved before calling identify")
|
69
|
+
end
|
59
70
|
record_identifiers[record_key(record)] = name
|
60
71
|
end
|
61
72
|
|
@@ -67,6 +78,12 @@ class Fixturies
|
|
67
78
|
private
|
68
79
|
def create_fixture_files
|
69
80
|
|
81
|
+
if self.class.fixtures_directory.nil?
|
82
|
+
raise "No fixtures_directory set. You must call set_fixtures_directory 'path/to/directory'"
|
83
|
+
end
|
84
|
+
|
85
|
+
FileUtils.mkdir_p(self.class.fixtures_directory)
|
86
|
+
|
70
87
|
self.class.table_names.each do |table_name|
|
71
88
|
|
72
89
|
# create a simple ActiveRecord klass to connect
|
@@ -80,9 +97,8 @@ class Fixturies
|
|
80
97
|
name = record_identifiers[record_key(record)] || "#{table_name.singularize}_#{i}"
|
81
98
|
hash[name] = record.attributes
|
82
99
|
end
|
83
|
-
|
84
|
-
|
85
|
-
File.open(Rails.root.join('spec', 'fixtures', "#{table_name}.yml"), 'w+') do |f|
|
100
|
+
|
101
|
+
File.open(Rails.root.join(self.class.fixtures_directory, "#{table_name}.yml"), 'w+') do |f|
|
86
102
|
f.write(hash.to_yaml)
|
87
103
|
end
|
88
104
|
|
@@ -94,7 +110,7 @@ class Fixturies
|
|
94
110
|
self.class.table_names.each do |table_name|
|
95
111
|
quoted_table_name = ActiveRecord::Base.connection.quote_table_name(table_name)
|
96
112
|
sql = "DELETE FROM #{quoted_table_name} "
|
97
|
-
ActiveRecord::Base.connection.delete(sql, "#{
|
113
|
+
ActiveRecord::Base.connection.delete(sql, "#{quoted_table_name} Delete all")
|
98
114
|
end
|
99
115
|
|
100
116
|
end
|