Class: SpecForge::Attribute::Factory
- Inherits:
-
Parameterized
- Object
- Attribute
- Parameterized
- SpecForge::Attribute::Factory
- Includes:
- Chainable
- Defined in:
- lib/spec_forge/attribute/factory.rb
Overview
Represents an attribute that references a factory to generate test data
This class allows SpecForge to integrate with FactoryBot for test data generation. It supports various build strategies like create, build, build_stubbed, etc.
Constant Summary collapse
- KEYWORD_REGEX =
Regular expression pattern that matches attribute keywords with this prefix Used for identifying this attribute type during parsing
/^factories\./i- BASE_STRATEGIES =
An array of base strategies that can be provided either with or without a size. "stubbed" will automatically be transformed into "build_stubbed"
%w[ build create build_stubbed attributes_for ].freeze
- BUILD_STRATEGIES =
Returns All available build strategies.
%w[ attributes_for attributes_for_list build build_list build_pair build_stubbed build_stubbed_list create create_list create_pair ].freeze
Constants included from Chainable
Instance Attribute Summary
Attributes included from Chainable
#header, #invocation_chain, #keyword
Attributes inherited from Parameterized
Instance Method Summary collapse
-
#base_object ⇒ Object
Returns the base object for the variable chain.
-
#initialize ⇒ Factory
constructor
Creates a new factory attribute with the specified name and arguments.
-
#resolve ⇒ Object
Similar to #resolved but doesn't cache the result, allowing for re-resolution.
Methods included from Chainable
Methods inherited from Parameterized
Constructor Details
#initialize ⇒ Factory
Creates a new factory attribute with the specified name and arguments
81 82 83 84 85 86 87 88 |
# File 'lib/spec_forge/attribute/factory.rb', line 81 def initialize(...) super # Check the arguments before preparing them arguments[:keyword] = Normalizer.normalize!(arguments[:keyword], using: :factory_reference) prepare_arguments end |
Instance Method Details
#base_object ⇒ Object
Returns the base object for the variable chain
95 96 97 98 99 100 101 102 103 |
# File 'lib/spec_forge/attribute/factory.rb', line 95 def base_object attributes = arguments[:keyword] # Default functionality is to create ("factory.user") return FactoryBot.create(factory_name) if attributes.blank? build_arguments = construct_factory_parameters(attributes) FactoryBot.public_send(*build_arguments) end |
#resolve ⇒ Object
Similar to #resolved but doesn't cache the result, allowing for re-resolution. Recursively calls #resolve on all nested attributes without storing results.
Use this when you need to ensure fresh values each time, particularly with factories or other attributes that should generate new values on each call.
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/spec_forge/attribute/factory.rb', line 119 def resolve case value when Array value.map(&resolve_proc) when Hash value.transform_values(&resolve_proc) else value end end |