1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00
thoughtbot--factory_bot/lib/factory_bot/internal.rb
Daniel Colson 0f4b404569 Deprecate remaining undocumented internal methods
This commit deprecates several undocumented top-level FactoryBot
methods:

  * callbacks
  * configuration
  * constructor
  * initialize_with
  * register_sequence
  * resent_configuration
  * skip_create
  * to_create

factory_bot users are not meant to access these methods directly.
Instead they are available for internal use through the new Internal
module introduced in #1262.

---

While addressing the new deprecation warnings, I tried to make the
delegation a bit more consistent, always delegating to
`FactoryBot::Internal`.

This involved requiring "factory_bot/internal" sooner. To avoid having
to fight with the order of requires and potential circular dependencies,
I moved the default strategy and callback registration out of constants
(this is how they used to be before #1297), so it doesn't matter if
those strategies are loaded before the `Internal` module. I also deleted
a pair of tests that were using these constants - these strategies and
callbacks are covered elsewhere in our test suite, and I don't think
these tests were adding much value (they missed #1379, for example).
2020-04-23 19:35:39 -04:00

104 lines
2.5 KiB
Ruby

module FactoryBot
# @api private
module Internal
class << self
delegate :after,
:before,
:callback_names,
:callbacks,
:constructor,
:factories,
:initialize_with,
:inline_sequences,
:sequences,
:skip_create,
:strategies,
:to_create,
:traits,
to: :configuration
def configuration
@configuration ||= Configuration.new
end
def reset_configuration
@configuration = nil
end
def register_inline_sequence(sequence)
inline_sequences.push(sequence)
end
def rewind_inline_sequences
inline_sequences.each(&:rewind)
end
def register_trait(trait)
trait.names.each do |name|
traits.register(name, trait)
end
trait
end
def trait_by_name(name)
traits.find(name)
end
def register_sequence(sequence)
sequence.names.each do |name|
sequences.register(name, sequence)
end
sequence
end
def sequence_by_name(name)
sequences.find(name)
end
def rewind_sequences
sequences.each(&:rewind)
rewind_inline_sequences
end
def register_factory(factory)
factory.names.each do |name|
factories.register(name, factory)
end
factory
end
def factory_by_name(name)
factories.find(name)
end
def register_strategy(strategy_name, strategy_class)
strategies.register(strategy_name, strategy_class)
StrategySyntaxMethodRegistrar.new(strategy_name).define_strategy_methods
end
def strategy_by_name(name)
strategies.find(name)
end
def register_default_strategies
register_strategy(:build, FactoryBot::Strategy::Build)
register_strategy(:create, FactoryBot::Strategy::Create)
register_strategy(:attributes_for, FactoryBot::Strategy::AttributesFor)
register_strategy(:build_stubbed, FactoryBot::Strategy::Stub)
register_strategy(:null, FactoryBot::Strategy::Null)
end
def register_default_callbacks
register_callback(:after_create)
register_callback(:after_build)
register_callback(:after_stub)
register_callback(:before_create)
end
def register_callback(name)
name = name.to_sym
callback_names << name
end
end
end
end