faker_maker 4.0.0 → 5.0.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4f341f27ec954b732dde7141c9ad532346c09dadbbed4ea469894b10cac652f8
4
- data.tar.gz: 61919d74b140d82fba2750bcf0e1c25473ebe21e02a2b213bd1109d94f22d9e2
3
+ metadata.gz: '09021d0ce98cccbc475dccc29d71e75d6554eeeba2589cae32ba3f1b455146fb'
4
+ data.tar.gz: 2694b0fece8d0853b86cc98f40f175b46a3c6671585c8150462dee2991b9d346
5
5
  SHA512:
6
- metadata.gz: de3ae09ed6b848612d3fc23533d04f4633eaa4be42f7f7be9fc3f3938f8b86afa2a495f6928a66f38207d63233e2b37ce88897178688a4f3aa2c6c5f524b51e4
7
- data.tar.gz: cdc3c4509819316bc389d2bf1857883ddfa62b14b93a8f794a043af70bd603dec46f7e0a0c37d29d8814ff447ac2762cd3d56a92fc917338bc3458b4113263c9
6
+ metadata.gz: 3017220049faf7e81b824bf3250cae33cbb9094c76e9016f1f49931299dd2df4b572a1be740b5e8fd39afa4166bdb5fd49c21c81a6591b67b51b3647764ec6d0
7
+ data.tar.gz: d809da52da4aa002cd0a06fa5d479df8da9e3a8b1b569c484e411c8aef53c648050db60f13a186403e383f3de9197c3773f0e0cb5701d9aa9e1d5427c28ee9aa
@@ -208,6 +208,13 @@ module FakerMaker
208
208
  FakerMaker[parent].populate_instance(instance, attr_override_values, chaos:) if parent?
209
209
 
210
210
  attributes = chaos ? chaos_select(chaos) : @attributes
211
+ # When chaos is enabled, protect explicitly overridden attributes from being affected by chaos;
212
+ # what you pass in is what you get.
213
+ if chaos
214
+ override_attrs = @attributes.select { |attr| attr_override_values.key?( attr.name ) }
215
+ attributes = (attributes + override_attrs).uniq
216
+ attributes = @attributes.select { |attr| attributes.include?( attr ) }
217
+ end
211
218
 
212
219
  attributes.each do |attribute|
213
220
  value = value_for_attribute( instance, attribute, attr_override_values, chaos: )
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FakerMaker
4
- VERSION = '4.0.0'
4
+ VERSION = '5.0.0'
5
5
  end
data/lib/faker_maker.rb CHANGED
@@ -29,7 +29,10 @@ module FakerMaker
29
29
  class NoSuchAttributeError < StandardError; end
30
30
  class ChaosConflictingAttributeError < StandardError; end
31
31
  class NoSuchAttributeNamingStrategy < StandardError; end
32
- # Your code goes here...
32
+
33
+ # Token value for marking attributes as omittable from generated JSON.
34
+ # Usage in factory definitions: `omit: FakerMaker::OMIT`
35
+ OMIT = '__OMIT__'.freeze
33
36
 
34
37
  module_function
35
38
 
@@ -36,3 +36,15 @@ You can also specify which attributes Chaos can use when instantiating your obje
36
36
  ```ruby
37
37
  result = FakerMaker[:item].build( chaos: %i[name manufacturer] )
38
38
  ```
39
+
40
+ ## Overrides are preserved
41
+
42
+ When you pass override attributes at build time, Chaos will not touch them. What you pass in is what you get.
43
+
44
+ ```ruby
45
+ result = FakerMaker[:item].build( attributes: { name: 'Specific Name' }, chaos: true )
46
+ result.name
47
+ => "Specific Name"
48
+ ```
49
+
50
+ Even though `name` is optional and Chaos might otherwise remove it, explicitly overriding it guarantees it will be present with the value you specified.
@@ -25,7 +25,7 @@ FakerMaker.factory :user do
25
25
  end
26
26
 
