to_factory 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. data/.travis.yml +1 -0
  2. data/README.md +8 -11
  3. data/lib/to_factory/collation.rb +40 -14
  4. data/lib/to_factory/file_sync.rb +9 -7
  5. data/lib/to_factory/file_writer.rb +21 -11
  6. data/lib/to_factory/finders/factory.rb +12 -6
  7. data/lib/to_factory/finders/model.rb +25 -9
  8. data/lib/to_factory/generation/attribute.rb +35 -17
  9. data/lib/to_factory/generation/factory.rb +32 -24
  10. data/lib/to_factory/klass_inference.rb +50 -0
  11. data/lib/to_factory/options_parser.rb +34 -0
  12. data/lib/to_factory/parsing/file.rb +1 -0
  13. data/lib/to_factory/parsing/syntax.rb +6 -17
  14. data/lib/to_factory/representation.rb +27 -0
  15. data/lib/to_factory/version.rb +1 -1
  16. data/lib/to_factory.rb +4 -4
  17. data/spec/example_factories/new_syntax/user_admin_root.rb +21 -0
  18. data/spec/example_factories/new_syntax/user_admin_super_admin.rb +4 -4
  19. data/spec/example_factories/old_syntax/user_admin_root.rb +21 -0
  20. data/spec/example_factories/old_syntax/user_admin_super_admin.rb +4 -3
  21. data/spec/integration/file_sync_spec.rb +9 -3
  22. data/spec/integration/file_writer_spec.rb +5 -3
  23. data/spec/integration/to_factory_method_spec.rb +17 -11
  24. data/spec/unit/collation_spec.rb +25 -18
  25. data/spec/unit/file_writer_spec.rb +4 -2
  26. data/spec/unit/finders/factory_spec.rb +2 -3
  27. data/spec/unit/generator_spec.rb +13 -16
  28. data/spec/unit/parsing/file_spec.rb +10 -11
  29. data/spec/unit/parsing/klass_inference_spec.rb +15 -16
  30. data/to_factory.gemspec +0 -1
  31. metadata +21 -32
  32. data/lib/to_factory/definition_group.rb +0 -47
  33. data/lib/to_factory/parsing/klass_inference.rb +0 -34
  34. data/spec/unit/definition_group_spec.rb +0 -19
@@ -1,4 +1,8 @@
1
1
  FactoryGirl.define do
2
+ factory(:"to_factory/user") do
3
+ name "User"
4
+ end
5
+
2
6
  factory(:admin, :parent => :"to_factory/user") do
3
7
  name "Admin"
4
8
  end
@@ -6,8 +10,4 @@ FactoryGirl.define do
6
10
  factory(:super_admin, :parent => :admin) do
7
11
  name "Super Admin"
8
12
  end
9
-
10
- factory(:"to_factory/user") do
11
- name "User"
12
- end
13
13
  end
@@ -0,0 +1,21 @@
1
+ Factory.define(:"to_factory/user") do |o|
2
+ o.name("User")
3
+ end
4
+
5
+ Factory.define(:root, :parent => :"to_factory/user") do |o|
6
+ o.birthday "2014-07-08T15:30Z"
7
+ o.email "test@example.com"
8
+ o.name "Jeff"
9
+ o.some_attributes({:a => 1})
10
+ o.some_id 8
11
+ end
12
+
13
+
14
+ Factory.define(:admin, :parent => :"to_factory/user") do |o|
15
+ o.name("Admin")
16
+ end
17
+
18
+ Factory.define(:super_admin, :parent => :admin) do |o|
19
+ o.name("Super Admin")
20
+ end
21
+
@@ -1,3 +1,7 @@
1
+ Factory.define(:"to_factory/user") do |o|
2
+ o.name "User"
3
+ end
4
+
1
5
  Factory.define(:admin, :parent => :"to_factory/user") do |o|
2
6
  o.name "Admin"
3
7
  end
@@ -6,6 +10,3 @@ Factory.define(:super_admin, :parent => :admin) do |o|
6
10
  o.name "Super Admin"
7
11
  end
8
12
 
9
- Factory.define(:"to_factory/user") do |o|
10
- o.name "User"
11
- end
@@ -47,6 +47,8 @@ describe "FileSync" do
47
47
  end
