to_factory 2.0.0 → 3.0.0.pre.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/test.yml +35 -0
- data/.gitignore +1 -0
- data/.rubocop-disabled.yml +95 -0
- data/.rubocop-enabled.yml +1194 -0
- data/.rubocop.yml +938 -0
- data/CHANGELOG.md +11 -0
- data/Gemfile +6 -1
- data/README.md +11 -10
- data/Rakefile +17 -9
- data/lib/to_factory/collation.rb +9 -9
- data/lib/to_factory/file_sync.rb +11 -11
- data/lib/to_factory/file_writer.rb +18 -2
- data/lib/to_factory/finders/factory.rb +8 -2
- data/lib/to_factory/finders/model.rb +11 -13
- data/lib/to_factory/generation/attribute.rb +10 -12
- data/lib/to_factory/generation/factory.rb +3 -3
- data/lib/to_factory/klass_inference.rb +4 -6
- data/lib/to_factory/options_parser.rb +4 -4
- data/lib/to_factory/parsing/file.rb +87 -14
- data/lib/to_factory/representation.rb +9 -6
- data/lib/to_factory/version.rb +1 -1
- data/lib/to_factory.rb +7 -17
- data/spec/db/migrate/1_create_users.rb +4 -4
- data/spec/db/migrate/2_create_projects.rb +4 -4
- data/spec/db/migrate/3_create_not_namespaced.rb +4 -4
- data/spec/db/migrate/4_add_birthday_to_users.rb +3 -3
- data/spec/db/migrate/5_add_serialized_attributes_to_users.rb +3 -3
- data/spec/example_factories/admin.rb +6 -6
- data/spec/example_factories/admin_with_header.rb +7 -7
- data/spec/example_factories/inherited_project_with_header.rb +7 -0
- data/spec/example_factories/project_with_header.rb +4 -4
- data/spec/example_factories/user.rb +5 -5
- data/spec/example_factories/user_admin.rb +11 -11
- data/spec/example_factories/user_admin_root.rb +12 -12
- data/spec/example_factories/user_admin_super_admin.rb +6 -6
- data/spec/example_factories/user_admin_with_header.rb +12 -12
- data/spec/example_factories/user_with_header.rb +6 -6
- data/spec/integration/empty_factory_file_spec.rb +19 -0
- data/spec/integration/file_sync_spec.rb +16 -24
- data/spec/integration/file_writer_spec.rb +13 -9
- data/spec/integration/lint_spec.rb +4 -3
- data/spec/integration/multiple_to_factory_calls_spec.rb +96 -0
- data/spec/integration/non_active_record_classes_spec.rb +40 -0
- data/spec/integration/to_factory_method_spec.rb +21 -20
- data/spec/spec_helper.rb +9 -18
- data/spec/support/broken_models/invalid_ruby_file.rb +1 -0
- data/spec/support/broken_models/project.rb +3 -0
- data/spec/support/data_creation.rb +11 -13
- data/spec/support/match_sexp.rb +0 -1
- data/spec/support/non_active_record/inherited_project.rb +3 -0
- data/spec/support/non_active_record/project.rb +3 -0
- data/spec/support/non_active_record/some_other_service_inheriting_from_something_else.rb +4 -0
- data/spec/support/non_active_record/some_service.rb +2 -0
- data/spec/support/non_active_record/something_else.rb +2 -0
- data/spec/support/ruby_parser_exception_causing_string.rb +29 -29
- data/spec/unit/collation_spec.rb +9 -10
- data/spec/unit/file_writer_spec.rb +4 -8
- data/spec/unit/finders/factory_spec.rb +7 -9
- data/spec/unit/finders/model_spec.rb +35 -8
- data/spec/unit/generation/attribute_spec.rb +22 -22
- data/spec/unit/generation/factory_spec.rb +38 -14
- data/spec/unit/parsing/file_spec.rb +9 -9
- data/spec/unit/parsing/klass_inference_spec.rb +5 -7
- data/to_factory.gemspec +17 -15
- metadata +109 -44
- data/.travis.yml +0 -14
- data/lib/to_factory/parsing/syntax.rb +0 -83
data/CHANGELOG.md
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -17,9 +17,17 @@ If you find yourself retro-fitting tests this gem will save you some of the legw
|
|
17
17
|
* adhoc generate from existing records
|
18
18
|
* unintrusively update factory files in place
|
19
19
|
* display factory definition for a record
|
20
|
-
* parse and write `
|
20
|
+
* parse and write `FactoryBot` syntax
|
21
|
+
|
22
|
+
Tested against Ruby 1.8.7, 1.9.2, 1.9.3, 2.0.0, 2.1.x, 2.2.x
|
23
|
+
|
24
|
+
## Warning :warning:
|
25
|
+
`ToFactory` writes into the `spec/factories` folder. Whilst it
|
26
|
+
is tested and avoids overwriting existing factories,
|
27
|
+
it is recommended that you execute after committing or when in a known
|
28
|
+
safe state.
|
29
|
+
|
21
30
|
|
22
|
-
Tested against Ruby 1.8.7, 1.9.2, 1.9.3, 2.0.0, 2.1.x, 2.2.0
|
23
31
|
|
24
32
|
|
25
33
|
## Installation :file_folder:
|
@@ -33,13 +41,6 @@ group :test, :development do
|
|
33
41
|
end
|
34
42
|
```
|
35
43
|
|
36
|
-
## Warning :warning:
|
37
|
-
`ToFactory` writes into the `spec/factories` folder. Whilst it
|
38
|
-
is tested and avoids overwriting existing factories,
|
39
|
-
it is recommended that you execute after committing or when in a known
|
40
|
-
safe state.
|
41
|
-
|
42
|
-
|
43
44
|
|
44
45
|
```bash
|
45
46
|
git add spec/factories
|
@@ -67,7 +68,7 @@ ToFactory(exclude: [User, Project])
|
|
67
68
|
ToFactory User.last
|
68
69
|
|
69
70
|
#writes to spec/factories/user.rb
|
70
|
-
|
71
|
+
FactoryBot.define
|
71
72
|
factory(:user) do |u|
|
72
73
|
email "test@example.com"
|
73
74
|
name "Mike"
|
data/Rakefile
CHANGED
@@ -1,34 +1,42 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
|
+
require 'byebug'
|
2
3
|
|
3
4
|
namespace :spec do
|
4
5
|
def setup_db
|
5
|
-
require
|
6
|
-
require
|
7
|
-
ActiveRecord::Base.establish_connection(:
|
8
|
-
ActiveRecord::Base.logger = Logger.new(File.open(
|
6
|
+
require "logger"
|
7
|
+
require "active_record"
|
8
|
+
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: "spec/db/test.sqlite3")
|
9
|
+
ActiveRecord::Base.logger = Logger.new(File.open("tmp/database.log", "a"))
|
10
|
+
ActiveRecord::Migrator.migrations_paths = File.expand_path("./spec/db/migrate")
|
9
11
|
end
|
10
12
|
|
11
13
|
desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x"
|
12
14
|
task :migrate_db do
|
13
15
|
setup_db
|
14
|
-
ActiveRecord::
|
16
|
+
ActiveRecord::Tasks::DatabaseTasks.migrate
|
15
17
|
end
|
16
18
|
|
17
19
|
desc "Migrate down"
|
18
20
|
task :migrate_down do
|
19
21
|
setup_db
|
20
|
-
ActiveRecord::Migrator.down
|
22
|
+
ActiveRecord::Migrator.new.migrate(:down)
|
21
23
|
end
|
22
24
|
end
|
23
25
|
|
24
26
|
begin
|
25
|
-
require
|
27
|
+
require "rspec/core/rake_task"
|
26
28
|
RSpec::Core::RakeTask.new do |t|
|
27
29
|
t.rspec_opts = ["-c", "-f progress", "-r ./spec/spec_helper.rb"]
|
28
|
-
t.pattern =
|
30
|
+
t.pattern = "spec/**/*_spec.rb"
|
29
31
|
end
|
30
|
-
task :
|
32
|
+
task default: :spec
|
31
33
|
rescue LoadError
|
32
34
|
|
33
35
|
end
|
34
36
|
|
37
|
+
begin
|
38
|
+
require "rubocop/rake_task"
|
39
|
+
|
40
|
+
RuboCop::RakeTask.new
|
41
|
+
rescue LoadError
|
42
|
+
end
|
data/lib/to_factory/collation.rb
CHANGED
@@ -2,11 +2,11 @@ module ToFactory
|
|
2
2
|
AlreadyExists = Class.new ArgumentError
|
3
3
|
|
4
4
|
class Collation
|
5
|
-
def self.organize(a,b)
|
5
|
+
def self.organize(a, b)
|
6
6
|
new(a, b).organize
|
7
7
|
end
|
8
8
|
|
9
|
-
def self.representations_from(a,b)
|
9
|
+
def self.representations_from(a, b)
|
10
10
|
new(a, b).representations
|
11
11
|
end
|
12
12
|
|
@@ -16,14 +16,14 @@ module ToFactory
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def organize
|
19
|
-
representations.group_by{|i| i.klass.name.underscore}.inject({}) do |o, (klass_name,r)|
|
20
|
-
o[klass_name] = r.sort_by
|
19
|
+
representations.group_by { |i| i.klass.name.underscore }.inject({}) do |o, (klass_name, r)|
|
20
|
+
o[klass_name] = r.sort_by { |r| [r.hierarchy_order, r.name] }
|
21
21
|
o
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
25
|
def representations
|
26
|
-
detect_collisions!(@a
|
26
|
+
detect_collisions!(@a, @b)
|
27
27
|
|
28
28
|
inference = KlassInference.new(merged)
|
29
29
|
|
@@ -36,7 +36,7 @@ module ToFactory
|
|
36
36
|
merged
|
37
37
|
end
|
38
38
|
|
39
|
-
def detect_collisions!(a,b)
|
39
|
+
def detect_collisions!(a, b)
|
40
40
|
collisions = []
|
41
41
|
a.each do |x|
|
42
42
|
b.each do |y|
|
@@ -53,8 +53,8 @@ module ToFactory
|
|
53
53
|
@merged ||= @a + @b
|
54
54
|
end
|
55
55
|
|
56
|
-
|
57
|
-
|
58
|
-
|
56
|
+
def raise_already_exists!(keys)
|
57
|
+
fail ToFactory::AlreadyExists.new "an item for each of the following keys #{keys.inspect} already exists"
|
58
|
+
end
|
59
59
|
end
|
60
60
|
end
|
data/lib/to_factory/file_sync.rb
CHANGED
@@ -8,20 +8,20 @@ module ToFactory
|
|
8
8
|
@factory_finder = ff
|
9
9
|
end
|
10
10
|
|
11
|
-
def perform(exclusions=[])
|
11
|
+
def perform(exclusions = [])
|
12
12
|
@file_writer.write(all_representations exclusions)
|
13
13
|
end
|
14
14
|
|
15
|
-
def all_representations(exclusions=[])
|
15
|
+
def all_representations(exclusions = [])
|
16
16
|
Collation.organize(
|
17
17
|
new_representations(exclusions),
|
18
18
|
existing_representations)
|
19
19
|
end
|
20
20
|
|
21
|
-
def new_representations(exclusions=[])
|
22
|
-
instances = @model_finder.call(exclusions)
|
21
|
+
def new_representations(exclusions = [])
|
22
|
+
instances = @model_finder.call(exclusions: exclusions)
|
23
23
|
|
24
|
-
instances.map{|r| Representation.from(r) }
|
24
|
+
instances.map { |r| Representation.from(r) }
|
25
25
|
end
|
26
26
|
|
27
27
|
private
|
@@ -34,16 +34,16 @@ module ToFactory
|
|
34
34
|
if m.respond_to?(:call)
|
35
35
|
m
|
36
36
|
else
|
37
|
-
lambda
|
37
|
+
lambda do|exclusions|
|
38
38
|
exclusions ||= []
|
39
39
|
records = if m.is_a?(ActiveRecord::Base)
|
40
|
-
|
41
|
-
|
42
|
-
|
40
|
+
Array m
|
41
|
+
else
|
42
|
+
m
|
43
43
|
end
|
44
44
|
|
45
|
-
records.reject{|o,_| exclusions.include?(o.class)}
|
46
|
-
|
45
|
+
records.reject { |o, _| exclusions.include?(o.class) }
|
46
|
+
end
|
47
47
|
end
|
48
48
|
end
|
49
49
|
end
|
@@ -23,9 +23,25 @@ module ToFactory
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def wrap_factories(definitions)
|
26
|
-
out = "
|
27
|
-
out << definitions.join("\n\n")
|
26
|
+
out = "FactoryBot.define do\n"
|
27
|
+
out << indent(definitions).join("\n\n")
|
28
|
+
out << "\n" unless out[-1] == "\n"
|
28
29
|
out << "end"
|
30
|
+
out << "\n" unless out[-1] == "\n"
|
31
|
+
end
|
32
|
+
|
33
|
+
def indent(definitions)
|
34
|
+
definitions.map do |d|
|
35
|
+
if d[/\A\s\s/]
|
36
|
+
d
|
37
|
+
else
|
38
|
+
lines = d.split("\n").map do |l|
|
39
|
+
" #{l}"
|
40
|
+
end
|
41
|
+
|
42
|
+
lines.join("\n")
|
43
|
+
end
|
44
|
+
end
|
29
45
|
end
|
30
46
|
|
31
47
|
def mkdir(name)
|
@@ -15,8 +15,14 @@ module ToFactory
|
|
15
15
|
|
16
16
|
def parsed_files
|
17
17
|
Dir.glob(File.join(ToFactory.factories, "**/*.rb")).map do |f|
|
18
|
-
|
19
|
-
end
|
18
|
+
parse_file(f)
|
19
|
+
end.compact
|
20
|
+
end
|
21
|
+
|
22
|
+
def parse_file(f)
|
23
|
+
ToFactory::Parsing::File.parse(f)
|
24
|
+
rescue ToFactory::Parsing::File::EmptyFileException => e
|
25
|
+
# ignore empty files
|
20
26
|
end
|
21
27
|
end
|
22
28
|
end
|
@@ -1,10 +1,11 @@
|
|
1
1
|
module ToFactory
|
2
2
|
module Finders
|
3
3
|
class Model
|
4
|
-
def call(exclusions
|
4
|
+
def call(exclusions: [], klasses: nil)
|
5
5
|
instances = []
|
6
|
+
klasses ||= find_klasses(exclusions)
|
6
7
|
|
7
|
-
|
8
|
+
klasses.each do |klass|
|
8
9
|
if instance = get_active_record_instance(klass)
|
9
10
|
instances << instance
|
10
11
|
end
|
@@ -15,14 +16,17 @@ module ToFactory
|
|
15
16
|
|
16
17
|
private
|
17
18
|
|
18
|
-
def
|
19
|
+
def find_klasses(exclusions)
|
20
|
+
klasses = []
|
19
21
|
models.each do |file|
|
20
22
|
matching_lines(file) do |match|
|
21
23
|
klass = rescuing_require(file, match)
|
22
24
|
|
23
|
-
|
25
|
+
klasses << klass unless exclusions.include?(klass)
|
24
26
|
end
|
25
27
|
end
|
28
|
+
|
29
|
+
klasses
|
26
30
|
end
|
27
31
|
|
28
32
|
def models
|
@@ -48,15 +52,9 @@ module ToFactory
|
|
48
52
|
end
|
49
53
|
|
50
54
|
def get_active_record_instance(klass)
|
51
|
-
if klass && klass.ancestors.include?(ActiveRecord::Base)
|
52
|
-
|
53
|
-
|
54
|
-
rescue
|
55
|
-
klass.find(:first)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
rescue Exception => e
|
59
|
-
warn "Failed to get record from #{klass} #{e.message}"
|
55
|
+
klass.first if klass && klass.ancestors.include?(ActiveRecord::Base)
|
56
|
+
rescue StandardError => e
|
57
|
+
warn "Failed to get record from #{klass} #{e.message.inspect}"
|
60
58
|
end
|
61
59
|
end
|
62
60
|
end
|
@@ -1,6 +1,8 @@
|
|
1
1
|
module ToFactory
|
2
2
|
module Generation
|
3
3
|
class Attribute
|
4
|
+
attr_writer :parser
|
5
|
+
|
4
6
|
def initialize(attribute, value)
|
5
7
|
@attribute = attribute
|
6
8
|
@value = value
|
@@ -11,24 +13,22 @@ module ToFactory
|
|
11
13
|
" #{setter}"
|
12
14
|
end
|
13
15
|
|
14
|
-
def inspect_value(value, nested=false)
|
16
|
+
def inspect_value(value, nested = false)
|
15
17
|
formatted = format(value, nested)
|
16
18
|
|
17
|
-
if !value.is_a?(Hash) && !nested
|
18
|
-
formatted = " #{formatted}"
|
19
|
-
end
|
19
|
+
formatted = " { #{formatted} }" if !value.is_a?(Hash) && !nested
|
20
20
|
|
21
21
|
formatted
|
22
22
|
end
|
23
23
|
|
24
|
-
def format(value, nested=false)
|
24
|
+
def format(value, nested = false)
|
25
25
|
case value
|
26
26
|
when Time, DateTime
|
27
27
|
inspect_time(value)
|
28
28
|
when Date
|
29
29
|
value.to_s.inspect
|
30
30
|
when BigDecimal
|
31
|
-
value.
|
31
|
+
"BigDecimal.new(#{value.to_s.inspect})"
|
32
32
|
when Hash
|
33
33
|
inspect_hash(value, nested)
|
34
34
|
when Array
|
@@ -45,7 +45,7 @@ module ToFactory
|
|
45
45
|
def validate_parseable!(value)
|
46
46
|
return value if parse(value)
|
47
47
|
|
48
|
-
"ToFactory: RubyParser exception parsing this attribute
|
48
|
+
"ToFactory: RubyParser exception parsing this attribute"
|
49
49
|
end
|
50
50
|
|
51
51
|
def parse(value)
|
@@ -65,17 +65,17 @@ module ToFactory
|
|
65
65
|
def inspect_hash(value, nested)
|
66
66
|
formatted = value.keys.inject([]) do |a, key|
|
67
67
|
a << key_value_pair(key, value)
|
68
|
-
end.join(
|
68
|
+
end.join(", ")
|
69
69
|
|
70
70
|
if nested
|
71
71
|
"{#{formatted}}"
|
72
72
|
else
|
73
|
-
"
|
73
|
+
" { {#{formatted}} }"
|
74
74
|
end
|
75
75
|
end
|
76
76
|
|
77
77
|
def inspect_array(value, nested)
|
78
|
-
values = value.map{|v| format(v, nested)}.join(", ")
|
78
|
+
values = value.map { |v| format(v, nested) }.join(", ")
|
79
79
|
"[#{values}]"
|
80
80
|
end
|
81
81
|
|
@@ -87,5 +87,3 @@ module ToFactory
|
|
87
87
|
end
|
88
88
|
end
|
89
89
|
end
|
90
|
-
|
91
|
-
|
@@ -28,7 +28,7 @@ module ToFactory
|
|
28
28
|
def attributes
|
29
29
|
to_skip = [:id, :created_at, :created_on, :updated_at, :updated_on]
|
30
30
|
|
31
|
-
@representation.attributes.delete_if{|key, _| key.nil? || to_skip.include?(key.to_sym)}
|
31
|
+
@representation.attributes.delete_if { |key, _| key.nil? || to_skip.include?(key.to_sym) }
|
32
32
|
end
|
33
33
|
|
34
34
|
private
|
@@ -37,14 +37,14 @@ module ToFactory
|
|
37
37
|
@representation.parent_name
|
38
38
|
end
|
39
39
|
|
40
|
-
def generic_header(factory_start, block_arg, ending, &
|
40
|
+
def generic_header(factory_start, block_arg, ending, &_block)
|
41
41
|
out = "#{factory_start}(:#{name}#{parent_clause}) do#{block_arg}\n"
|
42
42
|
out << yield.to_s
|
43
43
|
out << "#{ending}\n"
|
44
44
|
end
|
45
45
|
|
46
46
|
def parent_clause
|
47
|
-
has_parent? ?
|
47
|
+
has_parent? ? ", :parent => :#{add_quotes parent_name}" : ""
|
48
48
|
end
|
49
49
|
|
50
50
|
def has_parent?
|
@@ -14,14 +14,12 @@ module ToFactory
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
-
def infer(factory_name, count=0)
|
18
|
-
count
|
19
|
-
result
|
17
|
+
def infer(factory_name, count = 0)
|
18
|
+
count += 1
|
19
|
+
result = @mapping[factory_name]
|
20
20
|
return [result, count] if result.is_a? Class
|
21
21
|
|
22
|
-
if result.nil?
|
23
|
-
raise CannotInferClass.new(factory_name)
|
24
|
-
end
|
22
|
+
fail CannotInferClass.new(factory_name) if result.nil?
|
25
23
|
|
26
24
|
infer(result, count)
|
27
25
|
end
|
@@ -6,10 +6,10 @@ module ToFactory
|
|
6
6
|
|
7
7
|
def get_instance
|
8
8
|
args = case @options
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
when ActiveRecord::Base
|
10
|
+
from_record(@options)
|
11
|
+
when Array
|
12
|
+
from_array(*@options)
|
13
13
|
end
|
14
14
|
|
15
15
|
Representation.new(*args)
|
@@ -1,33 +1,106 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "ruby2ruby"
|
2
|
+
require "ruby_parser"
|
3
3
|
require "to_factory/parsing/ruby_parsing_helpers"
|
4
|
-
require 'to_factory/parsing/syntax'
|
5
4
|
|
6
5
|
module ToFactory
|
7
6
|
module Parsing
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
ParseException = Class.new ::StandardError
|
8
|
+
|
9
|
+
class CouldNotInferClassException < ParseException
|
10
|
+
attr_reader :sexp
|
11
11
|
|
12
|
-
def
|
13
|
-
|
12
|
+
def initialize(sexp)
|
13
|
+
@sexp = sexp
|
14
14
|
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class File
|
18
|
+
EmptyFileException = Class.new ArgumentError
|
19
|
+
|
20
|
+
include Parsing::RubyParsingHelpers
|
21
|
+
attr_accessor :contents
|
15
22
|
|
16
|
-
|
17
|
-
|
18
|
-
|
23
|
+
class << self
|
24
|
+
def parse(filename)
|
25
|
+
from_file(filename).parse
|
26
|
+
end
|
19
27
|
|
20
|
-
|
28
|
+
def from_file(filename)
|
29
|
+
begin
|
30
|
+
contents = ::File.read filename
|
31
|
+
rescue
|
32
|
+
nil
|
33
|
+
end
|
34
|
+
fail EmptyFileException.new "Invalid file #{filename}" if contents.to_s.strip.length == 0
|
35
|
+
|
36
|
+
new(contents)
|
37
|
+
end
|
21
38
|
end
|
22
39
|
|
23
40
|
def initialize(contents)
|
24
41
|
@contents = contents
|
25
42
|
end
|
26
43
|
|
27
|
-
def
|
28
|
-
|
44
|
+
def multiple_factories?
|
45
|
+
factories_sexp[0] == :block
|
46
|
+
end
|
47
|
+
|
48
|
+
def parse
|
49
|
+
factories.map do |x|
|
50
|
+
representation_from(x)
|
51
|
+
end
|
52
|
+
|
53
|
+
rescue Racc::ParseError, StringScanner::Error => e
|
54
|
+
raise ParseException.new("Original exception: #{e.message}\n #{e.backtrace.join("\n")}\nToFactory Error parsing \n#{@contents}\n o")
|
55
|
+
end
|
56
|
+
|
57
|
+
def header?
|
58
|
+
sexp[1][1][1] == :FactoryBot
|
59
|
+
rescue
|
60
|
+
false
|
29
61
|
end
|
30
62
|
|
63
|
+
private
|
64
|
+
|
65
|
+
def representation_from(x)
|
66
|
+
Representation.new(name_from(x), parent_from(x), to_ruby(x))
|
67
|
+
rescue CouldNotInferClassException => e
|
68
|
+
ruby = to_ruby(e.sexp)
|
69
|
+
Kernel.warn "ToFactory could not parse\n#{ruby}"
|
70
|
+
NullRepresentation.new(e.sexp)
|
71
|
+
end
|
72
|
+
|
73
|
+
def factories
|
74
|
+
if multiple_factories?
|
75
|
+
factories_sexp[1..-1]
|
76
|
+
else
|
77
|
+
[factories_sexp]
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def factories_sexp
|
82
|
+
header? ? sexp[3] : sexp
|
83
|
+
end
|
84
|
+
|
85
|
+
def name_from(sexp)
|
86
|
+
sexp[1][3][1]
|
87
|
+
rescue NoMethodError
|
88
|
+
raise CouldNotInferClassException.new(sexp)
|
89
|
+
end
|
90
|
+
|
91
|
+
def parent_from(x)
|
92
|
+
# e.g.
|
93
|
+
# s(:call, nil, :factory, s(:lit, :admin), s(:hash, s(:lit, :parent), s(:lit, :"to_factory/user")))
|
94
|
+
x[1][4][2][1]
|
95
|
+
rescue NoMethodError
|
96
|
+
# e.g.
|
97
|
+
# s(:call, nil, :factory, s(:lit, :"to_factory/user"))
|
98
|
+
x[1][3][1]
|
99
|
+
end
|
100
|
+
|
101
|
+
def sexp
|
102
|
+
@sexp ||= ruby_parser.process(@contents)
|
103
|
+
end
|
31
104
|
end
|
32
105
|
end
|
33
106
|
end
|
@@ -19,18 +19,19 @@ module ToFactory
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
|
23
22
|
class Representation
|
24
|
-
delegate :attributes, :
|
23
|
+
delegate :attributes, to: :record
|
25
24
|
attr_accessor :klass, :name, :parent_name, :definition, :hierarchy_order, :record
|
26
25
|
|
27
26
|
def self.from(options)
|
28
27
|
OptionsParser.new(options).get_instance
|
29
28
|
end
|
30
29
|
|
31
|
-
def initialize(name, parent_name, definition=nil, record=nil)
|
32
|
-
@name
|
33
|
-
|
30
|
+
def initialize(name, parent_name, definition = nil, record = nil)
|
31
|
+
@name = name.to_s
|
32
|
+
@parent_name = parent_name.to_s
|
33
|
+
@definition = definition
|
34
|
+
@record = record
|
34
35
|
end
|
35
36
|
|
36
37
|
def inspect
|
@@ -38,7 +39,9 @@ module ToFactory
|
|
38
39
|
end
|
39
40
|
|
40
41
|
def klass_name_inspect
|
41
|
-
@klass.name.inspect
|
42
|
+
@klass.name.inspect
|
43
|
+
rescue
|
44
|
+
"nil"
|
42
45
|
end
|
43
46
|
|
44
47
|
def definition
|
data/lib/to_factory/version.rb
CHANGED
data/lib/to_factory.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "fileutils"
|
2
2
|
|
3
3
|
require "to_factory/version"
|
4
4
|
require "to_factory/config"
|
@@ -13,22 +13,21 @@ require "to_factory/parsing/file"
|
|
13
13
|
require "to_factory/representation"
|
14
14
|
require "to_factory/klass_inference"
|
15
15
|
require "to_factory/options_parser"
|
16
|
-
require "
|
16
|
+
#require "factory_bot"
|
17
17
|
|
18
18
|
module ToFactory
|
19
|
-
class MissingActiveRecordInstanceException < Exception;end
|
20
|
-
class NotFoundError < Exception;end
|
19
|
+
class MissingActiveRecordInstanceException < Exception; end
|
20
|
+
class NotFoundError < Exception; end
|
21
21
|
|
22
22
|
class << self
|
23
|
-
|
24
23
|
def definition_for(item)
|
25
24
|
if item.is_a? ActiveRecord::Base
|
26
25
|
Representation.from(item).definition
|
27
26
|
else
|
28
|
-
if found = representations.find{|r| r.name.to_s == item.to_s }
|
27
|
+
if found = representations.find { |r| r.name.to_s == item.to_s }
|
29
28
|
found.definition
|
30
29
|
else
|
31
|
-
|
30
|
+
fail NotFoundError.new "No definition found for #{item}"
|
32
31
|
end
|
33
32
|
end
|
34
33
|
end
|
@@ -45,7 +44,7 @@ end
|
|
45
44
|
|
46
45
|
public
|
47
46
|
|
48
|
-
def ToFactory(args=nil)
|
47
|
+
def ToFactory(args = nil)
|
49
48
|
exclusions = if args.is_a?(Hash)
|
50
49
|
exclusions = Array(args.delete(:exclude) || [])
|
51
50
|
args = nil if args.keys.length == 0
|
@@ -59,12 +58,3 @@ def ToFactory(args=nil)
|
|
59
58
|
|
60
59
|
sync.perform(exclusions)
|
61
60
|
end
|
62
|
-
|
63
|
-
if defined?(Rails)
|
64
|
-
unless Rails.respond_to?(:configuration)
|
65
|
-
#FactoryGirl 1.3.x expects this method, but it isn't defined in Rails 2.0.2
|
66
|
-
def Rails.configuration
|
67
|
-
OpenStruct.new
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|