faker_maker 1.1.7 → 1.1.8

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: 11bf4bb970ba80be62323b929fc2b6aba156abb1aee757b911d9663f58e081e4
4
- data.tar.gz: c345ab60315a80d82f879f179c18e93ea81ba4ebd2751b0a4269c337a0a3a0ed
3
+ metadata.gz: 6b2f1fd221b22d6e524e97f69eb399bda8d0584192e960fb79e457ac5bfe1bb1
4
+ data.tar.gz: 3c757c4d82d1d8a9bbb0fc31d32911fa8235c4b967eb92d9008d3d8217711f15
5
5
  SHA512:
6
- metadata.gz: 95340d9e637a09583f8c9d8b619391cbedb825655b414ec1b1de073beeb041a2eceba65aaf53fd70929fc6d2b80d9d0237290031031b31da6021622ea678ca0f
7
- data.tar.gz: 9804e0879fe4b39d66e1409134710c90e7af608c6f14a87c35be65605029e1a54506d9ea0d95d94a590106be9051eafe2525a83d8dfcaaa74dcc765adadf1c32
6
+ metadata.gz: ee940fb5629d2a4f56402f164a3cbe11ad2a8150229dfeceeb596e78aa1a624b30bcd1f8518d3167750c97622b630595523448140358bd05e159327deccf38b3
7
+ data.tar.gz: 41f7f57bf43afe5c677592ae608d919aadac205cba4ddce3c4fd16602e6e7163d9cf060c1ce7722e9e77fb6eb4cf82028abc1dfb47f18cdd1ad8c379129f6c70
data/.rubocop.yml CHANGED
@@ -13,4 +13,25 @@ Metrics/BlockLength:
13
13
  - "**/*_spec.rb"
14
14
 
15
15
  Layout/TrailingWhitespace:
16
- Enabled: false
16
+ Enabled: true
17
+
18
+ Layout/SpaceAroundMethodCallOperator:
19
+ Enabled: true
20
+
21
+ Lint/RaiseException:
22
+ Enabled: true
23
+
24
+ Lint/StructNewOverride:
25
+ Enabled: true
26
+
27
+ Style/ExponentialNotation:
28
+ Enabled: true
29
+
30
+ Style/HashEachMethods:
31
+ Enabled: true
32
+
33
+ Style/HashTransformKeys:
34
+ Enabled: true
35
+
36
+ Style/HashTransformValues:
37
+ Enabled: true
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_JOBS: "12"
@@ -2,9 +2,17 @@
2
2
  layout: default
3
3
  title: Destroying Factories
4
4
  parent: Usage
5
- nav_order: 8
5
+ nav_order: 9
6
6
  ---
7
7
 
8
+ ## A Cautionary Tale
9
+
10
+ If you think you want to do this, you are probably wrong. This will not only de-register the factory from Faker Maker, but also delete the class definition from the interpreter. While it's cool that Ruby allows this, it's almost certainly going to hurt.
11
+
12
+ This functionality exists for experimenting with factories in REPLs.
13
+
14
+ Seriously, don't use this in anger.
15
+
8
16
  # Destroying Factories
9
17
 
10
18
  Faker Maker deliberately does not allow you to redefine a factory by redeclaring it. It will also be silent about your attempt to do so. This is to avoid throwing up runtime warning from the Ruby interpreter if you are embedding one factory definition in another.
@@ -43,4 +51,4 @@ FM[:user].as_json
43
51
  => {:name=>"Patsy Stone", :email=>"patsy@fabulous.co.uk"}
