fixture_factory 0.2.1 → 0.4.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: a3cfc56492aba74f896ae46e75abc1444ea6dce64c836da9e24b046aebff9f43
4
- data.tar.gz: d0e255b0c304c9763806f49acc00463d3b522f62db3356ab8703471befcca245
3
+ metadata.gz: c3dd15d0c2961a9b7faf54aa2c96eb7a251b2fcaef8d54de0979716875d9ed92
4
+ data.tar.gz: 629b71fe3025fa1e2c9c9168e8a84e54e0f24753b81f9641c6a30d4c59407e96
5
5
  SHA512:
6
- metadata.gz: 78368269a25209bd0dac219b4680879456353ab490300facf0a6ac427cc6df3400295ae24dfb7059ff4148c4221b06719794b6403ebacf095bc0e0f21f8b97b9
7
- data.tar.gz: 82e7039c1d6b27c570239627020cc28c130ea576120bda2cfa6b64ff17b28ecc134d832c22c212af92653be68823ed398036998fef74e0d10548f0f989717bf6
6
+ metadata.gz: 9f6f47491dad7bfe82f35d88093f912599b5fb28488808b1830ded445da43a27156984cf0f0c45eee762693a98edf5adc2409a72191dc64c2a5f2dafbd2d633b
7
+ data.tar.gz: e885a929f6b8505d64568640738b5969e641d94a48e57aa235e0c572fe69f9344c72f7ae5ffb5865de3904862bb85bdcef52cbeffb6b8af4f9ce432782dc1c8c
@@ -5,20 +5,20 @@ module FixtureFactory
5
5
  EMPTY_BLOCK = proc {}
6
6
 
7
7
  attr_writer :block
8
- attr_accessor :class_name, :fixture_method, :fixture_name, :parent, :sequence
8
+ attr_accessor :fixture_method, :fixture_name, :parent, :sequence, :proc_class
9
9
 
10
10
  def initialize(name, options = {})
11
- self.parent = options.fetch(:parent) { default_parent_for(name) }
12
- self.class_name = options.fetch(:class_name) { parent.class_name }
13
- self.fixture_method = options.fetch(:via) { parent.fixture_method }
14
- self.fixture_name = options.fetch(:like) { parent.fixture_name }
15
- self.block = options.fetch(:block) { EMPTY_BLOCK }
11
+ self.parent = options.fetch(:parent) { default_parent_for(name) }
12
+ self.proc_class = options.fetch(:class) { parent.proc_class }
13
+ self.fixture_method = options.fetch(:via) { parent.fixture_method }
14
+ self.fixture_name = options.fetch(:like) { parent.fixture_name }
15
+ self.block = options.fetch(:block) { EMPTY_BLOCK }
16
16
  self.sequence = Sequence.new
17
17
  end
18
18
 
19
19
  def block
20
20
  all_blocks = [parent&.block, @block].compact
21
- -> (*args) do
21
+ ->(*args) do
22
22
  all_blocks.reduce({}) do |attributes, block|
23
23
  block_attributes = FixtureFactory.evaluate(
24
24
  block, args: args, context: self
@@ -29,9 +29,9 @@ module FixtureFactory
29
29
  end
30
30
 
31
31
  def klass
32
- @klass ||= class_name.to_s.constantize
32
+ @klass ||= proc_class.call
33
33
  rescue NameError
34
- raise WrongClassError, class_name
34
+ raise WrongClassError, proc_class
35
35
  end
36
36
 
37
37
  def fixture_args
@@ -70,7 +70,7 @@ module FixtureFactory
70
70
  name,
71
71
  parent: nil,
72
72
  like: nil,
73
- class_name: name.to_s.classify,
73
+ class: -> { name.to_s.classify.safe_constantize },
74
74
  via: name.to_s.pluralize,
75
75
  )
76
76
  end
@@ -30,14 +30,32 @@ module FixtureFactory
30
30
  end
31
31
 
32
32
  class WrongClassError < Error
33
- def initialize(class_name)
34
- super(
35
- <<~MSG.squish
36
- No class named "#{class_name}".
37
- Try using the `class_name` option in your definition to specify a valid class name.
38
- https://github.com/Shopify/fixture_factory/blob/master/README.md#naming
39
- MSG
40
- )
33
+ def initialize(klass)
34
+ message = if klass.is_a?(String)
35
+ string_class_error_message(klass)
36
+ else
37
+ proc_class_error_message(klass)
38
+ end
39
+ super(message)
40
+ end
41
+
42
+ private
43
+
44
+ def proc_class_error_message(proc_class)
45
+ location, line_number = proc_class.source_location
46
+ <<~MSG.squish
47
+ Constant defined in file #{location} on line #{line_number} is not defined.
48
+ Try using the `class` option in your definition to specify a valid class name.
49
+ https://github.com/Shopify/fixture_factory/blob/master/README.md#naming
50
+ MSG
51
+ end
52
+
53
+ def string_class_error_message(class_name)
54
+ <<~MSG.squish
55
+ No class named "#{class_name}".
56
+ Try using the `class_name` option in your definition to specify a valid class name.
57
+ https://github.com/Shopify/fixture_factory/blob/master/README.md#naming
58
+ MSG
41
59
  end