27
27
  FM[:user].as_json
28
- => {:name=>"Patsy Stone"}
28
+ => {:name=>"Patsy Stone"}
29
29
  ```
30
30
 
31
31
  On the other hand, sometimes you really, really want to destroy a factory and start again (especially if you are experimenting in a REPL for example). FakerMaker allows you to shut a factory which will de-register it from the list of available factories and attempt to unload the class it has built from the Ruby interpreter.
@@ -43,7 +43,7 @@ FakerMaker.factory :user do
43
43
  end
44
44
 
45
45
  FM[:user].as_json
46
- => {:name=>"Patsy Stone", :email=>"patsy@fabulous.co.uk"}
47
- ```
46
+ => {:name=>"Patsy Stone", :email=>"patsy@fabulous.co.uk"}
47
+ ```
48
48
 
49
49
  It also provides the `shut_all!` method to remove all factories.
@@ -53,5 +53,5 @@ FakerMaker.factory :request do
53
53
  end
54
54
 
55
55
  FakerMaker[:request].build.body
56
- # => nil
56
+ => nil
57
57
  ```
@@ -23,7 +23,7 @@ v.engine_capacity = 125
23
23
 
24
24
  v.to_json
25
25
 
26
- => "{\"wheels\":4,\"colour\":\"blue\",\"engineCapacity\":125}"
26
+ => "{\"wheels\":4,\"colour\":\"blue\",\"engineCapacity\":125}"
27
27
  ```
28
28
 
29
29
  ## Per-attribute naming
@@ -12,17 +12,18 @@ end
12
12
  FM[:user].build.as_json
13
13
  => {:name=>"Patsy Stone", :email=>"patsy@fabulous.co.uk", :admin=>false}
14
14
 
15
- FM[:user].build(email: nil).as_json
15
+ FM[:user].build( attributes: { email: nil } ).as_json
16
16
  => {:name=>"Patsy Stone", :admin=>false}
17
17
  ```
18
18
 
19
19
  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.
20
20
 
21
- There are three special modifiers:
21
+ There are four special modifiers:
22
22
 
23
- * `:nil` (symbol) to omit output when the attribute is set to nil
24
- * `:empty` to omit output when the value is an empty string, an empty array or an empty hash
23
+ * `:nil` (symbol) to omit output when the attribute is set to nil.
24
+ * `:empty` to omit output when the value is an empty string, an empty array or an empty hash.
25
25
  * `:always` to never output this attribute.
26
+ * `FakerMaker::OMIT` to omit output only when the attribute is set to this token value.
26
27
 
27
28
  These can be mixed with real values, e.g.
28
29
 
@@ -33,3 +34,26 @@ FakerMaker.factory :user do
33
34
  admin {false}
34
35
  end
35
36
  ```
37
+
38
+ ## Using the omit token
39
+
40
+ You may want to exclude a field but still allow it to be set to `nil`, an empty string/array/hash, or any other value.
41
+
42
+ `FakerMaker::OMIT` is a token value that can provide this flexibility, e.g.
43
+
44
+ ```ruby
45
+ FakerMaker.factory :user do
46
+ name(omit: FakerMaker::OMIT) {'Patsy Stone'}
47
+ email(omit: :nil) {'patsy@fabulous.co.uk'}
48
+ admin(omit: :empty) {false}
49
+ end
50
+
51
+ FM[:user].build( attributes: { name: FakerMaker::OMIT } ).as_json
52
+ => {:email=>"patsy@fabulous.co.uk", :admin=>false}
53
+
54
+ FM[:user].build( attributes: { name: nil, email: nil } ).as_json
55
+ => {:name=>nil, :admin=>false}
56
+
57
+ FM[:user].build( attributes: { name: '', admin: '' } ).as_json
58
+ => {:name=>"", :email=>"patsy@fabulous.co.uk"}
59
+ ```
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faker_maker
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 5.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nigel Brookes-Thomas
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2026-05-07 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: activesupport