class FactoryBot::Factory
@api private
Attributes
Public Class Methods
Source
# File lib/factory_bot/factory.rb, line 9 def initialize(name, options = {}) assert_valid_options(options) @name = name.respond_to?(:to_sym) ? name.to_sym : name.to_s.underscore.to_sym @parent = options[:parent] @aliases = options[:aliases] || [] @class_name = options[:class] @uri_manager = FactoryBot::UriManager.new(names) @definition = Definition.new( @name, options[:traits] || [], automatically_define_enum_traits: options[:automatically_define_enum_traits], uri_manager: @uri_manager ) @compiled = false end
Public Instance Methods
Source
# File lib/factory_bot/factory.rb, line 63 def associations evaluator_class.attribute_list.associations end
Source
# File lib/factory_bot/factory.rb, line 29 def build_class @build_class ||= if class_name.is_a? Class class_name elsif class_name.to_s.safe_constantize class_name.to_s.safe_constantize else class_name.to_s.camelize.constantize end end
Source
# File lib/factory_bot/factory.rb, line 96 def compile unless @compiled parent.compile inherit_parent_traits @definition.inherit_automatically_define_enum_traits(parent.definition) @definition.compile(build_class) build_hierarchy @compiled = true end end
Source
# File lib/factory_bot/factory.rb, line 59 def human_names names.map { |name| name.to_s.humanize.downcase } end
Source
# File lib/factory_bot/factory.rb, line 92 def names [name] + @aliases end
Names for this factory, including aliases.
Example:
factory :user, aliases: [:author] do # ... end FactoryBot.create(:author).class # => User
Because an attribute defined without a value or block will build an association with the same name, this allows associations to be defined without factories, such as:
factory :user, aliases: [:author] do # ... end factory :post do author end FactoryBot.create(:post).author.class # => User
Source
# File lib/factory_bot/factory.rb, line 39 def run(build_strategy, overrides, &block) block ||= ->(result) { result } compile strategy = Strategy.lookup_strategy(build_strategy).new evaluator = evaluator_class.new(strategy, overrides.symbolize_keys) attribute_assigner = AttributeAssigner.new(evaluator, build_class, &compiled_constructor) observer = CallbacksObserver.new(callbacks, evaluator) evaluation = Evaluation.new(evaluator, attribute_assigner, compiled_to_create, observer) evaluation.notify(:before_all, nil) instance = strategy.result(evaluation).tap(&block) evaluation.notify(:after_all, instance) instance end
Source
# File lib/factory_bot/factory.rb, line 107 def with_traits(traits) clone.tap do |factory_with_traits| factory_with_traits.append_traits traits end end
Protected Instance Methods
Source
# File lib/factory_bot/factory.rb, line 123 def attributes compile AttributeList.new(@name).tap do |list| list.apply_attributes definition.attributes end end
Source
# File lib/factory_bot/factory.rb, line 138 def build_hierarchy hierarchy_class.build_from_definition definition end
Source
# File lib/factory_bot/factory.rb, line 142 def callbacks hierarchy_instance.callbacks end
Source
# File lib/factory_bot/factory.rb, line 115 def class_name @class_name || parent.class_name || name end
Source
# File lib/factory_bot/factory.rb, line 150 def compiled_constructor hierarchy_instance.constructor end
Source
# File lib/factory_bot/factory.rb, line 146 def compiled_to_create hierarchy_instance.to_create end
Source
# File lib/factory_bot/factory.rb, line 119 def evaluator_class @evaluator_class ||= EvaluatorClassDefiner.new(attributes, parent.evaluator_class).evaluator_class end
Source
# File lib/factory_bot/factory.rb, line 130 def hierarchy_class @hierarchy_class ||= Class.new(parent.hierarchy_class) end
Source
# File lib/factory_bot/factory.rb, line 134 def hierarchy_instance @hierarchy_instance ||= hierarchy_class.new end
Private Instance Methods
Source
# File lib/factory_bot/factory.rb, line 156 def assert_valid_options(options) options.assert_valid_keys(:class, :parent, :aliases, :traits, :automatically_define_enum_traits) unless [nil, true, false].include?(options[:automatically_define_enum_traits]) raise ArgumentError, "automatically_define_enum_traits must be true or false" end end
Source
# File lib/factory_bot/factory.rb, line 172 def inherit_parent_traits parent.defined_traits.each do |trait| next if defined_traits_names.include?(trait.name) define_trait(trait.clone) end end
Source
# File lib/factory_bot/factory.rb, line 179 def initialize_copy(source) super @definition = @definition.clone @evaluator_class = nil @hierarchy_class = nil @hierarchy_instance = nil @compiled = false end
Calls superclass method
Source
# File lib/factory_bot/factory.rb, line 164 def parent if @parent FactoryBot::Internal.factory_by_name(@parent) else NullFactory.new end end