to_factory 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/.rspec +2 -1
- data/.travis.yml +23 -0
- data/Gemfile +3 -13
- data/README.md +59 -20
- data/Rakefile +20 -14
- data/bin/ci +2 -0
- data/bin/spec +2 -0
- data/lib/to_factory/collation.rb +34 -0
- data/lib/to_factory/config.rb +18 -0
- data/lib/to_factory/definition_group.rb +47 -0
- data/lib/to_factory/file_sync.rb +48 -0
- data/lib/to_factory/file_writer.rb +39 -0
- data/lib/to_factory/finders/factory.rb +17 -0
- data/lib/to_factory/finders/model.rb +47 -0
- data/lib/to_factory/generation/attribute.rb +52 -0
- data/lib/to_factory/generation/factory.rb +74 -0
- data/lib/to_factory/parsing/file.rb +38 -0
- data/lib/to_factory/parsing/new_syntax.rb +9 -0
- data/lib/to_factory/parsing/old_syntax.rb +9 -0
- data/lib/to_factory/parsing/syntax.rb +86 -0
- data/lib/to_factory/version.rb +1 -1
- data/lib/to_factory.rb +55 -1
- data/spec/db/migrate/{201108201012010100_create_users.rb → 1_create_users.rb} +0 -0
- data/spec/db/migrate/2_create_projects.rb +13 -0
- data/spec/db/migrate/3_create_not_namespaced.rb +11 -0
- data/spec/db/migrate/4_add_birthday_to_users.rb +9 -0
- data/spec/example_factories/new_syntax/admin.rb +6 -0
- data/spec/example_factories/new_syntax/admin_with_header.rb +8 -0
- data/spec/example_factories/new_syntax/project_with_header.rb +7 -0
- data/spec/example_factories/new_syntax/user.rb +6 -0
- data/spec/example_factories/new_syntax/user_admin.rb +13 -0
- data/spec/example_factories/new_syntax/user_admin_with_header.rb +15 -0
- data/spec/example_factories/new_syntax/user_with_header.rb +8 -0
- data/spec/example_factories/old_syntax/admin.rb +6 -0
- data/spec/example_factories/old_syntax/project_with_header.rb +5 -0
- data/spec/example_factories/old_syntax/user.rb +6 -0
- data/spec/example_factories/old_syntax/user_admin.rb +13 -0
- data/spec/example_factories/old_syntax/user_admin_with_header.rb +13 -0
- data/spec/example_factories/old_syntax/user_with_header.rb +6 -0
- data/spec/integration/config_spec.rb +19 -0
- data/spec/integration/file_sync_spec.rb +77 -0
- data/spec/integration/file_writer_spec.rb +16 -0
- data/spec/integration/generate_class_method_spec.rb +107 -0
- data/spec/integration/lint_spec.rb +16 -0
- data/spec/spec_helper.rb +90 -14
- data/spec/support/match_sexp.rb +24 -0
- data/spec/support/models/not_active_record.rb +2 -0
- data/spec/support/models/project.rb +3 -0
- data/spec/support/models/user.rb +3 -0
- data/spec/support/terse_expect_syntax.rb +23 -0
- data/spec/unit/collation_spec.rb +35 -0
- data/spec/unit/definition_group_spec.rb +19 -0
- data/spec/unit/file_writer_spec.rb +28 -0
- data/spec/unit/finders/factory_spec.rb +27 -0
- data/spec/unit/finders/model_spec.rb +28 -0
- data/spec/unit/generator_spec.rb +71 -0
- data/spec/unit/parsing/file_spec.rb +83 -0
- data/tmp/.keep +0 -0
- data/to_factory.gemspec +49 -22
- metadata +225 -25
- data/.rvmrc +0 -1
- data/Gemfile.lock +0 -113
- data/lib/to_factory/generator.rb +0 -115
- data/spec/config/database.yml +0 -2
- data/spec/db/test.sqlite +0 -0
- data/spec/generator_spec.rb +0 -166
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: to_factory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.3
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Mark Burns
|
@@ -15,13 +15,58 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2015-01-05 00:00:00 +00:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
|
-
name:
|
22
|
+
name: sqlite3
|
23
23
|
prerelease: false
|
24
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 23
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 3
|
33
|
+
- 6
|
34
|
+
version: 1.3.6
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: ruby-debug
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 3
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
version: "0"
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: database_cleaner
|
53
|
+
prerelease: false
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ~>
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 13
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
- 3
|
63
|
+
version: "0.3"
|
64
|
+
type: :development
|
65
|
+
version_requirements: *id003
|
66
|
+
- !ruby/object:Gem::Dependency
|
67
|
+
name: ruby2ruby
|
68
|
+
prerelease: false
|
69
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
25
70
|
none: false
|
26
71
|
requirements:
|
27
72
|
- - ">="
|
@@ -31,11 +76,77 @@ dependencies:
|
|
31
76
|
- 0
|
32
77
|
version: "0"
|
33
78
|
type: :runtime
|
34
|
-
version_requirements: *
|
79
|
+
version_requirements: *id004
|
35
80
|
- !ruby/object:Gem::Dependency
|
36
|
-
name:
|
81
|
+
name: deep_merge
|
37
82
|
prerelease: false
|
38
|
-
requirement: &
|
83
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
hash: 3
|
89
|
+
segments:
|
90
|
+
- 0
|
91
|
+
version: "0"
|
92
|
+
type: :runtime
|
93
|
+
version_requirements: *id005
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: activerecord
|
96
|
+
prerelease: false
|
97
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ">"
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
hash: 3
|
103
|
+
segments:
|
104
|
+
- 2
|
105
|
+
- 0
|
106
|
+
version: "2.0"
|
107
|
+
- - <
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
hash: 27
|
110
|
+
segments:
|
111
|
+
- 4
|
112
|
+
- 0
|
113
|
+
version: "4.0"
|
114
|
+
type: :runtime
|
115
|
+
version_requirements: *id006
|
116
|
+
- !ruby/object:Gem::Dependency
|
117
|
+
name: i18n
|
118
|
+
prerelease: false
|
119
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
120
|
+
none: false
|
121
|
+
requirements:
|
122
|
+
- - <
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
hash: 5
|
125
|
+
segments:
|
126
|
+
- 0
|
127
|
+
- 7
|
128
|
+
version: "0.7"
|
129
|
+
type: :runtime
|
130
|
+
version_requirements: *id007
|
131
|
+
- !ruby/object:Gem::Dependency
|
132
|
+
name: factory_girl
|
133
|
+
prerelease: false
|
134
|
+
requirement: &id008 !ruby/object:Gem::Requirement
|
135
|
+
none: false
|
136
|
+
requirements:
|
137
|
+
- - <
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
hash: 3
|
140
|
+
segments:
|
141
|
+
- 2
|
142
|
+
- 0
|
143
|
+
version: "2.0"
|
144
|
+
type: :development
|
145
|
+
version_requirements: *id008
|
146
|
+
- !ruby/object:Gem::Dependency
|
147
|
+
name: rspec
|
148
|
+
prerelease: false
|
149
|
+
requirement: &id009 !ruby/object:Gem::Requirement
|
39
150
|
none: false
|
40
151
|
requirements:
|
41
152
|
- - ~>
|
@@ -45,9 +156,24 @@ dependencies:
|
|
45
156
|
- 3
|
46
157
|
- 0
|
47
158
|
version: "3.0"
|
48
|
-
type: :
|
49
|
-
version_requirements: *
|
50
|
-
|
159
|
+
type: :development
|
160
|
+
version_requirements: *id009
|
161
|
+
- !ruby/object:Gem::Dependency
|
162
|
+
name: rake
|
163
|
+
prerelease: false
|
164
|
+
requirement: &id010 !ruby/object:Gem::Requirement
|
165
|
+
none: false
|
166
|
+
requirements:
|
167
|
+
- - ~>
|
168
|
+
- !ruby/object:Gem::Version
|
169
|
+
hash: 35
|
170
|
+
segments:
|
171
|
+
- 10
|
172
|
+
- 0
|
173
|
+
version: "10.0"
|
174
|
+
type: :development
|
175
|
+
version_requirements: *id010
|
176
|
+
description: ActiveRecord::Base#to_factory method to create factory_girl definitions from real data
|
51
177
|
email:
|
52
178
|
- markthedeveloper@gmail.com
|
53
179
|
executables: []
|
@@ -59,24 +185,68 @@ extra_rdoc_files: []
|
|
59
185
|
files:
|
60
186
|
- .gitignore
|
61
187
|
- .rspec
|
62
|
-
- .
|
188
|
+
- .travis.yml
|
63
189
|
- Gemfile
|
64
|
-
- Gemfile.lock
|
65
190
|
- README.md
|
66
191
|
- Rakefile
|
192
|
+
- bin/ci
|
193
|
+
- bin/spec
|
67
194
|
- lib/to_factory.rb
|
68
|
-
- lib/to_factory/
|
195
|
+
- lib/to_factory/collation.rb
|
196
|
+
- lib/to_factory/config.rb
|
197
|
+
- lib/to_factory/definition_group.rb
|
198
|
+
- lib/to_factory/file_sync.rb
|
199
|
+
- lib/to_factory/file_writer.rb
|
200
|
+
- lib/to_factory/finders/factory.rb
|
201
|
+
- lib/to_factory/finders/model.rb
|
202
|
+
- lib/to_factory/generation/attribute.rb
|
203
|
+
- lib/to_factory/generation/factory.rb
|
204
|
+
- lib/to_factory/parsing/file.rb
|
205
|
+
- lib/to_factory/parsing/new_syntax.rb
|
206
|
+
- lib/to_factory/parsing/old_syntax.rb
|
207
|
+
- lib/to_factory/parsing/syntax.rb
|
69
208
|
- lib/to_factory/version.rb
|
70
|
-
- spec/
|
71
|
-
- spec/db/migrate/
|
72
|
-
- spec/db/
|
73
|
-
- spec/
|
209
|
+
- spec/db/migrate/1_create_users.rb
|
210
|
+
- spec/db/migrate/2_create_projects.rb
|
211
|
+
- spec/db/migrate/3_create_not_namespaced.rb
|
212
|
+
- spec/db/migrate/4_add_birthday_to_users.rb
|
213
|
+
- spec/example_factories/new_syntax/admin.rb
|
214
|
+
- spec/example_factories/new_syntax/admin_with_header.rb
|
215
|
+
- spec/example_factories/new_syntax/project_with_header.rb
|
216
|
+
- spec/example_factories/new_syntax/user.rb
|
217
|
+
- spec/example_factories/new_syntax/user_admin.rb
|
218
|
+
- spec/example_factories/new_syntax/user_admin_with_header.rb
|
219
|
+
- spec/example_factories/new_syntax/user_with_header.rb
|
220
|
+
- spec/example_factories/old_syntax/admin.rb
|
221
|
+
- spec/example_factories/old_syntax/project_with_header.rb
|
222
|
+
- spec/example_factories/old_syntax/user.rb
|
223
|
+
- spec/example_factories/old_syntax/user_admin.rb
|
224
|
+
- spec/example_factories/old_syntax/user_admin_with_header.rb
|
225
|
+
- spec/example_factories/old_syntax/user_with_header.rb
|
226
|
+
- spec/integration/config_spec.rb
|
227
|
+
- spec/integration/file_sync_spec.rb
|
228
|
+
- spec/integration/file_writer_spec.rb
|
229
|
+
- spec/integration/generate_class_method_spec.rb
|
230
|
+
- spec/integration/lint_spec.rb
|
74
231
|
- spec/spec_helper.rb
|
232
|
+
- spec/support/match_sexp.rb
|
233
|
+
- spec/support/models/not_active_record.rb
|
234
|
+
- spec/support/models/project.rb
|
235
|
+
- spec/support/models/user.rb
|
236
|
+
- spec/support/terse_expect_syntax.rb
|
237
|
+
- spec/unit/collation_spec.rb
|
238
|
+
- spec/unit/definition_group_spec.rb
|
239
|
+
- spec/unit/file_writer_spec.rb
|
240
|
+
- spec/unit/finders/factory_spec.rb
|
241
|
+
- spec/unit/finders/model_spec.rb
|
242
|
+
- spec/unit/generator_spec.rb
|
243
|
+
- spec/unit/parsing/file_spec.rb
|
244
|
+
- tmp/.keep
|
75
245
|
- to_factory.gemspec
|
76
246
|
has_rdoc: true
|
77
247
|
homepage: ""
|
78
|
-
licenses:
|
79
|
-
|
248
|
+
licenses:
|
249
|
+
- MIT
|
80
250
|
post_install_message:
|
81
251
|
rdoc_options: []
|
82
252
|
|
@@ -102,14 +272,44 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
102
272
|
version: "0"
|
103
273
|
requirements: []
|
104
274
|
|
105
|
-
rubyforge_project:
|
275
|
+
rubyforge_project:
|
106
276
|
rubygems_version: 1.6.2
|
107
277
|
signing_key:
|
108
278
|
specification_version: 3
|
109
279
|
summary: Turn ActiveRecord instances into factories
|
110
280
|
test_files:
|
111
|
-
- spec/
|
112
|
-
- spec/db/migrate/
|
113
|
-
- spec/db/
|
114
|
-
- spec/
|
281
|
+
- spec/db/migrate/1_create_users.rb
|
282
|
+
- spec/db/migrate/2_create_projects.rb
|
283
|
+
- spec/db/migrate/3_create_not_namespaced.rb
|
284
|
+
- spec/db/migrate/4_add_birthday_to_users.rb
|
285
|
+
- spec/example_factories/new_syntax/admin.rb
|
286
|
+
- spec/example_factories/new_syntax/admin_with_header.rb
|
287
|
+
- spec/example_factories/new_syntax/project_with_header.rb
|
288
|
+
- spec/example_factories/new_syntax/user.rb
|
289
|
+
- spec/example_factories/new_syntax/user_admin.rb
|
290
|
+
- spec/example_factories/new_syntax/user_admin_with_header.rb
|
291
|
+
- spec/example_factories/new_syntax/user_with_header.rb
|
292
|
+
- spec/example_factories/old_syntax/admin.rb
|
293
|
+
- spec/example_factories/old_syntax/project_with_header.rb
|
294
|
+
- spec/example_factories/old_syntax/user.rb
|
295
|
+
- spec/example_factories/old_syntax/user_admin.rb
|
296
|
+
- spec/example_factories/old_syntax/user_admin_with_header.rb
|
297
|
+
- spec/example_factories/old_syntax/user_with_header.rb
|
298
|
+
- spec/integration/config_spec.rb
|
299
|
+
- spec/integration/file_sync_spec.rb
|
300
|
+
- spec/integration/file_writer_spec.rb
|
301
|
+
- spec/integration/generate_class_method_spec.rb
|
302
|
+
- spec/integration/lint_spec.rb
|
115
303
|
- spec/spec_helper.rb
|
304
|
+
- spec/support/match_sexp.rb
|
305
|
+
- spec/support/models/not_active_record.rb
|
306
|
+
- spec/support/models/project.rb
|
307
|
+
- spec/support/models/user.rb
|
308
|
+
- spec/support/terse_expect_syntax.rb
|
309
|
+
- spec/unit/collation_spec.rb
|
310
|
+
- spec/unit/definition_group_spec.rb
|
311
|
+
- spec/unit/file_writer_spec.rb
|
312
|
+
- spec/unit/finders/factory_spec.rb
|
313
|
+
- spec/unit/finders/model_spec.rb
|
314
|
+
- spec/unit/generator_spec.rb
|
315
|
+
- spec/unit/parsing/file_spec.rb
|
data/.rvmrc
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
rvm use ree
|
data/Gemfile.lock
DELETED
@@ -1,113 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
to_factory (0.0.2)
|
5
|
-
factory_girl_rails
|
6
|
-
rails (~> 3.0)
|
7
|
-
|
8
|
-
GEM
|
9
|
-
remote: http://rubygems.org/
|
10
|
-
remote: http://gems.github.com/
|
11
|
-
specs:
|
12
|
-
abstract (1.0.0)
|
13
|
-
actionmailer (3.0.10)
|
14
|
-
actionpack (= 3.0.10)
|
15
|
-
mail (~> 2.2.19)
|
16
|
-
actionpack (3.0.10)
|
17
|
-
activemodel (= 3.0.10)
|
18
|
-
activesupport (= 3.0.10)
|
19
|
-
builder (~> 2.1.2)
|
20
|
-
erubis (~> 2.6.6)
|
21
|
-
i18n (~> 0.5.0)
|
22
|
-
rack (~> 1.2.1)
|
23
|
-
rack-mount (~> 0.6.14)
|
24
|
-
rack-test (~> 0.5.7)
|
25
|
-
tzinfo (~> 0.3.23)
|
26
|
-
activemodel (3.0.10)
|
27
|
-
activesupport (= 3.0.10)
|
28
|
-
builder (~> 2.1.2)
|
29
|
-
i18n (~> 0.5.0)
|
30
|
-
activerecord (3.0.10)
|
31
|
-
activemodel (= 3.0.10)
|
32
|
-
activesupport (= 3.0.10)
|
33
|
-
arel (~> 2.0.10)
|
34
|
-
tzinfo (~> 0.3.23)
|
35
|
-
activeresource (3.0.10)
|
36
|
-
activemodel (= 3.0.10)
|
37
|
-
activesupport (= 3.0.10)
|
38
|
-
activesupport (3.0.10)
|
39
|
-
arel (2.0.10)
|
40
|
-
builder (2.1.2)
|
41
|
-
columnize (0.3.4)
|
42
|
-
diff-lcs (1.1.2)
|
43
|
-
erubis (2.6.6)
|
44
|
-
abstract (>= 1.0.0)
|
45
|
-
factory_girl (2.0.2)
|
46
|
-
factory_girl_rails (1.1.0)
|
47
|
-
factory_girl (~> 2.0.0)
|
48
|
-
railties (>= 3.0.0)
|
49
|
-
i18n (0.5.0)
|
50
|
-
linecache (0.43)
|
51
|
-
mail (2.2.19)
|
52
|
-
activesupport (>= 2.3.6)
|
53
|
-
i18n (>= 0.4.0)
|
54
|
-
mime-types (~> 1.16)
|
55
|
-
treetop (~> 1.4.8)
|
56
|
-
mime-types (1.16)
|
57
|
-
polyglot (0.3.2)
|
58
|
-
rack (1.2.3)
|
59
|
-
rack-mount (0.6.14)
|
60
|
-
rack (>= 1.0.0)
|
61
|
-
rack-test (0.5.7)
|
62
|
-
rack (>= 1.0)
|
63
|
-
rails (3.0.10)
|
64
|
-
actionmailer (= 3.0.10)
|
65
|
-
actionpack (= 3.0.10)
|
66
|
-
activerecord (= 3.0.10)
|
67
|
-
activeresource (= 3.0.10)
|
68
|
-
activesupport (= 3.0.10)
|
69
|
-
bundler (~> 1.0)
|
70
|
-
railties (= 3.0.10)
|
71
|
-
railties (3.0.10)
|
72
|
-
actionpack (= 3.0.10)
|
73
|
-
activesupport (= 3.0.10)
|
74
|
-
rake (>= 0.8.7)
|
75
|
-
rdoc (~> 3.4)
|
76
|
-
thor (~> 0.14.4)
|
77
|
-
rake (0.9.2)
|
78
|
-
rdoc (3.9.2)
|
79
|
-
rspec (2.6.0)
|
80
|
-
rspec-core (~> 2.6.0)
|
81
|
-
rspec-expectations (~> 2.6.0)
|
82
|
-
rspec-mocks (~> 2.6.0)
|
83
|
-
rspec-core (2.6.4)
|
84
|
-
rspec-expectations (2.6.0)
|
85
|
-
diff-lcs (~> 1.1.2)
|
86
|
-
rspec-mocks (2.6.0)
|
87
|
-
ruby-debug (0.10.4)
|
88
|
-
columnize (>= 0.1)
|
89
|
-
ruby-debug-base (~> 0.10.4.0)
|
90
|
-
ruby-debug-base (0.10.4)
|
91
|
-
linecache (>= 0.3)
|
92
|
-
spork (0.8.5)
|
93
|
-
sqlite3 (1.3.4)
|
94
|
-
thor (0.14.6)
|
95
|
-
treetop (1.4.10)
|
96
|
-
polyglot
|
97
|
-
polyglot (>= 0.3.1)
|
98
|
-
tzinfo (0.3.29)
|
99
|
-
wirble (0.1.3)
|
100
|
-
|
101
|
-
PLATFORMS
|
102
|
-
ruby
|
103
|
-
|
104
|
-
DEPENDENCIES
|
105
|
-
activerecord
|
106
|
-
activesupport (~> 3.0)
|
107
|
-
factory_girl_rails
|
108
|
-
rspec
|
109
|
-
ruby-debug
|
110
|
-
spork
|
111
|
-
sqlite3
|
112
|
-
to_factory!
|
113
|
-
wirble
|
data/lib/to_factory/generator.rb
DELETED
@@ -1,115 +0,0 @@
|
|
1
|
-
module ToFactory
|
2
|
-
class MissingActiveRecordInstanceException < Exception;end
|
3
|
-
class MustBeActiveRecordSubClassException < Exception;end
|
4
|
-
|
5
|
-
def self.included base
|
6
|
-
base.class_eval do
|
7
|
-
unless self.ancestors.include? ActiveRecord::Base
|
8
|
-
raise MustBeActiveRecordSubClassException
|
9
|
-
end
|
10
|
-
|
11
|
-
end
|
12
|
-
|
13
|
-
base.instance_eval do
|
14
|
-
define_method :to_factory do
|
15
|
-
Generator.new(self).factory_with_attributes
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
class Generator
|
21
|
-
attr_accessor :model_class, :object_instance
|
22
|
-
|
23
|
-
def initialize object=nil
|
24
|
-
return unless object
|
25
|
-
|
26
|
-
if object.is_a? ActiveRecord::Base
|
27
|
-
@model_class = object.class.to_s
|
28
|
-
@object_instance = object
|
29
|
-
else
|
30
|
-
@model_class = object
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def factory
|
35
|
-
out = "Factory.define :#{name} do |#{abbreviation}|\n"
|
36
|
-
out << yield if block_given?
|
37
|
-
out << "end"
|
38
|
-
end
|
39
|
-
|
40
|
-
def factory_with_attributes
|
41
|
-
factory do
|
42
|
-
to_skip = [:id, :created_at, :updated_at]
|
43
|
-
attributes = object_instance.attributes.delete_if{|key, value| to_skip.include? key.to_sym}
|
44
|
-
|
45
|
-
attributes.map do |attr, value|
|
46
|
-
factory_attribute(attr, value)
|
47
|
-
end.sort.join("\n") << "\n"
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def factory_for options
|
52
|
-
@object_instance = find_from options
|
53
|
-
return nil unless @object_instance
|
54
|
-
factory_with_attributes
|
55
|
-
end
|
56
|
-
|
57
|
-
private
|
58
|
-
def find_from options
|
59
|
-
return model_class.find options if options.is_a? Integer
|
60
|
-
unless options.is_a? Hash
|
61
|
-
raise ArgumentError.new("Expected hash of attributes or integer to look up record")
|
62
|
-
end
|
63
|
-
|
64
|
-
options = options.with_indifferent_access
|
65
|
-
|
66
|
-
if id=options['id'].to_i
|
67
|
-
return model_class.find(id) if id > 0
|
68
|
-
end
|
69
|
-
|
70
|
-
finder, params = finder_and_params_from_options options
|
71
|
-
|
72
|
-
model_class.send finder, *params
|
73
|
-
end
|
74
|
-
|
75
|
-
public
|
76
|
-
|
77
|
-
def factory_attribute attr, value=nil
|
78
|
-
raise MissingActiveRecordInstanceException unless object_instance
|
79
|
-
|
80
|
-
value = object_instance.send attr unless value
|
81
|
-
|
82
|
-
value = "nil" if value.nil?
|
83
|
-
value = "\"#{value}\"" if value.is_a? String
|
84
|
-
" #{abbreviation}.#{attr} #{value}"
|
85
|
-
end
|
86
|
-
|
87
|
-
def name
|
88
|
-
model_class.to_s.underscore
|
89
|
-
end
|
90
|
-
|
91
|
-
private
|
92
|
-
|
93
|
-
def finder_and_params_from_options options
|
94
|
-
params =[]
|
95
|
-
finder = "find_by_"
|
96
|
-
|
97
|
-
options.keys.each do |k|
|
98
|
-
if finder == "find_by_"
|
99
|
-
finder << k
|
100
|
-
else
|
101
|
-
finder << "_and_#{k}"
|
102
|
-
end
|
103
|
-
|
104
|
-
value = options[k]
|
105
|
-
params << value
|
106
|
-
end
|
107
|
-
|
108
|
-
[finder, params]
|
109
|
-
end
|
110
|
-
|
111
|
-
def abbreviation
|
112
|
-
name[0..0]
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|
data/spec/config/database.yml
DELETED
data/spec/db/test.sqlite
DELETED
Binary file
|