factory_bot_factory 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +60 -0
- data/LICENSE.txt +21 -0
- data/README.md +149 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/factory_bot_factory.gemspec +33 -0
- data/lib/factory_bot_factory/base.rb +23 -0
- data/lib/factory_bot_factory/config.rb +18 -0
- data/lib/factory_bot_factory/converters/hash_converter.rb +16 -0
- data/lib/factory_bot_factory/converters/nil_converter.rb +9 -0
- data/lib/factory_bot_factory/converters/numeric_converter.rb +9 -0
- data/lib/factory_bot_factory/converters/string_converter.rb +9 -0
- data/lib/factory_bot_factory/factories/base_factory.rb +56 -0
- data/lib/factory_bot_factory/factories/hash_factory.rb +17 -0
- data/lib/factory_bot_factory/factories/model_factory.rb +14 -0
- data/lib/factory_bot_factory/factories/open_struct_factory.rb +18 -0
- data/lib/factory_bot_factory/line_writer.rb +67 -0
- data/lib/factory_bot_factory/version.rb +3 -0
- data/lib/factory_bot_factory.rb +31 -0
- metadata +153 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 1a5446b4470b7e8fb666695ea54aecc662725ccbab46afc1425b19feb4a2f996
|
|
4
|
+
data.tar.gz: 3dbffe0f96d8135c177e9f78fd25506c8bdcb0c89a37fc423d2b045f2df819ed
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 768c46d1e6981210d158ee455bc590bd3e4743f05fc58f9f9d99955ae6a0f41d80bd897f1b5eba4333c4e1b1db0a18973ea7a9f0e5a75913699131d134e0a0d4
|
|
7
|
+
data.tar.gz: 5adf7eba8856308b52c77d3734189bd0d2bc8c10327bab60b63570514055c90577ca981088d281e80a72c839696ced6866339b5a752ee88a6aa92d0f1a36fe5a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
|
10
|
+
orientation.
|
|
11
|
+
|
|
12
|
+
## Our Standards
|
|
13
|
+
|
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
|
15
|
+
include:
|
|
16
|
+
|
|
17
|
+
* Using welcoming and inclusive language
|
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
|
19
|
+
* Gracefully accepting constructive criticism
|
|
20
|
+
* Focusing on what is best for the community
|
|
21
|
+
* Showing empathy towards other community members
|
|
22
|
+
|
|
23
|
+
Examples of unacceptable behavior by participants include:
|
|
24
|
+
|
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
26
|
+
advances
|
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
28
|
+
* Public or private harassment
|
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
|
30
|
+
address, without explicit permission
|
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
|
+
professional setting
|
|
33
|
+
|
|
34
|
+
## Our Responsibilities
|
|
35
|
+
|
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
|
38
|
+
response to any instances of unacceptable behavior.
|
|
39
|
+
|
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
|
44
|
+
threatening, offensive, or harmful.
|
|
45
|
+
|
|
46
|
+
## Scope
|
|
47
|
+
|
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
|
49
|
+
when an individual is representing the project or its community. Examples of
|
|
50
|
+
representing a project or community include using an official project e-mail
|
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
|
53
|
+
further defined and clarified by project maintainers.
|
|
54
|
+
|
|
55
|
+
## Enforcement
|
|
56
|
+
|
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
+
reported by contacting the project team at jenny610138@gmail.com. All
|
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
|
63
|
+
|
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
|
66
|
+
members of the project's leadership.
|
|
67
|
+
|
|
68
|
+
## Attribution
|
|
69
|
+
|
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: http://contributor-covenant.org
|
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
factory_bot_factory (0.0.3)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
activesupport (6.1.4.1)
|
|
10
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
11
|
+
i18n (>= 1.6, < 2)
|
|
12
|
+
minitest (>= 5.1)
|
|
13
|
+
tzinfo (~> 2.0)
|
|
14
|
+
zeitwerk (~> 2.3)
|
|
15
|
+
coderay (1.1.3)
|
|
16
|
+
concurrent-ruby (1.1.9)
|
|
17
|
+
diff-lcs (1.4.4)
|
|
18
|
+
factory_bot (6.2.0)
|
|
19
|
+
activesupport (>= 5.0.0)
|
|
20
|
+
faker (2.19.0)
|
|
21
|
+
i18n (>= 1.6, < 2)
|
|
22
|
+
i18n (1.8.10)
|
|
23
|
+
concurrent-ruby (~> 1.0)
|
|
24
|
+
method_source (1.0.0)
|
|
25
|
+
minitest (5.14.4)
|
|
26
|
+
pry (0.13.1)
|
|
27
|
+
coderay (~> 1.1)
|
|
28
|
+
method_source (~> 1.0)
|
|
29
|
+
rake (10.5.0)
|
|
30
|
+
rspec (3.10.0)
|
|
31
|
+
rspec-core (~> 3.10.0)
|
|
32
|
+
rspec-expectations (~> 3.10.0)
|
|
33
|
+
rspec-mocks (~> 3.10.0)
|
|
34
|
+
rspec-core (3.10.1)
|
|
35
|
+
rspec-support (~> 3.10.0)
|
|
36
|
+
rspec-expectations (3.10.1)
|
|
37
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
38
|
+
rspec-support (~> 3.10.0)
|
|
39
|
+
rspec-mocks (3.10.2)
|
|
40
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
41
|
+
rspec-support (~> 3.10.0)
|
|
42
|
+
rspec-support (3.10.2)
|
|
43
|
+
tzinfo (2.0.4)
|
|
44
|
+
concurrent-ruby (~> 1.0)
|
|
45
|
+
zeitwerk (2.4.2)
|
|
46
|
+
|
|
47
|
+
PLATFORMS
|
|
48
|
+
ruby
|
|
49
|
+
|
|
50
|
+
DEPENDENCIES
|
|
51
|
+
bundler (~> 1.17)
|
|
52
|
+
factory_bot
|
|
53
|
+
factory_bot_factory!
|
|
54
|
+
faker
|
|
55
|
+
pry (~> 0.13.1)
|
|
56
|
+
rake (~> 10.0)
|
|
57
|
+
rspec (~> 3.0)
|
|
58
|
+
|
|
59
|
+
BUNDLED WITH
|
|
60
|
+
1.17.3
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 YenJung Chen
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# Factory Bot Factory
|
|
2
|
+
|
|
3
|
+
A Gem that helps you generate FactoryBot's Factory file from exsiting Hash, OpenStruct or ActiveModels.
|
|
4
|
+
|
|
5
|
+
The main purpose is to speed up the process of building big factory.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'factory_bot_factory'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install factory_bot_factory
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
## Quick Demo
|
|
23
|
+
|
|
24
|
+
- Build a Hash Factory
|
|
25
|
+
|
|
26
|
+
```ruby
|
|
27
|
+
require 'factory_bot_factory'
|
|
28
|
+
|
|
29
|
+
data = { id: 1, tags: ["tag1", "tag2"], address: { country: "US" } }
|
|
30
|
+
puts FactoryBotFactory.build("order_hash", Hash, data)
|
|
31
|
+
|
|
32
|
+
# output
|
|
33
|
+
FactoryBot.define do
|
|
34
|
+
factory :order_hash, class: Hash do
|
|
35
|
+
id { 1 }
|
|
36
|
+
tags do
|
|
37
|
+
[
|
|
38
|
+
"tag1",
|
|
39
|
+
"tag2"
|
|
40
|
+
]
|
|
41
|
+
end
|
|
42
|
+
address do
|
|
43
|
+
{
|
|
44
|
+
"country": "US"
|
|
45
|
+
}
|
|
46
|
+
end
|
|
47
|
+
initialize_with { attributes }
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
- Nested Hash Factory
|
|
53
|
+
|
|
54
|
+
```ruby
|
|
55
|
+
data = { id: 1, tags: ["tag1", "tag2"], address: { country: "US" } }
|
|
56
|
+
puts FactoryBotFactory.build("order_hash", Hash, data, nested_level: 2)
|
|
57
|
+
|
|
58
|
+
# output
|
|
59
|
+
FactoryBot.define do
|
|
60
|
+
factory :order_hash, class: Hash do
|
|
61
|
+
id { 1 }
|
|
62
|
+
tags do
|
|
63
|
+
[
|
|
64
|
+
"tag1",
|
|
65
|
+
"tag2"
|
|
66
|
+
]
|
|
67
|
+
end
|
|
68
|
+
address { build(:order_hash_address) }
|
|
69
|
+
initialize_with { attributes }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
factory :order_hash_address, class: Hash do
|
|
73
|
+
country { "US" }
|
|
74
|
+
initialize_with { attributes }
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
- Export the file somewhere
|
|
80
|
+
|
|
81
|
+
```ruby
|
|
82
|
+
FactoryBotFactory.build("order_hash", Hash, data, file_path: "spec/factories/order_hash.rb")
|
|
83
|
+
|
|
84
|
+
require 'factory_bot'
|
|
85
|
+
FactoryBot.reload
|
|
86
|
+
FactoryBot.build(:order_hash, id: 2)
|
|
87
|
+
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Supported Factories
|
|
91
|
+
|
|
92
|
+
- Hash
|
|
93
|
+
```ruby
|
|
94
|
+
FactoryBotFactory.build("order_hash", Hash, data)
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
- OpenStruct
|
|
98
|
+
```
|
|
99
|
+
FactoryBotFactory.build("order_open_struct", OpenStruct, data)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
- Your ActiveModel or ActiveRecord Model
|
|
103
|
+
```
|
|
104
|
+
FactoryBotFactory.build("user", User, user_instance)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Configure your own converter
|
|
108
|
+
|
|
109
|
+
- Configure
|
|
110
|
+
|
|
111
|
+
```ruby
|
|
112
|
+
FactoryBotFactory.configure do |config|
|
|
113
|
+
config.string_converter = Proc.new do |k, v|
|
|
114
|
+
if v.to_s.match?(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/)
|
|
115
|
+
'Random.uuid()'
|
|
116
|
+
elsif k.to_s.include?('name')
|
|
117
|
+
'Faker::Name.name'
|
|
118
|
+
else
|
|
119
|
+
"'#{v}'"
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
FactoryBotFactory.build("order_hash", Hash, { name: 'My Name', id: "de9515ee-006e-4a28-8af3-e88a5c771b93" })
|
|
125
|
+
|
|
126
|
+
# output
|
|
127
|
+
FactoryBot.define do
|
|
128
|
+
factory :order_hash, class: Hash do
|
|
129
|
+
name { Faker::Name.name }
|
|
130
|
+
id { Random.uuid() }
|
|
131
|
+
initialize_with { attributes }
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
See move converters 'lib/factory_bot_factory/config.rb'
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
## Contributing
|
|
140
|
+
|
|
141
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/cdragon1116/factory_bot_factory. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
|
142
|
+
|
|
143
|
+
## License
|
|
144
|
+
|
|
145
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
146
|
+
|
|
147
|
+
## Code of Conduct
|
|
148
|
+
|
|
149
|
+
Everyone interacting in the Factorybuilder project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/cdragon1116/factory_bot_factory/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "factorybuilder"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
3
|
+
require "factory_bot_factory/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "factory_bot_factory"
|
|
7
|
+
spec.version = FactoryBotFactory::VERSION
|
|
8
|
+
spec.authors = ["cdragon"]
|
|
9
|
+
spec.email = "cdragon1116@gmail.com"
|
|
10
|
+
spec.homepage = "https://github.com/cdragon1116/factory_bot_factory"
|
|
11
|
+
|
|
12
|
+
spec.summary = "A Gem that generate FactoryBot's Factory file from exsiting Hash, OpenStruct or Models."
|
|
13
|
+
spec.description = "A Gem that generate FactoryBot's Factory file from exsiting Hash, OpenStruct or Models."
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
spec.bindir = "exe"
|
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
25
|
+
spec.require_paths = ["lib"]
|
|
26
|
+
|
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
|
28
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
29
|
+
spec.add_development_dependency "pry", '~> 0.13.1'
|
|
30
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
31
|
+
spec.add_development_dependency "factory_bot"
|
|
32
|
+
spec.add_development_dependency "faker"
|
|
33
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module FactoryBotFactory
|
|
2
|
+
class Base
|
|
3
|
+
class << self
|
|
4
|
+
def build(factory_name, klass, data, options = {})
|
|
5
|
+
raise NestedToDeepError, 'Only support 5 nested levels' if options[:nested_level].to_i > 5
|
|
6
|
+
|
|
7
|
+
factory(klass, data).new(options.merge(factory_name: factory_name)).generate(data)
|
|
8
|
+
rescue => e
|
|
9
|
+
puts "Please check your input data and make sure you pass supported type of factory."
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
private
|
|
13
|
+
|
|
14
|
+
def factory(klass, data)
|
|
15
|
+
if data.respond_to?(:attributes) && klass == data.class
|
|
16
|
+
Object.const_get("FactoryBotFactory::ModelFactory")
|
|
17
|
+
else
|
|
18
|
+
Object.const_get("FactoryBotFactory::#{klass}Factory")
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module FactoryBotFactory
|
|
2
|
+
class Config
|
|
3
|
+
DEFAULT_CONVERTERS = {
|
|
4
|
+
hash_converter: Converters::HashConverter,
|
|
5
|
+
string_converter: Converters::StringConverter,
|
|
6
|
+
numertic_converter: Converters::NumericConverter,
|
|
7
|
+
open_struct_converter: Converters::HashConverter,
|
|
8
|
+
nil_converter: Converters::NilConverter
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
attr_accessor :hash_converter, :string_converter, :numertic_converter, :open_struct_converter, :nil_converter
|
|
12
|
+
|
|
13
|
+
def initialize(options = {})
|
|
14
|
+
options = DEFAULT_CONVERTERS.merge(options)
|
|
15
|
+
options.each { |k, v| instance_variable_set(:"@#{k}", v) }
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
module FactoryBotFactory
|
|
2
|
+
module Converters
|
|
3
|
+
class HashConverter
|
|
4
|
+
EMPTY_PAIR_REGEX = /(?<content>"(?:[^\\"]|\\.)+")|(?<open>\{)\s+(?<close>\})|(?<open>\[)\s+(?<close>\])/m
|
|
5
|
+
|
|
6
|
+
def self.call(key, value, options = {})
|
|
7
|
+
value = value.to_h if value.is_a?(OpenStruct)
|
|
8
|
+
|
|
9
|
+
value = JSON
|
|
10
|
+
.pretty_generate(value)
|
|
11
|
+
.gsub(EMPTY_PAIR_REGEX, '\k<open>\k<content>\k<close>')
|
|
12
|
+
.split("\n")
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
module FactoryBotFactory
|
|
2
|
+
class BaseFactory
|
|
3
|
+
def initialize(options = {})
|
|
4
|
+
@factory_name = options[:factory_name]
|
|
5
|
+
@file_path = options[:file_path]
|
|
6
|
+
@nested_level = [(options[:nested_level] || 1), 5].min
|
|
7
|
+
@line_writer = LineWriter.new(options)
|
|
8
|
+
@factory_queue = []
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def generate(data)
|
|
12
|
+
output = LineWriter.wrap_definition do
|
|
13
|
+
@factory_queue << [@factory_name, data, @nested_level]
|
|
14
|
+
inner_output = []
|
|
15
|
+
|
|
16
|
+
loop do
|
|
17
|
+
factory_option = @factory_queue.shift
|
|
18
|
+
inner_output += build_factory(*factory_option)
|
|
19
|
+
break if @factory_queue.empty?
|
|
20
|
+
inner_output << LineWriter::WHITE_SPACE
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
inner_output
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
output = output.join(LineWriter::NEW_LINE)
|
|
27
|
+
File.open(@file_path, 'w') {|f| f.write(output) } if @file_path
|
|
28
|
+
output
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def build_factory(name, value, level)
|
|
34
|
+
raise "This method should be implemented in a subclass"
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def build_nested_attribute(name, key, value, current_level, max_level)
|
|
38
|
+
return @line_writer.build(key, value) if current_level == max_level
|
|
39
|
+
|
|
40
|
+
if is_key_value_pair?(value)
|
|
41
|
+
push_to_factory_queue(name, key, value, max_level)
|
|
42
|
+
@line_writer.build_nested_line(name, key)
|
|
43
|
+
else
|
|
44
|
+
@line_writer.build(key, value)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def push_to_factory_queue(name, key, value, max_level)
|
|
49
|
+
@factory_queue.push(["#{name}_#{key}", value, max_level - 1])
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def is_key_value_pair?(value)
|
|
53
|
+
value.is_a?(Hash) || value.is_a?(OpenStruct)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module FactoryBotFactory
|
|
2
|
+
class HashFactory < BaseFactory
|
|
3
|
+
def build_factory(name, value, level)
|
|
4
|
+
output = LineWriter.wrap_factory(name, 'Hash') do
|
|
5
|
+
inner_output = []
|
|
6
|
+
value = value.attributes if value.respond_to?(:attributes)
|
|
7
|
+
value = value.to_h if value.respond_to?(:to_h)
|
|
8
|
+
value.each do |key, value|
|
|
9
|
+
inner_output += build_nested_attribute(name, key, value, 1, level)
|
|
10
|
+
end
|
|
11
|
+
inner_output << "initialize_with { attributes }"
|
|
12
|
+
inner_output
|
|
13
|
+
end
|
|
14
|
+
output
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module FactoryBotFactory
|
|
2
|
+
class ModelFactory < BaseFactory
|
|
3
|
+
def build_factory(name, value, level)
|
|
4
|
+
output = LineWriter.wrap_factory(name, value.class.name) do
|
|
5
|
+
inner_output = []
|
|
6
|
+
value.attributes.each do |key, value|
|
|
7
|
+
inner_output += build_nested_attribute(value.class.name, key, value, 1, 1)
|
|
8
|
+
end
|
|
9
|
+
inner_output
|
|
10
|
+
end
|
|
11
|
+
output
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module FactoryBotFactory
|
|
2
|
+
class OpenStructFactory < BaseFactory
|
|
3
|
+
def build_factory(name, value, level)
|
|
4
|
+
output = LineWriter.wrap_factory(name, 'OpenStruct') do
|
|
5
|
+
inner_output = []
|
|
6
|
+
value = value.attributes if value.respond_to?(:attributes)
|
|
7
|
+
value = value.to_h if value.respond_to?(:to_h)
|
|
8
|
+
value.each do |key, value|
|
|
9
|
+
value = build_nested_attribute(name, key, value, 1, level)
|
|
10
|
+
inner_output += value
|
|
11
|
+
inner_output
|
|
12
|
+
end
|
|
13
|
+
inner_output << "to_create {}"
|
|
14
|
+
end
|
|
15
|
+
output
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
module FactoryBotFactory
|
|
2
|
+
class LineWriter
|
|
3
|
+
NEW_LINE = "\n".freeze
|
|
4
|
+
WHITE_SPACE = "\s".freeze
|
|
5
|
+
INDENT_SPACE = 2
|
|
6
|
+
|
|
7
|
+
attr_reader :hash_converter, :string_converter, :numertic_converter, :open_struct_converter, :nil_converter
|
|
8
|
+
|
|
9
|
+
def initialize(options = {})
|
|
10
|
+
@options = options
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def build(key, value)
|
|
14
|
+
options = [key, value, @options]
|
|
15
|
+
|
|
16
|
+
values = if value.nil? || value == "null"
|
|
17
|
+
FactoryBotFactory.config.nil_converter.call(*options)
|
|
18
|
+
elsif value.is_a?(String) || value.is_a?(Symbol)
|
|
19
|
+
FactoryBotFactory.config.string_converter.call(*options)
|
|
20
|
+
elsif value.is_a?(Numeric)
|
|
21
|
+
FactoryBotFactory.config.numertic_converter.call(*options)
|
|
22
|
+
elsif value.is_a?(Hash) || value.is_a?(Array)
|
|
23
|
+
FactoryBotFactory.config.hash_converter.call(*options)
|
|
24
|
+
else value.is_a?(OpenStruct)
|
|
25
|
+
FactoryBotFactory.config.open_struct_converter.call(*options)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
wrap_block(key, values)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def build_nested_line(prefix, key)
|
|
32
|
+
["#{key} { build(:#{prefix}_#{key}) }"]
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def wrap_block(key, values)
|
|
38
|
+
values = [values] unless values.is_a?(Array)
|
|
39
|
+
|
|
40
|
+
if values.size > 1
|
|
41
|
+
["#{key} do"] + values.map { |v| LineWriter.indent(1, v) } + ["end"]
|
|
42
|
+
else values.is_a?(Array)
|
|
43
|
+
["#{key} { #{values[0]} }"]
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
class << self
|
|
48
|
+
def indent(level, value)
|
|
49
|
+
"#{WHITE_SPACE * INDENT_SPACE * level}#{value}"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def wrap_definition(&_block)
|
|
53
|
+
output = ["FactoryBot.define do"]
|
|
54
|
+
output += yield.map { |s| indent(1, s) }
|
|
55
|
+
output << "end"
|
|
56
|
+
output
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def wrap_factory(name, target, &_block)
|
|
60
|
+
output = ["factory :#{name}, class: #{target} do"]
|
|
61
|
+
output += yield.map { |s| indent(1, s) }
|
|
62
|
+
output << "end"
|
|
63
|
+
output
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
require "factory_bot_factory/version"
|
|
3
|
+
|
|
4
|
+
# Load all Factories and Converters
|
|
5
|
+
Dir.glob("#{File.dirname(__FILE__)}/factory_bot_factory/factories/*").each { |file| require(file) }
|
|
6
|
+
Dir.glob("#{File.dirname(__FILE__)}/factory_bot_factory/converters/*").each { |file| require(file) }
|
|
7
|
+
|
|
8
|
+
require 'factory_bot_factory/line_writer.rb'
|
|
9
|
+
require 'factory_bot_factory/base.rb'
|
|
10
|
+
require "factory_bot_factory/config"
|
|
11
|
+
|
|
12
|
+
module FactoryBotFactory
|
|
13
|
+
class Error < StandardError; end
|
|
14
|
+
class NestedToDeepError < Error; end
|
|
15
|
+
|
|
16
|
+
class << self
|
|
17
|
+
def build(factory_name, klass, data, options = {})
|
|
18
|
+
configure
|
|
19
|
+
Base.build(factory_name, klass, data, options)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def configure
|
|
23
|
+
yield config if block_given?
|
|
24
|
+
config
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def config
|
|
28
|
+
@_config ||= Config.new
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: factory_bot_factory
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.5
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- cdragon
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2021-09-19 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.17'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.17'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: pry
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: 0.13.1
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: 0.13.1
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rspec
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '3.0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '3.0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: factory_bot
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '0'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - ">="
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '0'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: faker
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - ">="
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '0'
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - ">="
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: '0'
|
|
97
|
+
description: A Gem that generate FactoryBot's Factory file from exsiting Hash, OpenStruct
|
|
98
|
+
or Models.
|
|
99
|
+
email: cdragon1116@gmail.com
|
|
100
|
+
executables: []
|
|
101
|
+
extensions: []
|
|
102
|
+
extra_rdoc_files: []
|
|
103
|
+
files:
|
|
104
|
+
- ".gitignore"
|
|
105
|
+
- ".rspec"
|
|
106
|
+
- ".travis.yml"
|
|
107
|
+
- CODE_OF_CONDUCT.md
|
|
108
|
+
- Gemfile
|
|
109
|
+
- Gemfile.lock
|
|
110
|
+
- LICENSE.txt
|
|
111
|
+
- README.md
|
|
112
|
+
- Rakefile
|
|
113
|
+
- bin/console
|
|
114
|
+
- bin/setup
|
|
115
|
+
- factory_bot_factory.gemspec
|
|
116
|
+
- lib/factory_bot_factory.rb
|
|
117
|
+
- lib/factory_bot_factory/base.rb
|
|
118
|
+
- lib/factory_bot_factory/config.rb
|
|
119
|
+
- lib/factory_bot_factory/converters/hash_converter.rb
|
|
120
|
+
- lib/factory_bot_factory/converters/nil_converter.rb
|
|
121
|
+
- lib/factory_bot_factory/converters/numeric_converter.rb
|
|
122
|
+
- lib/factory_bot_factory/converters/string_converter.rb
|
|
123
|
+
- lib/factory_bot_factory/factories/base_factory.rb
|
|
124
|
+
- lib/factory_bot_factory/factories/hash_factory.rb
|
|
125
|
+
- lib/factory_bot_factory/factories/model_factory.rb
|
|
126
|
+
- lib/factory_bot_factory/factories/open_struct_factory.rb
|
|
127
|
+
- lib/factory_bot_factory/line_writer.rb
|
|
128
|
+
- lib/factory_bot_factory/version.rb
|
|
129
|
+
homepage: https://github.com/cdragon1116/factory_bot_factory
|
|
130
|
+
licenses:
|
|
131
|
+
- MIT
|
|
132
|
+
metadata: {}
|
|
133
|
+
post_install_message:
|
|
134
|
+
rdoc_options: []
|
|
135
|
+
require_paths:
|
|
136
|
+
- lib
|
|
137
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
138
|
+
requirements:
|
|
139
|
+
- - ">="
|
|
140
|
+
- !ruby/object:Gem::Version
|
|
141
|
+
version: '0'
|
|
142
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
143
|
+
requirements:
|
|
144
|
+
- - ">="
|
|
145
|
+
- !ruby/object:Gem::Version
|
|
146
|
+
version: '0'
|
|
147
|
+
requirements: []
|
|
148
|
+
rubygems_version: 3.0.9
|
|
149
|
+
signing_key:
|
|
150
|
+
specification_version: 4
|
|
151
|
+
summary: A Gem that generate FactoryBot's Factory file from exsiting Hash, OpenStruct
|
|
152
|
+
or Models.
|
|
153
|
+
test_files: []
|