faker_maker 1.1.2 → 1.1.3
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 +4 -4
- data/Gemfile +2 -0
- data/README.md +69 -0
- data/Rakefile +2 -0
- data/bin/console +1 -0
- data/faker_maker.gemspec +4 -2
- data/lib/faker_maker.rb +2 -0
- data/lib/faker_maker/attribute.rb +15 -1
- data/lib/faker_maker/base.rb +2 -0
- data/lib/faker_maker/definition_proxy.rb +2 -0
- data/lib/faker_maker/factory.rb +20 -3
- data/lib/faker_maker/version.rb +3 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8dad7967bc0e3c689498afafca60a7b9c691aa8af466880ac5af87d5668e4706
|
4
|
+
data.tar.gz: c7659747e882e4a9361507759629f4482068475c859dc9883a79ffdb62b916fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fb83e7d087b54877d133d89b7b00f4c849eb8355c5dc9233b53dfe69d8a9096ec8f2da6edc5539a2334ab7afc880e1c5fb6369b590e69b31e8a7a99fb94b39f2
|
7
|
+
data.tar.gz: 1e661f28f49569d73dbeeb8ddbd27226a5d124522d6fd07b53be9341f7021f7dc46595d9d1f55303f62df92a1d49b7276c68e854fee6f84e03b1866aa6197c79
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -140,8 +140,33 @@ FakerMaker.factory :basket do
|
|
140
140
|
end
|
141
141
|
```
|
142
142
|
|
143
|
+
### Organising dependencies
|
144
|
+
|
145
|
+
Factory definition files are Plain Ol' Ruby. If you depend on another factory because you either extend from it or use it just add a `require` or (depending on your load path) `require_relative` to the top of your file.
|
146
|
+
|
143
147
|
### JSON field names
|
144
148
|
|
149
|
+
JavaScript likes to use camelCase, Ruby's idiom is to use snake_case. This can make make manipulating factory-built objects in ruby ugly. To avoid this, you can call your fields one thing and ask the JSON outputter to rename the field when generating JSON.
|
150
|
+
|
151
|
+
```ruby
|
152
|
+
FakerMaker.factory :vehicle do
|
153
|
+
wheels { 4 }
|
154
|
+
colour { Faker::Color.color_name }
|
155
|
+
engine_capacity(json: 'engineCapacity') { rand( 600..2500 ) }
|
156
|
+
end
|
157
|
+
|
158
|
+
v = FM[:vehicle].build
|
159
|
+
v.engine_capacity = 125
|
160
|
+
```
|
161
|
+
|
162
|
+
and calls to `as_json` and `to_json` will report the fieldname as `engineCapacity`.
|
163
|
+
|
164
|
+
```ruby
|
165
|
+
v.to_json
|
166
|
+
|
167
|
+
=> "{\"wheels\":4,\"colour\":\"blue\",\"engineCapacity\":125}"
|
168
|
+
```
|
169
|
+
|
145
170
|
### Building instances
|
146
171
|
|
147
172
|
Instances are Plain Ol' Ruby Objects and the attributes are attached with getters and setters with their values assigned to the value return from their block at build time.
|
@@ -166,6 +191,14 @@ or by passing in a block:
|
|
166
191
|
result = FakerMaker[:item].build{ |i| i.name = 'Electric Sheep' }
|
167
192
|
```
|
168
193
|
|
194
|
+
this is particularly useful for overriding nested values, since all the getters and setters of the embedded objects are already constructed:
|
195
|
+
|
196
|
+
```ruby
|
197
|
+
result = FakerMaker[:basket].build do |b|
|
198
|
+
b.items.first.name = 'Neon Badger'
|
199
|
+
end
|
200
|
+
```
|
201
|
+
|
169
202
|
if you're crazy enough to want to do both styles during creation, the values in the block will be preserved, e.g.
|
170
203
|
|
171
204
|
```ruby
|
@@ -192,6 +225,42 @@ As another convenience, `FakerMaker` is also assigned to the variable `FM` to it
|
|
192
225
|
result = FM[:basket].build
|
193
226
|
```
|
194
227
|
|
228
|
+
### Omitting fields
|
229
|
+
|
230
|
+
Sometimes you want a field present, other times you don't. This is often the case when you want to skip fields which have null or empty values.
|
231
|
+
|
232
|
+
```ruby
|
233
|
+
FakerMaker.factory :user do
|
234
|
+
name {'Patsy Stone'}
|
235
|
+
email(omit: :nil) {'patsy@fabulous.co.uk'}
|
236
|
+
admin {false}
|
237
|
+
end
|
238
|
+
|
239
|
+
FM[:user].build.as_json
|
240
|
+
=> {:name=>"Patsy Stone", :email=>"patsy@fabulous.co.uk", :admin=>false}
|
241
|
+
|
242
|
+
FM[:user].build(email: nil).as_json
|
243
|
+
=> {:name=>"Patsy Stone", :admin=>false}
|
244
|
+
```
|
245
|
+
|
246
|
+
The `omit` modifier can take a single value or an array. If it is passed a value and the attribute equals this value, it will not be included in the output from `as_json` (which returns a Ruby Hash) or in `to_json` methods.
|
247
|
+
|
248
|
+
There are three special modifiers:
|
249
|
+
|
250
|
+
* `:nil` (symbol) to omit output when the attribute is set to nil
|
251
|
+
* `:empty` to omit output when the value is an empty string, an empty array or an empty hash
|
252
|
+
* `:always` to never output this attibute.
|
253
|
+
|
254
|
+
These can be mixed with real values, e.g.
|
255
|
+
|
256
|
+
```ruby
|
257
|
+
FakerMaker.factory :user do
|
258
|
+
name {'Patsy Stone'}
|
259
|
+
email(omit: [:nil, :empty, 'test@foobar.com']) {'patsy@fabulous.co.uk'}
|
260
|
+
admin {false}
|
261
|
+
end
|
262
|
+
```
|
263
|
+
|
195
264
|
### Embedding factories
|
196
265
|
|
197
266
|
To use factories with factories, the following pattern is recommended:
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
data/faker_maker.gemspec
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Disable rubocop checks for the .gemspec
|
2
4
|
# I'll take the output from 'bundle gem new' to be authoritative
|
3
5
|
# rubocop:disable all
|
@@ -39,10 +41,10 @@ Gem::Specification.new do |spec|
|
|
39
41
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
40
42
|
spec.require_paths = ['lib']
|
41
43
|
|
42
|
-
spec.add_dependency 'activesupport', '>= 5.2', '<
|
44
|
+
spec.add_dependency 'activesupport', '>= 5.2', '< 7'
|
43
45
|
|
44
46
|
spec.add_development_dependency 'bundler', '~> 2.0'
|
45
|
-
spec.add_development_dependency 'faker', '~> 1
|
47
|
+
spec.add_development_dependency 'faker', '~> 2.1'
|
46
48
|
spec.add_development_dependency 'pry', '~> 0.12'
|
47
49
|
spec.add_development_dependency 'rake', '~> 12.3'
|
48
50
|
spec.add_development_dependency 'rspec', '~> 3.8'
|
data/lib/faker_maker.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module FakerMaker
|
2
4
|
# Attributes describe the fields of classes
|
3
5
|
class Attribute
|
@@ -9,6 +11,7 @@ module FakerMaker
|
|
9
11
|
@block = block || proc { nil }
|
10
12
|
@cardinality = options[:has] || 1
|
11
13
|
@translation = options[:json]
|
14
|
+
@omit = *options[:omit]
|
12
15
|
@array = options[:array] == true
|
13
16
|
end
|
14
17
|
|
@@ -27,6 +30,17 @@ module FakerMaker
|
|
27
30
|
def translation?
|
28
31
|
!@translation.blank?
|
29
32
|
end
|
33
|
+
|
34
|
+
def omit?( value )
|
35
|
+
case value
|
36
|
+
when nil
|
37
|
+
@omit.include? :nil
|
38
|
+
when '', [], {}
|
39
|
+
@omit.include? :empty
|
40
|
+
else
|
41
|
+
@omit.include?( :always ) || @omit.include?( value )
|
42
|
+
end
|
43
|
+
end
|
30
44
|
|
31
45
|
private
|
32
46
|
|
@@ -35,7 +49,7 @@ module FakerMaker
|
|
35
49
|
end
|
36
50
|
|
37
51
|
def assert_valid_options( options )
|
38
|
-
options.assert_valid_keys :has, :array, :json
|
52
|
+
options.assert_valid_keys :has, :array, :json, :omit
|
39
53
|
end
|
40
54
|
end
|
41
55
|
end
|
data/lib/faker_maker/base.rb
CHANGED
data/lib/faker_maker/factory.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# rubocop:disable Metrics/ClassLength
|
2
4
|
module FakerMaker
|
3
5
|
# Factories construct instances of a fake
|
4
6
|
class Factory
|
5
|
-
attr_reader :name, :
|
7
|
+
attr_reader :name, :class_name, :parent
|
6
8
|
|
7
9
|
def initialize( name, options = {} )
|
8
10
|
assert_valid_options options
|
@@ -47,6 +49,10 @@ module FakerMaker
|
|
47
49
|
build.to_json
|
48
50
|
end
|
49
51
|
|
52
|
+
def as_json(*_args)
|
53
|
+
build.as_json
|
54
|
+
end
|
55
|
+
|
50
56
|
def parent?
|
51
57
|
!@parent.nil?
|
52
58
|
end
|
@@ -65,7 +71,16 @@ module FakerMaker
|
|
65
71
|
|
66
72
|
def attribute_names( collection = [] )
|
67
73
|
collection |= FakerMaker[parent].attribute_names( collection ) if parent?
|
68
|
-
collection | attributes.map( &:name )
|
74
|
+
collection | @attributes.map( &:name )
|
75
|
+
end
|
76
|
+
|
77
|
+
def attributes( collection = [] )
|
78
|
+
collection |= FakerMaker[parent].attributes( collection ) if parent?
|
79
|
+
collection | @attributes
|
80
|
+
end
|
81
|
+
|
82
|
+
def find_attribute( name = '' )
|
83
|
+
attributes.filter { |a| a.name == name }.first
|
69
84
|
end
|
70
85
|
|
71
86
|
protected
|
@@ -116,7 +131,9 @@ module FakerMaker
|
|
116
131
|
|
117
132
|
def attach_json_overrides_to_class
|
118
133
|
@klass.define_method :as_json do |options = {}|
|
119
|
-
super( options.merge( except: 'fm_factory' ) )
|
134
|
+
super( options.merge( except: 'fm_factory' ) )
|
135
|
+
.transform_keys { |key| @fm_factory.json_key_map[key] || key }
|
136
|
+
.filter { |key, value| !@fm_factory.find_attribute(key)&.omit?( value ) }
|
120
137
|
end
|
121
138
|
end
|
122
139
|
|
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: 1.1.
|
4
|
+
version: 1.1.3
|
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: 2019-
|
11
|
+
date: 2019-08-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: '5.2'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '
|
22
|
+
version: '7'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,7 +29,7 @@ dependencies:
|
|
29
29
|
version: '5.2'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
32
|
+
version: '7'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: bundler
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -50,14 +50,14 @@ dependencies:
|
|
50
50
|
requirements:
|
51
51
|
- - "~>"
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version: '1
|
53
|
+
version: '2.1'
|
54
54
|
type: :development
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
57
|
requirements:
|
58
58
|
- - "~>"
|
59
59
|
- !ruby/object:Gem::Version
|
60
|
-
version: '1
|
60
|
+
version: '2.1'
|
61
61
|
- !ruby/object:Gem::Dependency
|
62
62
|
name: pry
|
63
63
|
requirement: !ruby/object:Gem::Requirement
|