48
48
  end
49
49
 
50
+
51
+
50
52
  context "with a pre-existing file" do
51
53
  let(:sync) { ToFactory::FileSync.new(user) }
52
54
  before do
@@ -64,9 +66,13 @@ describe "FileSync" do
64
66
  sync.perform
65
67
 
66
68
  parser = ToFactory::Parsing::File.new(user_file)
67
- result = parser.parse[ToFactory::User]
68
- expect(result[:admin]).to match_sexp expected_admin_file
69
- expect(result[:"to_factory/user"]).to match_sexp expected_user_file
69
+ result = parser.parse
70
+ admin= result.find{|r| r.name == "admin"}
71
+ user= result.find{|r| r.name == "to_factory/user"}
72
+
73
+
74
+ expect(admin.definition).to match_sexp expected_admin_file
75
+ expect(user.definition).to match_sexp expected_user_file
70
76
 
71
77
  expect(lambda{
72
78
  sync.perform
@@ -4,13 +4,15 @@ describe ToFactory::FileWriter do
4
4
  let(:user_file_contents) { File.read "./tmp/factories/to_factory/user.rb"}
5
5
  let!(:user) { create_user! }
6
6
  let!(:admin) { create_admin! }
7
+
7
8
  it do
8
9
  fs = ToFactory::FileSync.new
9
- definitions = fs.new_definitions
10
+ representations = fs.all_representations
10
11
  #sanity check generation isn't broken
11
- expect(definitions.keys).to eq [ToFactory::User]
12
+ expect(representations.keys).to eq [ToFactory::User]
13
+ expect(representations.values[0][0]).to be_a ToFactory::Representation
12
14
 
13
- fw.write definitions
15
+ fw.write representations
14
16
  expect(user_file_contents).to match_sexp expected
15
17
  end
16
18
  end
@@ -24,6 +24,23 @@ describe ToFactory do
24
24
  end
25
25
 
26
26
  describe "Object#ToFactory" do
27
+ context "with multiple levels of parent classes" do
28
+ let(:filename) { "spec/example_factories/#{version}_syntax/#{'user_admin_super_admin'}.rb"}
29
+
30
+ it "gets the output order correct" do
31
+ output = "./tmp/factories/to_factory/user.rb"
32
+ `mkdir -p ./tmp/factories/to_factory`
33
+ `cp #{filename} #{output}`
34
+
35
+ ToFactory(:root => user)
36
+
37
+ expected = File.read "spec/example_factories/#{version}_syntax/#{'user_admin_root'}.rb"
38
+
39
+ #user, admin, super_admin, root
40
+ expect(File.read(output)).to match_sexp expected
41
+ end
42
+ end
43
+
27
44
  it "generates all factories" do
28
45
  ToFactory()
29
46
  expect(user_file) .to match_sexp expected_user_file
@@ -51,17 +68,6 @@ describe ToFactory do
51
68
  expect(user_file).to be_present
52
69
  expect(project_file).to be_nil
53
70
  end
54
-
55
- it "doesn't auto generate any if :all is specified" do
56
- ToFactory(:exclude => :all)
57
- expect(user_file).to be_nil
58
- expect(project_file).to be_nil
59
-
60
- #sanity check
61
- ToFactory()
62
- expect(user_file).to be_present
63
- expect(project_file).to be_present
64
- end
65
71
  end
66
72
 
67
73
  context "with no existing file" do
@@ -1,34 +1,41 @@
1
1
  describe ToFactory::Collation do
2
- def perform
3
- ToFactory::Collation.merge(a, b)
4
- end
2
+ describe "detect_collisions!" do
3
+ let(:collation) { ToFactory::Collation.new(a,b) }
4
+ let(:a) { [double(:name => "a")]}
5
+ let(:b) { [double(:name => "a")]}
5
6
 
6
- context "matching keys" do
7
- let(:a) { {:A => {:a => 1}} }
8
- let(:b) { {:B => {:a => 2}} }
7
+ def perform
8
+ collation.detect_collisions!(a, b)
9
+ end
9
10
 
10
11
  it do
11
12
  expect(lambda{perform}).to raise_error ToFactory::AlreadyExists
12
13
  end
13
- end
14
14
 
15
- context "non matching keys" do
16
- let(:a) { {:A => {:a => 1}} }
17
- let(:b) { {:B => {:b => 2}} }
15
+ context "non matching keys" do
16
+ let(:a) { [double(:name => "a")]}
17
+ let(:b) { [double(:name => "b")]}
18
18
 
19
- it do
20
- result = {:A => {:a => 1}, :B => {:b => 2}}.with_indifferent_access
21
- expect(perform).to eq result
19
+ it do
20
+ expect(perform).to eq nil
21
+ end
22
22
  end
23
23
  end
24
24
 
25
- context "merging" do
26
- let(:a) { {:A => {:a => 1}} }
27
- let(:b) { {:A => {:b => 2}} }
25
+ context "organizing" do
26
+ let(:root) { ToFactory::Representation.new(:root, "super_admin", " Factory.define(:root, :parent => :\"to_factory/user\") do|o|\n o.birthday \"2014-07-08T15:30Z\"\n o.email \"test@example.com\"\n o.name \"Jeff\"\n o.some_id 8\n end\n") }
27
+ let(:user) { ToFactory::Representation.new("to_factory/user", nil, "Factory.define(:\"to_factory/user\") { |o| o.name(\"User\") }") }
28
+ let(:admin) { ToFactory::Representation.new("admin", "to_factory/user", "Factory.define(:admin, :parent => :\"to_factory/user\") { |o| o.name(\"Admin\") }") }
29
+ let(:super_admin) { ToFactory::Representation.new("super_admin", "admin", "Factory.define(:super_admin, :parent => :admin) { |o| o.name(\"Super Admin\") }") }
28
30
 
29
31
  it do
30
- result = {:A => {:a => 1, :b => 2}}.with_indifferent_access
31
- expect(perform).to eq result
32
+ new_definitions = [ root ]
33
+ pre_existing = [ admin, user, super_admin ]
34
+
35
+ result = ToFactory::Collation.organize(new_definitions, pre_existing)
36
+ result = result[ToFactory::User]
37
+ expect(result.map &:hierarchy_order).to eq [1,2,3,4]
38
+ expect(result).to eq [user, admin, super_admin, root]
32
39
  end
33
40
  end
34
41
  end
@@ -11,8 +11,10 @@ describe ToFactory::FileWriter do
11
11
  end
12
12
 
13
13
  it "adds factories for all models" do
14
- fw.write({ToFactory::User =>{:user => "factory a"},
15
- ToFactory::Project => {:project =>"factory b"}
14
+ user_representation = double :name => :user, "definition" => "factory a"
15
+ project_representation = double :name => "project", :definition =>"factory b"
16
+ fw.write({ToFactory::User =>[user_representation],
17
+ ToFactory::Project => [project_representation]
16
18
  })
17
19
 
18
20
  if ToFactory.new_syntax?
@@ -13,12 +13,11 @@ describe ToFactory::Finders::Factory do
13
13
  finder = ToFactory::Finders::Factory.new
14
14
 
15
15
  result = finder.call
16
- result = result[ToFactory::User]
17
16
 
18
- expect(result[:"to_factory/user"]).
17
+ expect(result[0].definition).
19
18
  to match_sexp user_file_contents
20
19
 
21
- expect(result[:admin]).
20
+ expect(result[1].definition).
22
21
  to match_sexp admin_file_contents
23
22
  end
24
23
  end
@@ -10,26 +10,23 @@ describe ToFactory::Generation::Factory do
10
10
 
11
11
  let!(:user) { create_user! }
12
12
 
13
- let(:generator) { ToFactory::Generation::Factory.new user, :"to_factory/user" }
13
+ let(:representation) { ToFactory::Representation.from(user) }
14
+ let(:generator) { ToFactory::Generation::Factory.new representation }
15
+
14
16
 
15
- describe ".new" do
16
- it "requires an activerecord instance" do
17
- expect(lambda{ToFactory::Generation::Factory.new "", ""}).to raise_error ToFactory::MissingActiveRecordInstanceException
18
- end
19
- end
20
17
 
21
18
  describe "#header" do
22
19
  it do
23
20
  if ToFactory.new_syntax?
24
- expect(generator.header{}).to match_sexp <<-eof.strip_heredoc
25
- factory(:"to_factory/user") do
26
- end
27
- eof
21
+ expect(generator.header{}).to match_sexp <<-eof.strip_heredoc
22
+ factory(:"to_factory/user") do
23
+ end
24
+ eof
28
25
  else
29
- expect(generator.header{}).to match_sexp <<-eof.strip_heredoc
30
- Factory.define(:"to_factory/user") do |o|
31
- end
32
- eof
26
+ expect(generator.header{}).to match_sexp <<-eof.strip_heredoc
27
+ Factory.define(:"to_factory/user") do |o|
28
+ end
29
+ eof
33
30
  end
34
31
  end
35
32
  end
@@ -64,8 +61,8 @@ describe ToFactory::Generation::Factory do
64
61
 
65
62
  it do
66
63
  expect(generator.to_factory).to match_sexp expected
67
- result = ToFactory(user).values.first.values.first
68
- expect(result).to match_sexp expected
64
+ result = ToFactory(user).values.first.first
65
+ expect(result.definition).to match_sexp expected
69
66
  end
70
67
  end
71
68
  end
@@ -42,7 +42,7 @@ describe ToFactory::Parsing::File do
42
42
  it do
43
43
  result = parser.parse
44
44
 
45
- expect(result[ToFactory::User].keys).to match_array [:super_admin, :admin, :"to_factory/user"]
45
+ expect(result.map(&:name)).to match_array ["super_admin", "admin", "to_factory/user"]
46
46
  end
47
47
  end
48
48
 
@@ -50,9 +50,9 @@ describe ToFactory::Parsing::File do
50
50
  let(:filename) { "spec/example_factories/#{version}_syntax/#{'user'}.rb"}
51
51
 
52
52
  it do
53
- result = parser.parse[ToFactory::User]
53
+ result = parser.parse
54
54
 
55
- expect(result[:"to_factory/user" ]).to match_sexp user_contents
55
+ expect(result.first.definition).to match_sexp user_contents
56
56
  end
57
57
  end
58
58
 
@@ -60,9 +60,9 @@ describe ToFactory::Parsing::File do
60
60
  let(:filename) { "spec/example_factories/#{version}_syntax/#{'user_with_header'}.rb"}
61
61
 
62
62
  it do
63
- result = parser.parse[ToFactory::User]
63
+ result = parser.parse
64
64
 
65
- expect(result[:"to_factory/user" ]).to match_sexp user_contents
65
+ expect(result.first.definition).to match_sexp user_contents
66
66
  end
67
67
  end
68
68
 
@@ -72,11 +72,10 @@ describe ToFactory::Parsing::File do
72
72
  it do
73
73
  result = parser.parse
74
74
 
75
- expect(result.keys).to match_array [ToFactory::User]
76
- users = result[ToFactory::User]
75
+ user, admin = result
77
76
 
78
- expect(users[:"to_factory/user" ]).to match_sexp user_contents
79
- expect(users[:admin]).to match_sexp admin_contents
77
+ expect(user.definition).to match_sexp user_contents
78
+ expect(admin.definition).to match_sexp admin_contents
80
79
  end
81
80
  end
82
81
 
@@ -84,9 +83,9 @@ describe ToFactory::Parsing::File do
84
83
  let(:filename) { "spec/example_factories/#{version}_syntax/#{'user_admin_with_header'}.rb"}
85
84
 
86
85
  it do
87
- result = parser.parse[ToFactory::User]
86
+ result = parser.parse
88
87
 
89
- expect(result[:"to_factory/user" ]).to match_sexp user_contents
88
+ expect(result.first.definition).to match_sexp user_contents
90
89
  end
91
90
  end
92
91
  end
@@ -1,24 +1,23 @@
1
- describe ToFactory::Parsing::KlassInference do
2
- let(:inference) { ToFactory::Parsing::KlassInference.new }
1
+ describe ToFactory::KlassInference do
2
+ let(:inference) { ToFactory::KlassInference.new representations }
3
3
 
4
- mappings = #klass, parent, result
5
- [:admin, :"to_factory/user", ToFactory::User],
6
- [:some_project, :"to_factory/project", ToFactory::Project],
7
- [:sub_project, :"some_project", ToFactory::Project],
8
- [:super_admin, :admin, ToFactory::User],
9
- [:"to_factory/user", nil, ToFactory::User]
4
+ mappings = [
5
+ [:super_admin, :admin, ToFactory::User, 3],
6
+ [:"to_factory/user", nil, ToFactory::User, 1],
7
+ [:admin, :"to_factory/user", ToFactory::User, 2],
8
+ [:some_project, :"to_factory/project", ToFactory::Project, 2],
9
+ [:sub_project, :"some_project", ToFactory::Project, 3],
10
+ ]
10
11
 
11
12
 
12
- to_infer = mappings.map{|a| a[0..1]}
13
+ let(:representations) { mappings.map{|name, parent, _| ToFactory::Representation.new(name,parent) }}
13
14
 
14
- before do
15
- inference.setup to_infer
16
- end
15
+ mappings.each do |name, parent_name, expected_klass, expected_order|
16
+ it "having #{name} and #{parent_name.inspect} implies #{expected_klass} #{expected_order} "do
17
+ result_klass, order = inference.infer(name.to_s)
17
18
 
18
- mappings.each do |klass, parent, expected|
19
- it "having #{klass} and #{parent.inspect} implies #{expected} "do
20
- result = inference.infer(klass)
21
- expect(result).to eq expected
19
+ expect(result_klass).to eq expected_klass
20
+ expect(order ).to eq expected_order
22
21
  end
23
22
  end
24
23
 
data/to_factory.gemspec CHANGED
@@ -34,7 +34,6 @@ Gem::Specification.new do |spec|
34
34
  old_active_record = RUBY_VERSION =~ /^1\.8|^1.9\.[1|2]/
35
35
 
36
36
  spec.add_dependency "ruby2ruby"
37
- spec.add_dependency "deep_merge"
38
37
 
39
38
  if old_active_record
40
39
  spec.add_dependency 'activerecord', ">2.0", "< 4.0"
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: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Mark Burns
@@ -77,24 +77,10 @@ dependencies:
77
77
  version: "0"
78
78
  type: :runtime
79
79
  version_requirements: *id004
80
- - !ruby/object:Gem::Dependency
81
- name: deep_merge
82
- prerelease: false
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
80
  - !ruby/object:Gem::Dependency
95
81
  name: activerecord
96
82
  prerelease: false
97
- requirement: &id006 !ruby/object:Gem::Requirement
83
+ requirement: &id005 !ruby/object:Gem::Requirement
98
84
  none: false
99
85
  requirements:
100
86
  - - ">"
@@ -112,11 +98,11 @@ dependencies:
112
98
  - 0
113
99
  version: "4.0"
114
100
  type: :runtime
115
- version_requirements: *id006
101
+ version_requirements: *id005
116
102
  - !ruby/object:Gem::Dependency
117
103
  name: i18n
118
104
  prerelease: false
119
- requirement: &id007 !ruby/object:Gem::Requirement
105
+ requirement: &id006 !ruby/object:Gem::Requirement
120
106
  none: false
121
107
  requirements:
122
108
  - - <
@@ -127,11 +113,11 @@ dependencies:
127
113
  - 7
128
114
  version: "0.7"
129
115
  type: :runtime
130
- version_requirements: *id007
116
+ version_requirements: *id006
131
117
  - !ruby/object:Gem::Dependency
132
118
  name: factory_girl
133
119
  prerelease: false
134
- requirement: &id008 !ruby/object:Gem::Requirement
120
+ requirement: &id007 !ruby/object:Gem::Requirement
135
121
  none: false
136
122
  requirements:
137
123
  - - <
@@ -142,11 +128,11 @@ dependencies:
142
128
  - 0
143
129
  version: "2.0"
144
130
  type: :development
145
- version_requirements: *id008
131
+ version_requirements: *id007
146
132
  - !ruby/object:Gem::Dependency
147
133
  name: rspec
148
134
  prerelease: false
149
- requirement: &id009 !ruby/object:Gem::Requirement
135
+ requirement: &id008 !ruby/object:Gem::Requirement
150
136
  none: false
151
137
  requirements:
152
138
  - - ~>
@@ -157,11 +143,11 @@ dependencies:
157
143
  - 0
158
144
  version: "3.0"
159
145
  type: :development
160
- version_requirements: *id009
146
+ version_requirements: *id008
161
147
  - !ruby/object:Gem::Dependency
162
148
  name: rake
163
149
  prerelease: false
164
- requirement: &id010 !ruby/object:Gem::Requirement
150
+ requirement: &id009 !ruby/object:Gem::Requirement
165
151
  none: false
166
152
  requirements:
167
153
  - - ~>
@@ -172,7 +158,7 @@ dependencies:
172
158
  - 0
173
159
  version: "10.0"
174
160
  type: :development
175
- version_requirements: *id010
161
+ version_requirements: *id009
176
162
  description: Autogenerate and append/create factory_girl definitions from the console
177
163
  email:
178
164
  - markthedeveloper@gmail.com
@@ -194,18 +180,19 @@ files:
194
180
  - lib/to_factory.rb
195
181
  - lib/to_factory/collation.rb
196
182
  - lib/to_factory/config.rb
197
- - lib/to_factory/definition_group.rb
198
183
  - lib/to_factory/file_sync.rb
199
184
  - lib/to_factory/file_writer.rb
200
185
  - lib/to_factory/finders/factory.rb
201
186
  - lib/to_factory/finders/model.rb
202
187
  - lib/to_factory/generation/attribute.rb
203
188
  - lib/to_factory/generation/factory.rb
189
+ - lib/to_factory/klass_inference.rb
190
+ - lib/to_factory/options_parser.rb
204
191
  - lib/to_factory/parsing/file.rb
205
- - lib/to_factory/parsing/klass_inference.rb
206
192
  - lib/to_factory/parsing/new_syntax.rb
207
193
  - lib/to_factory/parsing/old_syntax.rb
208
194
  - lib/to_factory/parsing/syntax.rb
195
+ - lib/to_factory/representation.rb
209
196
  - lib/to_factory/version.rb
210
197
  - spec/db/migrate/1_create_users.rb
211
198
  - spec/db/migrate/2_create_projects.rb
@@ -217,6 +204,7 @@ files:
217
204
  - spec/example_factories/new_syntax/project_with_header.rb
218
205
  - spec/example_factories/new_syntax/user.rb
219
206
  - spec/example_factories/new_syntax/user_admin.rb
207
+ - spec/example_factories/new_syntax/user_admin_root.rb
220
208
  - spec/example_factories/new_syntax/user_admin_super_admin.rb
221
209
  - spec/example_factories/new_syntax/user_admin_with_header.rb
222
210
  - spec/example_factories/new_syntax/user_with_header.rb
@@ -224,6 +212,7 @@ files:
224
212
  - spec/example_factories/old_syntax/project_with_header.rb
225
213
  - spec/example_factories/old_syntax/user.rb
226
214
  - spec/example_factories/old_syntax/user_admin.rb
215
+ - spec/example_factories/old_syntax/user_admin_root.rb
227
216
  - spec/example_factories/old_syntax/user_admin_super_admin.rb
228
217
  - spec/example_factories/old_syntax/user_admin_with_header.rb
229
218
  - spec/example_factories/old_syntax/user_with_header.rb
@@ -239,7 +228,6 @@ files:
239
228
  - spec/support/models/project.rb
240
229
  - spec/support/models/user.rb
241
230
  - spec/unit/collation_spec.rb
242
- - spec/unit/definition_group_spec.rb
243
231
  - spec/unit/file_writer_spec.rb
244
232
  - spec/unit/finders/factory_spec.rb
245
233
  - spec/unit/finders/model_spec.rb
@@ -294,6 +282,7 @@ test_files:
294
282
  - spec/example_factories/new_syntax/project_with_header.rb
295
283
  - spec/example_factories/new_syntax/user.rb
296
284
  - spec/example_factories/new_syntax/user_admin.rb
285
+ - spec/example_factories/new_syntax/user_admin_root.rb
297
286
  - spec/example_factories/new_syntax/user_admin_super_admin.rb
298
287
  - spec/example_factories/new_syntax/user_admin_with_header.rb
299
288
  - spec/example_factories/new_syntax/user_with_header.rb
@@ -301,6 +290,7 @@ test_files:
301
290
  - spec/example_factories/old_syntax/project_with_header.rb
302
291
  - spec/example_factories/old_syntax/user.rb
303
292
  - spec/example_factories/old_syntax/user_admin.rb
293
+ - spec/example_factories/old_syntax/user_admin_root.rb
304
294
  - spec/example_factories/old_syntax/user_admin_super_admin.rb
305
295
  - spec/example_factories/old_syntax/user_admin_with_header.rb
306
296
  - spec/example_factories/old_syntax/user_with_header.rb
@@ -316,7 +306,6 @@ test_files:
316
306
  - spec/support/models/project.rb
317
307
  - spec/support/models/user.rb
318
308
  - spec/unit/collation_spec.rb
319
- - spec/unit/definition_group_spec.rb
320
309
  - spec/unit/file_writer_spec.rb
321
310
  - spec/unit/finders/factory_spec.rb
322
311
  - spec/unit/finders/model_spec.rb
@@ -1,47 +0,0 @@
1
- module ToFactory
2
- class DefinitionGroup
3
- def self.perform(instances)
4
- new.perform(instances)
5
- end
6
-
7
- def perform(instances)
8
- instances.inject({}) do |result, record|
9
- klass, name, definition = define_factory(record)
10
- result[klass] ||= {}
11
- result[klass][name] = definition
12
- result
13
- end
14
- end
15
-
16
- def define_factory(record)
17
- name, item, parent_klass = extract_details(record)
18
-
19
- definition = ToFactory::Generation::Factory.new(item, name).to_factory(parent_klass)
20
-
21
- [item.class, name, definition]
22
- end
23
-
24
- def calculate_name(klass)
25
- klass.name.to_s.underscore
26
- end
27
-
28
- private
29
-
30
- def extract_details(record)
31
- if record.is_a?(ActiveRecord::Base)
32
- name = calculate_name record.class
33
-
34
- [name, record, nil]
35
- elsif record.is_a?(Array)
36
- name, item = record
37
- parent_klass_name = calculate_name(item.class)
38
- parent_klass_name = nil if parent_klass_name.to_s == name.to_s
39
- [name, item, parent_klass_name]
40
- else
41
- raise NotImplemented
42
- end
43
- end
44
- end
45
- end
46
-
47
-
@@ -1,34 +0,0 @@
1
- module ToFactory
2
- module Parsing
3
- CannotInferClass = Class.new ArgumentError
4
-
5
- class KlassInference
6
- def initialize
7
- @mapping = {}
8
- end
9
-
10
- def setup(factory_names_and_parents)
11
- factory_names_and_parents.each do |factory_name, parent|
12
- @mapping[factory_name.to_s] = to_const(parent || factory_name)
13
- end
14
- end
15
-
16
- def infer(factory_name)
17
- result = @mapping[factory_name.to_s]
18
- return result if result.is_a? Class
19
-
20
- raise CannotInferClass.new(factory_name) if result.nil?
21
-
22
- infer(result)
23
- end
24
-
25
- private
26
-
27
- def to_const(factory_name)
28
- factory_name.to_s.camelize.constantize
29
- rescue NameError
30
- factory_name
31
- end
32
- end
33
- end
34
- end
@@ -1,19 +0,0 @@
1
- describe ToFactory::DefinitionGroup do
2
- let(:user) { create_user! }
3
- let(:admin) { create_admin! }
4
- let(:user_definition) { File.read "./spec/example_factories/#{version}_syntax/user.rb"}
5
- let(:admin_definition) { File.read "./spec/example_factories/#{version}_syntax/admin.rb"}
6
- let(:group) { ToFactory::DefinitionGroup.new }
7
-
8
- it do
9
- result = group.perform([[:"to_factory/user", user], [:admin, admin]])
10
-
11
- expect(result.keys[0]).to eq ToFactory::User
12
- expect(result.values[0][:"to_factory/user"]).to match_sexp user_definition
13
- expect(result.values[0][:admin]).to match_sexp admin_definition
14
- end
15
-
16
- it "#calculate_name" do
17
- expect(group.calculate_name(ToFactory::Project)).to eq "to_factory/project"
18
- end
19
- end