faker_maker 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/docs/usage/embedding_factories.md +51 -1
- data/docs/usage/index.md +11 -0
- data/faker_maker.gemspec +1 -0
- data/lib/faker_maker/attribute.rb +4 -3
- data/lib/faker_maker/factory.rb +20 -2
- data/lib/faker_maker/version.rb +1 -1
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a077bc198db9d38ed9784aa30231b51955197b1ae28c0fc790aad28bd5c07f9f
|
4
|
+
data.tar.gz: f8eea37df0773cf0abb16f57e104a45941e24577c3ca0eb9ff19fa7922cad93a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d2423079545d812ca8434016d2e34399ac4c72be3cabdb237a36fe4f645de606c87f308cbcb082ad8dda0638f49fbd84a409421ee447fa0b67297475c1724f87
|
7
|
+
data.tar.gz: f6ead79d10a9f82bffc4a031629ce3c1c3b7885563db2c9ff00a6fc671e5d207bc4405cde40af11cee0261e95d5dbbd64b7cb5e7087749898d7d42587e626125
|
@@ -15,9 +15,59 @@ FakerMaker.factory :item do
|
|
15
15
|
price { Faker::Commerce.price }
|
16
16
|
end
|
17
17
|
|
18
|
+
FakerMaker.factory :basket do
|
19
|
+
items( has: 10, factory: :item )
|
20
|
+
end
|
21
|
+
```
|
22
|
+
|
23
|
+
In this example, FakerMaker will build an `item` (well, 10 `item`s in this case) using item factory as it is building a `basket`. The advantage of this method is that `item` factory can be declared *after* the `basket` factory.
|
24
|
+
|
25
|
+
If you want to select randomly from one or more factories, provide an array of factory names:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
FakerMaker.factory :coupon do
|
29
|
+
discount { Faker::Commerce.price }
|
30
|
+
end
|
31
|
+
|
32
|
+
FakerMaker.factory :item do
|
33
|
+
name { Faker::Commerce.product_name }
|
34
|
+
price { Faker::Commerce.price }
|
35
|
+
end
|
36
|
+
|
37
|
+
FakerMaker.factory :basket do
|
38
|
+
items( has: 10, factory: [:item, :discount] )
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
In this example, through 10 iterations, one of `item` and `discount` factories will be called to build their objects.
|
43
|
+
|
44
|
+
Blocks can still be provided and the referenced factory built object will be passed to the block:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
FakerMaker.factory :item do
|
48
|
+
name { Faker::Commerce.product_name }
|
49
|
+
price { Faker::Commerce.price }
|
50
|
+
end
|
51
|
+
|
52
|
+
FakerMaker.factory :basket do
|
53
|
+
items( has: 10, factory: :item ) { |item| item.price = 10.99 ; item}
|
54
|
+
end
|
55
|
+
```
|
56
|
+
**Important:** the value for the attribute will be the value returned from the block. If you want to modify the contents of the referenced factory's object, don't forget to return it at the end of the block (as above).
|
57
|
+
|
58
|
+
## Alternative method
|
59
|
+
|
60
|
+
There is an alternative style which might be of use:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
FakerMaker.factory :item do
|
64
|
+
name { Faker::Commerce.product_name }
|
65
|
+
price { Faker::Commerce.price }
|
66
|
+
end
|
67
|
+
|
18
68
|
FakerMaker.factory :basket do
|
19
69
|
items( has: 10 ) { FakerMaker[:item].build }
|
20
70
|
end
|
21
71
|
```
|
22
72
|
|
23
|
-
|
73
|
+
With this pattern, you might have to [manage your dependencies]({% link usage/dependencies.md %}) and `require` your referenced factory.
|
data/docs/usage/index.md
CHANGED
@@ -49,4 +49,15 @@ FakerMaker.factory :user, class: 'EmailUser' do
|
|
49
49
|
email {'patsy@fabulous.co.uk'}
|
50
50
|
admin {false}
|
51
51
|
end
|
52
|
+
```
|
53
|
+
|
54
|
+
Fields with no block (or reference to another factory) will be nil.
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
FakerMaker.factory :request do
|
58
|
+
body
|
59
|
+
end
|
60
|
+
|
61
|
+
FakerMaker[:request].build.body
|
62
|
+
# => nil
|
52
63
|
```
|
data/faker_maker.gemspec
CHANGED
@@ -53,6 +53,7 @@ Gem::Specification.new do |spec|
|
|
53
53
|
spec.add_development_dependency 'rake', '~> 13.0'
|
54
54
|
spec.add_development_dependency 'rspec', '~> 3.8'
|
55
55
|
spec.add_development_dependency 'rubocop', '~> 1.0'
|
56
|
+
spec.add_development_dependency 'racc', '~> 1.0'
|
56
57
|
|
57
58
|
spec.required_ruby_version = '>= 3.0'
|
58
59
|
end
|
@@ -3,18 +3,19 @@
|
|
3
3
|
module FakerMaker
|
4
4
|
# Attributes describe the fields of classes
|
5
5
|
class Attribute
|
6
|
-
attr_reader :name, :block, :translation, :required, :optional, :optional_weighting
|
6
|
+
attr_reader :name, :block, :translation, :required, :optional, :optional_weighting, :embedded_factories
|
7
7
|
|
8
8
|
DEFAULT_OPTIONAL_WEIGHTING = 0.5
|
9
9
|
|
10
10
|
def initialize( name, block = nil, options = {} )
|
11
11
|
assert_valid_options options
|
12
12
|
@name = name
|
13
|
-
@block = block ||
|
13
|
+
@block = block || nil
|
14
14
|
@cardinality = options[:has] || 1
|
15
15
|
@translation = options[:json]
|
16
16
|
@omit = *options[:omit]
|
17
17
|
@array = options[:array] == true
|
18
|
+
@embedded_factories = *options[:factory]
|
18
19
|
|
19
20
|
if options[:required].to_s.downcase.eql?('true') || options[:optional].to_s.downcase.eql?('false')
|
20
21
|
@required = true
|
@@ -58,7 +59,7 @@ module FakerMaker
|
|
58
59
|
end
|
59
60
|
|
60
61
|
def assert_valid_options( options )
|
61
|
-
options.assert_valid_keys :has, :array, :json, :omit, :required, :optional
|
62
|
+
options.assert_valid_keys :has, :array, :json, :omit, :required, :optional, :factory
|
62
63
|
end
|
63
64
|
|
64
65
|
def determine_optional_weighting( value )
|
data/lib/faker_maker/factory.rb
CHANGED
@@ -81,6 +81,7 @@ module FakerMaker
|
|
81
81
|
def as_json(*_args)
|
82
82
|
build.as_json
|
83
83
|
end
|
84
|
+
alias to_h as_json
|
84
85
|
|
85
86
|
def parent?
|
86
87
|
!@parent.nil?
|
@@ -163,12 +164,28 @@ module FakerMaker
|
|
163
164
|
if attribute_hash_overridden_value?( attr, attr_override_values )
|
164
165
|
attr_override_values[attr.name]
|
165
166
|
elsif attr.array?
|
166
|
-
[].tap
|
167
|
+
[].tap do |a|
|
168
|
+
attr.cardinality.times do
|
169
|
+
manufacture = manufacture_from_embedded_factory( attr )
|
170
|
+
# if manufacture has been build and there is a block, instance_exec the block
|
171
|
+
# otherwise just add the manufacture to the array
|
172
|
+
a << (attr.block ? instance.instance_exec(manufacture, &attr.block) : manufacture)
|
173
|
+
end
|
174
|
+
end
|
167
175
|
else
|
168
|
-
|
176
|
+
manufacture = manufacture_from_embedded_factory( attr )
|
177
|
+
attr.block ? instance.instance_exec(manufacture, &attr.block) : manufacture
|
169
178
|
end
|
170
179
|
end
|
171
180
|
|
181
|
+
def manufacture_from_embedded_factory( attr )
|
182
|
+
# The name of the embedded factory randomly selected from the list of embedded factories.
|
183
|
+
embedded_factory_name = attr.embedded_factories.sample
|
184
|
+
# The object that is being manufactured by the factory.
|
185
|
+
# If an embedded factory name is provided, it builds the object using FakerMaker.
|
186
|
+
embedded_factory_name ? FakerMaker[embedded_factory_name].build : nil
|
187
|
+
end
|
188
|
+
|
172
189
|
def instantiate
|
173
190
|
assemble.new
|
174
191
|
end
|
@@ -186,6 +203,7 @@ module FakerMaker
|
|
186
203
|
.transform_keys { |key| @fm_factory.json_key_map[key] || key }
|
187
204
|
.filter { |key, value| !@fm_factory.find_attribute(key)&.omit?( value ) }
|
188
205
|
end
|
206
|
+
@klass.alias_method :to_h, :as_json
|
189
207
|
end
|
190
208
|
|
191
209
|
def assert_valid_options( options )
|
data/lib/faker_maker/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faker_maker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nigel Brookes-Thomas
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-05-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -170,6 +170,20 @@ dependencies:
|
|
170
170
|
- - "~>"
|
171
171
|
- !ruby/object:Gem::Version
|
172
172
|
version: '1.0'
|
173
|
+
- !ruby/object:Gem::Dependency
|
174
|
+
name: racc
|
175
|
+
requirement: !ruby/object:Gem::Requirement
|
176
|
+
requirements:
|
177
|
+
- - "~>"
|
178
|
+
- !ruby/object:Gem::Version
|
179
|
+
version: '1.0'
|
180
|
+
type: :development
|
181
|
+
prerelease: false
|
182
|
+
version_requirements: !ruby/object:Gem::Requirement
|
183
|
+
requirements:
|
184
|
+
- - "~>"
|
185
|
+
- !ruby/object:Gem::Version
|
186
|
+
version: '1.0'
|
173
187
|
description: FakerMaker is a simple factory builder so you can throw away your fixtures
|
174
188
|
and generate test data instead.
|
175
189
|
email:
|
@@ -248,7 +262,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
248
262
|
- !ruby/object:Gem::Version
|
249
263
|
version: '0'
|
250
264
|
requirements: []
|
251
|
-
rubygems_version: 3.
|
265
|
+
rubygems_version: 3.5.9
|
252
266
|
signing_key:
|
253
267
|
specification_version: 4
|
254
268
|
summary: FakerMaker bakes fakes.
|