42
60
  end
43
61
  end
@@ -6,10 +6,6 @@ module FixtureFactory
6
6
  module Registry
7
7
  extend ActiveSupport::Concern
8
8
 
9
- included do
10
- class_attribute(:fixture_factory_definitions) # :nodoc:
11
- end
12
-
13
9
  class_methods do
14
10
  # Defines a fixture given a name, options, and a block.
15
11
  #
@@ -46,12 +42,13 @@ module FixtureFactory
46
42
  # factory(:admin, via: :users, like: :bob_admin, class: 'User')
47
43
  # end
48
44
  def factory(name, options = {}, &block)
49
- if options.key?(:class)
50
- ActiveSupport::Deprecation.warn(<<~MSG.squish)
51
- factory class: option is deprecated and will be removed.
52
- Please use the class_name: option instead.
53
- MSG
54
- options[:class_name] ||= options.delete(:class)
45
+ if options.key?(:class_name)
46
+ options[:class] ||= -> do
47
+ class_name = options.delete(:class_name).to_s
48
+ class_name.constantize
49
+ rescue NameError
50
+ raise WrongClassError, class_name
51
+ end
55
52
  end
56
53
 
57
54
  parent = all_factory_definitions[options[:parent]]
@@ -61,7 +58,7 @@ module FixtureFactory
61
58
  end
62
59
 
63
60
  # Sets up factories definitions for the current class scope. Accepts an
64
- # optional block to define factories in.
61
+ # optional block to define factories in. Calling this method multiple times appends to existing definitions.
65
62
  #
66
63
  # === Example
67
64
  #
@@ -75,10 +72,18 @@ module FixtureFactory
75
72
  # factory(:post)
76
73
  # end
77
74
  def define_factories(&block)
78
- self.fixture_factory_definitions = {}.with_indifferent_access
75
+ fixture_factory_definitions
79
76
  instance_exec(&block) if block.present?
80
77
  end
81
78
 
79
+ def fixture_factory_definitions # :nodoc:
80
+ @fixture_factory_definitions ||= {}.with_indifferent_access
81
+ end
82
+
83
+ def fixture_factory_definitions=(value) # :nodoc:
84
+ @fixture_factory_definitions = value
85
+ end
86
+
82
87
  def all_factory_definitions # :nodoc:
83
88
  fixtury_ancestors = ancestors.select do |ancestor|
84
89
  ancestor.respond_to?(:fixture_factory_definitions)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FixtureFactory
4
- VERSION = "0.2.1"
4
+ VERSION = "0.4.0"
5
5
  end
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'fixture_factory/version'
4
- require 'fixture_factory/definition'
5
- require 'fixture_factory/methods'
6
- require 'fixture_factory/registry'
7
- require 'fixture_factory/sequence'
8
- require 'fixture_factory/errors'
3
+ require "fixture_factory/version"
4
+ require "fixture_factory/definition"
5
+ require "fixture_factory/methods"
6
+ require "fixture_factory/registry"
7
+ require "fixture_factory/sequence"
8
+ require "fixture_factory/errors"
9
9
 
10
10
  module FixtureFactory
11
11
  class << self
@@ -32,6 +32,7 @@ module FixtureFactory
32
32
 
33
33
  def extract_attributes(object)
34
34
  raise ArgumentError, "FixtureFactory blocks must return a hash-like object" unless object.respond_to?(:to_h)
35
+
35
36
  object.to_h.symbolize_keys
36
37
  end
37
38
 
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fixture_factory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shopify Inc.
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-25 00:00:00.000000000 Z
11
+ date: 2026-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: activerecord
14
+ name: activemodel
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '5.2'
27
27
  - !ruby/object:Gem::Dependency
28
- name: activemodel
28
+ name: activerecord
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: sqlite3
70
+ name: byebug
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
@@ -81,7 +81,7 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: byebug
84
+ name: minitest
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
@@ -109,7 +109,7 @@ dependencies:
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  - !ruby/object:Gem::Dependency
112
- name: minitest
112
+ name: sqlite3
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - ">="
@@ -122,7 +122,7 @@ dependencies:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
- description:
125
+ description:
126
126
  email:
127
127
  - gems@shopify.com
128
128
  executables: []
@@ -141,7 +141,7 @@ licenses:
141
141
  - MIT
142
142
  metadata:
143
143
  allowed_push_host: https://rubygems.org
144
- post_install_message:
144
+ post_install_message:
145
145
  rdoc_options: []
146
146
  require_paths:
147
147
  - lib
@@ -149,15 +149,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
149
149
  requirements:
150
150
  - - ">="
151
151
  - !ruby/object:Gem::Version
152
- version: '0'
152
+ version: 2.7.0
153
153
  required_rubygems_version: !ruby/object:Gem::Requirement
154
154
  requirements:
155
155
  - - ">="
156
156
  - !ruby/object:Gem::Version
157
157
  version: '0'
158
158
  requirements: []
159
- rubygems_version: 3.0.3
160
- signing_key:
159
+ rubygems_version: 3.5.22
160
+ signing_key:
161
161
  specification_version: 4
162
162
  summary: Factories via fixtures
163
163
  test_files: []