44
52
  ```
45
53
 
46
- It also provides the `shut_all!` method to remove all factories.
54
+ It also provides the `shut_all!` method to remove all factories.
@@ -2,7 +2,7 @@
2
2
  layout: default
3
3
  title: Embedding Factories
4
4
  parent: Usage
5
- nav_order: 7
5
+ nav_order: 8
6
6
  ---
7
7
 
8
8
  # Embedding Factories
@@ -0,0 +1,33 @@
1
+ ---
2
+ layout: default
3
+ title: Lifecycle Hooks
4
+ parent: Usage
5
+ nav_order: 8
6
+ ---
7
+
8
+ # Lifecycle Hooks
9
+
10
+ Faker Maker has a few hooks which can be added to the factory which are triggered when the factory builds an instance.
11
+
12
+ * `before_build` the instance has been created but none of the values have been set yet
13
+ * `after_build` the instance has been created and all of the values have been set
14
+
15
+ For instance:
16
+
17
+ ```ruby
18
+ FakerMaker.factory :user do
19
+ before_build do
20
+ puts 'Building an instance of User'
21
+ end
22
+
23
+ name {'Patsy Stone'}
24
+ email {'patsy@fabulous.co.uk'}
25
+ admin {false}
26
+
27
+ after_build do
28
+ puts "Built an instance of User (#{faker_maker_factory.instance.name})"
29
+ end
30
+ end
31
+ ```
32
+
33
+ Access to the factory object is through the `faker_maker_factory` method. The instance under construction is available through the `faker_maker_factory.instance` method.
data/lib/faker_maker.rb CHANGED
@@ -1,14 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'active_support'
4
- require 'active_support/core_ext/string'
5
4
  require 'active_support/core_ext/hash'
6
5
  require 'active_support/core_ext/object/json'
7
- require 'faker_maker/version'
6
+ require 'active_support/core_ext/string'
7
+ require 'faker_maker/attribute'
8
8
  require 'faker_maker/base'
9
- require 'faker_maker/factory'
9
+ require 'faker_maker/lifecycle_hooks'
10
10
  require 'faker_maker/definition_proxy'
11
- require 'faker_maker/attribute'
11
+ require 'faker_maker/factory'
12
+ require 'faker_maker/lifecycle_hooks'
13
+ require 'faker_maker/version'
12
14
 
13
15
  # FakerMaker module for generating Fakes
14
16
  module FakerMaker
@@ -40,21 +42,21 @@ module FakerMaker
40
42
 
41
43
  factory
42
44
  end
43
-
45
+
44
46
  def find_factory( name )
45
47
  factories[name]
46
48
  end
47
-
49
+
48
50
  def shut!( name )
49
51
  factory = find_factory( name )
50
- if factory
51
- factories[name] = nil
52
- Object.send( :remove_const, factory.class_name )
53
- end
52
+ return unless factory
53
+
54
+ factories[name] = nil
55
+ Object.send( :remove_const, factory.class_name )
54
56
  end
55
-
57
+
56
58
  def shut_all!
57
- factories.keys.each { |f| shut!( f ) }
59
+ factories.each_key { |f| shut!( f ) }
58
60
  end
59
61
  end
60
62
 
@@ -12,7 +12,7 @@ module FakerMaker
12
12
  @cardinality = options[:has] || 1
13
13
  @translation = options[:json]
14
14
  @omit = *options[:omit]
15
- @array = options[:array] == true
15
+ @array = options[:array] == true
16
16
  end
17
17
 
18
18
  def array?
@@ -21,7 +21,7 @@ module FakerMaker
21
21
 
22
22
  def cardinality
23
23
  if @cardinality.is_a? Range
24
- rand( @cardinality )
24
+ rand( @cardinality )
25
25
  else
26
26
  @cardinality
27
27
  end
@@ -30,7 +30,7 @@ module FakerMaker
30
30
  def translation?
31
31
  !@translation.blank?
32
32
  end
33
-
33
+
34
34
  def omit?( value )
35
35
  case value
36
36
  when nil
@@ -42,7 +42,7 @@ module FakerMaker
42
42
  end
43
43
  end
44
44
 
45
- private
45
+ private
46
46
 
47
47
  def forced_array?
48
48
  @cardinality.is_a?( Range ) || @cardinality > 1
@@ -5,7 +5,7 @@ module FakerMaker
5
5
  module Base
6
6
  def factory(name, options = {}, &block)
7
7
  factory = FakerMaker.find_factory(name)
8
- if factory.nil?
8
+ if factory.nil?
9
9
  factory = FakerMaker::Factory.new name, options
10
10
  proxy = DefinitionProxy.new factory
11
11
  proxy.instance_eval( &block ) if block_given?
@@ -14,6 +14,5 @@ module FakerMaker
14
14
  factory
15
15
  end
16
16
  end
17
-
18
17
  end
19
18
  end
@@ -3,15 +3,21 @@
3
3
  module FakerMaker
4
4
  # Proxy for mapping attribute names
5
5
  class DefinitionProxy
6
+ include FakerMaker::LifecycleHooks::DefinitionProxy
7
+
6
8
  def initialize(factory)
7
9
  @factory = factory
8
10
  end
9
11
 
12
+ def faker_maker_factory
13
+ @factory
14
+ end
15
+
10
16
  def method_missing(name, *args, &block) # rubocop:disable Style/MethodMissingSuper
11
17
  attribute = FakerMaker::Attribute.new name, block, *args
12
18
  @factory.attach_attribute attribute
13
19
  end
14
-
20
+
15
21
  def respond_to_missing?(method_name, include_private = false)
16
22
  super
17
23
  end
@@ -14,9 +14,9 @@ module FakerMaker
14
14
  @klass = nil
15
15
  @parent = options[:parent]
16
16
  end
17
-
17
+
18
18
  def parent_class
19
- if @parent
19
+ if @parent
20
20
  Object.const_get( FakerMaker[@parent].class_name )
21
21
  else
22
22
  Object
@@ -24,14 +24,19 @@ module FakerMaker
24
24
  end
25
25
 
26
26
  def attach_attribute( attribute )
27
- @attributes << attribute
27
+ @attributes << attribute
28
+ end
29
+
30
+ def instance
31
+ @instance ||= instantiate
28
32
  end
29
33
 
30
34
  def build( attributes = {} )
31
- instance = instantiate
35
+ before_build if respond_to? :before_build
32
36
  assert_only_known_attributes_for_override( attributes )
33
37
  populate_instance instance, attributes
34
38
  yield instance if block_given?
39
+ after_build if respond_to? :after_build
35
40
  instance
36
41
  end
37
42
 
@@ -73,19 +78,19 @@ module FakerMaker
73
78
  collection |= FakerMaker[parent].attribute_names( collection ) if parent?
74
79
  collection | @attributes.map( &:name )
75
80
  end
76
-
81
+
77
82
  def attributes( collection = [] )
78
83
  collection |= FakerMaker[parent].attributes( collection ) if parent?
79
84
  collection | @attributes
80
85
  end
81
-
86
+
82
87
  def find_attribute( name = '' )
83
88
  attributes.filter { |a| [a.name, a.translation].include? name }.first
84
89
  end
85
90
 
86
- protected
91
+ protected
87
92
 
88
- def populate_instance( instance, attr_override_values )
93
+ def populate_instance( instance, attr_override_values )
89
94
  FakerMaker[parent].populate_instance instance, attr_override_values if parent?
90
95
  @attributes.each do |attr|
91
96
  value = value_for_attribute( instance, attr, attr_override_values )
@@ -93,21 +98,20 @@ module FakerMaker
93
98
  end
94
99
  instance.instance_variable_set( :@fm_factory, self )
95
100
  end
96
-
97
-
101
+
98
102
  private
99
-
103
+
100
104
  def assert_only_known_attributes_for_override( attr_override_values )
101
105
  unknown_attrs = attr_override_values.keys - attribute_names
102
106
  issue = "Can't build an instance of '#{class_name}' " \
103
107
  "setting '#{unknown_attrs.join( ', ' )}', no such attribute(s)"
104
108
  raise FakerMaker::NoSuchAttributeError, issue unless unknown_attrs.empty?
105
109
  end
106
-
110
+
107
111
  def attribute_hash_overridden_value?( attr, attr_override_values )
108
112
  attr_override_values.keys.include?( attr.name )
109
113
  end
110
-
114
+
111
115
  def value_for_attribute( instance, attr, attr_override_values )
112
116
  if attribute_hash_overridden_value?( attr, attr_override_values )
113
117
  attr_override_values[attr.name]
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FakerMaker
4
+ module LifecycleHooks
5
+ # Lifecycle hooks which can be called during the building of an instance
6
+ module DefinitionProxy
7
+ def before_build(&block)
8
+ @factory.define_singleton_method(:before_build) { block.call }
9
+ end
10
+
11
+ def after_build(&block)
12
+ @factory.define_singleton_method(:after_build) { block.call }
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FakerMaker
4
- VERSION = '1.1.7'
4
+ VERSION = '1.1.8'
5
5
  end
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.7
4
+ version: 1.1.8
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: 2020-02-21 00:00:00.000000000 Z
11
+ date: 2020-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -220,6 +220,7 @@ files:
220
220
  - _config.yml
221
221
  - bin/console
222
222
  - bin/setup
223
+ - docs/.bundle/config
223
224
  - docs/.keep
224
225
  - docs/_config.yml
225
226
  - docs/contributing.md
@@ -235,6 +236,7 @@ files:
235
236
  - docs/usage/index.md
236
237
  - docs/usage/inheritance.md
237
238
  - docs/usage/json_field_names.md
239
+ - docs/usage/lifecycle_hooks.md
238
240
  - docs/usage/omitting_fields copy.md
239
241
  - faker_maker.gemspec
240
242
  - img/unipug.svg
@@ -243,6 +245,7 @@ files:
243
245
  - lib/faker_maker/base.rb
244
246
  - lib/faker_maker/definition_proxy.rb
245
247
  - lib/faker_maker/factory.rb
248
+ - lib/faker_maker/lifecycle_hooks.rb
246
249
  - lib/faker_maker/version.rb
247
250
  homepage: https://billyruffian.github.io/faker_maker/
248
251
  licenses: