factory_bot_factory 0.0.5 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1a5446b4470b7e8fb666695ea54aecc662725ccbab46afc1425b19feb4a2f996
4
- data.tar.gz: 3dbffe0f96d8135c177e9f78fd25506c8bdcb0c89a37fc423d2b045f2df819ed
3
+ metadata.gz: 683013489cb5caa6aedb9dd6f5aeea5e961e79fb78647f11b984629b8e5edea5
4
+ data.tar.gz: c31acd1032b0b136f6f29263a7910b2a943f85df96411aaa7c66a4f5d36b64e0
5
5
  SHA512:
6
- metadata.gz: 768c46d1e6981210d158ee455bc590bd3e4743f05fc58f9f9d99955ae6a0f41d80bd897f1b5eba4333c4e1b1db0a18973ea7a9f0e5a75913699131d134e0a0d4
7
- data.tar.gz: 5adf7eba8856308b52c77d3734189bd0d2bc8c10327bab60b63570514055c90577ca981088d281e80a72c839696ced6866339b5a752ee88a6aa92d0f1a36fe5a
6
+ metadata.gz: c20d1624c1dcf7b845718361526938901236061891691c9978161763f4aad965867caaee1ee29ff52434df6918a9ac6c9f38eb0e17353ce2d927d85c51344bc8
7
+ data.tar.gz: a6eff9d1210bd3e288a46309208b69faeae0c7c2c55b313afaff43d1423368dc8f31cef02df02657391f7d3b7bcfb5083a849d015de0fae7fea6cf91d4e46f47
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- factory_bot_factory (0.0.3)
4
+ factory_bot_factory (1.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -26,7 +26,7 @@ GEM
26
26
  pry (0.13.1)
27
27
  coderay (~> 1.1)
28
28
  method_source (~> 1.0)
29
- rake (10.5.0)
29
+ rake (13.0.6)
30
30
  rspec (3.10.0)
31
31
  rspec-core (~> 3.10.0)
32
32
  rspec-expectations (~> 3.10.0)
@@ -48,13 +48,13 @@ PLATFORMS
48
48
  ruby
49
49
 
50
50
  DEPENDENCIES
51
- bundler (~> 1.17)
51
+ bundler (>= 2.2.10)
52
52
  factory_bot
53
53
  factory_bot_factory!
54
54
  faker
55
55
  pry (~> 0.13.1)
56
- rake (~> 10.0)
56
+ rake (>= 11.2.2)
57
57
  rspec (~> 3.0)
58
58
 
59
59
  BUNDLED WITH
60
- 1.17.3
60
+ 2.2.27
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Factory Bot Factory
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/factory_bot_factory.svg)](https://rubygems.org/gems/factory_bot_factory) ![Gem Version](https://app.travis-ci.com/cdragon1116/factory_bot_factory.svg?branch=main)
4
+
3
5
  A Gem that helps you generate FactoryBot's Factory file from exsiting Hash, OpenStruct or ActiveModels.
4
6
 
5
7
  The main purpose is to speed up the process of building big factory.
@@ -19,7 +21,16 @@ Or install it yourself as:
19
21
  $ gem install factory_bot_factory
20
22
 
21
23
 
22
- ## Quick Demo
24
+ ## Qucik Usage
25
+
26
+ ```ruby
27
+ FactoryBotFactory.build({ id: 1 })
28
+ FactoryBotFactory.build(OpenStruct.new(id: 1))
29
+ FactoryBotFactory.build(User.last)
30
+ ```
31
+
32
+
33
+ ## More Options
23
34
 
24
35
  - Build a Hash Factory
25
36
 
@@ -27,11 +38,11 @@ Or install it yourself as:
27
38
  require 'factory_bot_factory'
28
39
 
29
40
  data = { id: 1, tags: ["tag1", "tag2"], address: { country: "US" } }
30
- puts FactoryBotFactory.build("order_hash", Hash, data)
41
+ puts FactoryBotFactory.build(data)
31
42
 
32
43
  # output
33
44
  FactoryBot.define do
34
- factory :order_hash, class: Hash do
45
+ factory :hash, class: Hash do
35
46
  id { 1 }
36
47
  tags do
37
48
  [
@@ -53,11 +64,11 @@ end
53
64
 
54
65
  ```ruby
55
66
  data = { id: 1, tags: ["tag1", "tag2"], address: { country: "US" } }
56
- puts FactoryBotFactory.build("order_hash", Hash, data, nested_level: 2)
67
+ puts FactoryBotFactory.build(data, nested_level: 2)
57
68
 
58
69
  # output
59
70
  FactoryBot.define do
60
- factory :order_hash, class: Hash do
71
+ factory :hash, class: Hash do
61
72
  id { 1 }
62
73
  tags do
63
74
  [
@@ -65,11 +76,11 @@ FactoryBot.define do
65
76
  "tag2"
66
77
  ]
67
78
  end
68
- address { build(:order_hash_address) }
79
+ address { build(:hash_address) }
69
80
  initialize_with { attributes }
70
81
  end
71
82
 
72
- factory :order_hash_address, class: Hash do
83
+ factory :hash_address, class: Hash do
73
84
  country { "US" }
74
85
  initialize_with { attributes }
75
86
  end
@@ -79,34 +90,44 @@ end
79
90
  - Export the file somewhere
80
91
 
81
92
  ```ruby
82
- FactoryBotFactory.build("order_hash", Hash, data, file_path: "spec/factories/order_hash.rb")
93
+ FactoryBotFactory.build(data, file_path: "spec/factories/order_hash.rb")
83
94
 
84
95
  require 'factory_bot'
85
96
  FactoryBot.reload
86
97
  FactoryBot.build(:order_hash, id: 2)
87
-
88
98
  ```
89
99
 
90
- ## Supported Factories
100
+ - Specifize Factory Name
91
101
 
92
- - Hash
93
102
  ```ruby
94
- FactoryBotFactory.build("order_hash", Hash, data)
95
- ```
103
+ puts FactoryBotFactory.build({ id: 1 }, factory_name: 'order')
96
104
 
97
- - OpenStruct
98
- ```
99
- FactoryBotFactory.build("order_open_struct", OpenStruct, data)
105
+ # output
106
+ FactoryBot.define do
107
+ factory :order, class: Hash do
108
+ id { 1 }
109
+ initialize_with { attributes }
110
+ end
111
+ end
100
112
  ```
101
113
 
102
- - Your ActiveModel or ActiveRecord Model
103
- ```
104
- FactoryBotFactory.build("user", User, user_instance)
114
+ - Specifize Output Data Structure: Hash, OpenStruct and your ActiveModel or ActiveRecord Model
115
+
116
+ ```ruby
117
+ puts FactoryBotFactory.build({ id: 1 }, klass: OpenStruct)
118
+
119
+ # output
120
+ FactoryBot.define do
121
+ factory :hash, class: OpenStruct do
122
+ id { 1 }
123
+ to_create {}
124
+ end
125
+ end
105
126
  ```
106
127
 
107
128
  ## Configure your own converter
108
129
 
109
- - Configure
130
+ - Configuration
110
131
 
111
132
  ```ruby
112
133
  FactoryBotFactory.configure do |config|
@@ -120,12 +141,15 @@ FactoryBotFactory.configure do |config|
120
141
  end
121
142
  end
122
143
  end
144
+ ```
123
145
 
124
- FactoryBotFactory.build("order_hash", Hash, { name: 'My Name', id: "de9515ee-006e-4a28-8af3-e88a5c771b93" })
146
+ - Output
147
+ ```ruby
148
+ FactoryBotFactory.build({ name: 'My Name', id: "de9515ee-006e-4a28-8af3-e88a5c771b93" })
125
149
 
126
150
  # output
127
151
  FactoryBot.define do
128
- factory :order_hash, class: Hash do
152
+ factory :hash, class: Hash do
129
153
  name { Faker::Name.name }
130
154
  id { Random.uuid() }
131
155
  initialize_with { attributes }
@@ -133,8 +157,7 @@ FactoryBot.define do
133
157
  end
134
158
  ```
135
159
 
136
- See move converters 'lib/factory_bot_factory/config.rb'
137
-
160
+ See more converters [Here](https://github.com/cdragon1116/factory_bot_factory/blob/master/lib/factory_bot_factory/config.rb#L3-L9)
138
161
 
139
162
  ## Contributing
140
163
 
@@ -24,8 +24,8 @@ Gem::Specification.new do |spec|
24
24
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
25
  spec.require_paths = ["lib"]
26
26
 
27
- spec.add_development_dependency "bundler", "~> 1.17"
28
- spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "bundler", ">= 2.2.10"
28
+ spec.add_development_dependency 'rake', '>= 11.2.2'
29
29
  spec.add_development_dependency "pry", '~> 0.13.1'
30
30
  spec.add_development_dependency "rspec", "~> 3.0"
31
31
  spec.add_development_dependency "factory_bot"
@@ -0,0 +1,27 @@
1
+ module FactoryBotFactory
2
+ class Attribute
3
+ attr_reader :key, :value, :options
4
+
5
+ def initialize(key, value, options = {})
6
+ @key = key
7
+ @value = value
8
+ @options = options
9
+ end
10
+
11
+ def build
12
+ options = [key, value, options]
13
+
14
+ values = if value.nil? || value == "null"
15
+ FactoryBotFactory.config.nil_converter.call(*options)
16
+ elsif value.is_a?(String) || value.is_a?(Symbol)
17
+ FactoryBotFactory.config.string_converter.call(*options)
18
+ elsif value.is_a?(Numeric)
19
+ FactoryBotFactory.config.numertic_converter.call(*options)
20
+ elsif value.is_a?(Hash) || value.is_a?(Array)
21
+ FactoryBotFactory.config.hash_converter.call(*options)
22
+ else value.is_a?(OpenStruct)
23
+ FactoryBotFactory.config.open_struct_converter.call(*options)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,16 +1,33 @@
1
1
  module FactoryBotFactory
2
2
  class Base
3
3
  class << self
4
- def build(factory_name, klass, data, options = {})
4
+ def build(*args)
5
+ factory_name, klass, data, options = build_args(*args)
6
+ options ||= {}
7
+
8
+ raise ArgumentError if factory_name.nil? || klass.nil? || !valid_data?(data)
5
9
  raise NestedToDeepError, 'Only support 5 nested levels' if options[:nested_level].to_i > 5
6
10
 
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."
11
+ factory(klass, data).new({ factory_name: factory_name }.merge(options)).generate(data)
10
12
  end
11
13
 
12
14
  private
13
15
 
16
+ def build_args(*args)
17
+ # To be deprecated
18
+ if args[0].is_a?(String) || args[0].is_a?(Symbol) && args.size >= 3
19
+ args
20
+ else
21
+ arg = args[0]
22
+ options = args[1..-1].inject({}) { |h, _h| h.merge(_h) } if args[1..-1]
23
+ [arg.class.name.downcase, (options[:klass] || arg.class), arg, options]
24
+ end
25
+ end
26
+
27
+ def valid_data?(data)
28
+ data.is_a?(Hash) || data.is_a?(OpenStruct) || data.respond_to?(:attributes)
29
+ end
30
+
14
31
  def factory(klass, data)
15
32
  if data.respond_to?(:attributes) && klass == data.class
16
33
  Object.const_get("FactoryBotFactory::ModelFactory")
@@ -1,5 +1,6 @@
1
1
  module FactoryBotFactory
2
2
  class BaseFactory
3
+
3
4
  def initialize(options = {})
4
5
  @factory_name = options[:factory_name]
5
6
  @file_path = options[:file_path]
@@ -24,12 +25,17 @@ module FactoryBotFactory
24
25
  end
25
26
 
26
27
  output = output.join(LineWriter::NEW_LINE)
27
- File.open(@file_path, 'w') {|f| f.write(output) } if @file_path
28
+ write_to_file(output) if @file_path
28
29
  output
29
30
  end
30
31
 
31
32
  private
32
33
 
34
+ def write_to_file(output)
35
+ raise FileExistsError, "File already exists in #{@file_path}" if File.file?(@file_path)
36
+ File.open(@file_path, 'w') {|f| f.write(output) }
37
+ end
38
+
33
39
  def build_factory(name, value, level)
34
40
  raise "This method should be implemented in a subclass"
35
41
  end
@@ -1,3 +1,5 @@
1
+ require 'factory_bot_factory/factories/base_factory.rb'
2
+
1
3
  module FactoryBotFactory
2
4
  class HashFactory < BaseFactory
3
5
  def build_factory(name, value, level)
@@ -1,3 +1,5 @@
1
+ require 'factory_bot_factory/factories/base_factory.rb'
2
+
1
3
  module FactoryBotFactory
2
4
  class OpenStructFactory < BaseFactory
3
5
  def build_factory(name, value, level)
@@ -1,31 +1,17 @@
1
+ require "factory_bot_factory/attribute"
2
+
1
3
  module FactoryBotFactory
2
4
  class LineWriter
3
5
  NEW_LINE = "\n".freeze
4
6
  WHITE_SPACE = "\s".freeze
5
7
  INDENT_SPACE = 2
6
8
 
7
- attr_reader :hash_converter, :string_converter, :numertic_converter, :open_struct_converter, :nil_converter
8
-
9
9
  def initialize(options = {})
10
- @options = options
10
+ @options = options
11
11
  end
12
12
 
13
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)
14
+ wrap_block(key, Attribute.new(key, value, @options).build)
29
15
  end
30
16
 
31
17
  def build_nested_line(prefix, key)
@@ -1,3 +1,3 @@
1
1
  module FactoryBotFactory
2
- VERSION = "0.0.5"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -2,21 +2,23 @@ require "json"
2
2
  require "factory_bot_factory/version"
3
3
 
4
4
  # Load all Factories and Converters
5
- Dir.glob("#{File.dirname(__FILE__)}/factory_bot_factory/factories/*").each { |file| require(file) }
6
5
  Dir.glob("#{File.dirname(__FILE__)}/factory_bot_factory/converters/*").each { |file| require(file) }
6
+ require "factory_bot_factory/config"
7
7
 
8
- require 'factory_bot_factory/line_writer.rb'
9
8
  require 'factory_bot_factory/base.rb'
10
- require "factory_bot_factory/config"
9
+ require 'factory_bot_factory/line_writer.rb'
10
+ Dir.glob("#{File.dirname(__FILE__)}/factory_bot_factory/factories/*").each { |file| require(file) }
11
+
11
12
 
12
13
  module FactoryBotFactory
13
14
  class Error < StandardError; end
14
15
  class NestedToDeepError < Error; end
16
+ class FileExistsError < ::FactoryBotFactory::Error; end
15
17
 
16
18
  class << self
17
- def build(factory_name, klass, data, options = {})
19
+ def build(*args)
18
20
  configure
19
- Base.build(factory_name, klass, data, options)
21
+ Base.build(*args)
20
22
  end
21
23
 
22
24
  def configure
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factory_bot_factory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - cdragon
@@ -14,30 +14,30 @@ dependencies:
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.17'
19
+ version: 2.2.10
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.17'
26
+ version: 2.2.10
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: 11.2.2
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: 11.2.2
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: pry
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -114,6 +114,7 @@ files:
114
114
  - bin/setup
115
115
  - factory_bot_factory.gemspec
116
116
  - lib/factory_bot_factory.rb
117
+ - lib/factory_bot_factory/attribute.rb
117
118
  - lib/factory_bot_factory/base.rb
118
119
  - lib/factory_bot_factory/config.rb
119
120
  - lib/factory_bot_factory/converters/hash_converter.rb