Sha256: a59a9eab73a686d384bb4fb2ea3995ad761d3b915e467bbdf99646b8c8abcd0c

Contents?: true

Size: 978 Bytes

Versions: 5

Compression:

Stored size: 978 Bytes

Contents

module FactoryGirl
  # @api private
  class StrategySyntaxMethodRegistrar
    def initialize(strategy_name)
      @strategy_name = strategy_name
    end

    def define_strategy_methods
      define_singular_strategy_method
      define_list_strategy_method
    end

    private

    def define_singular_strategy_method
      strategy_name = @strategy_name

      define_syntax_method(strategy_name) do |name, *traits_and_overrides, &block|
        FactoryRunner.new(name, strategy_name, traits_and_overrides).run(&block)
      end
    end

    def define_list_strategy_method
      strategy_name = @strategy_name

      define_syntax_method("#{strategy_name}_list") do |name, amount, *traits_and_overrides, &block|
        amount.times.map { send(strategy_name, name, *traits_and_overrides, &block) }
      end
    end

    def define_syntax_method(name, &block)
      FactoryGirl::Syntax::Methods.module_exec do
        define_method(name, &block)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
challah-1.0.0 vendor/bundle/gems/factory_girl-4.2.0/lib/factory_girl/strategy_syntax_method_registrar.rb
challah-1.0.0.beta3 vendor/bundle/gems/factory_girl-4.2.0/lib/factory_girl/strategy_syntax_method_registrar.rb
challah-1.0.0.beta2 vendor/bundle/gems/factory_girl-4.2.0/lib/factory_girl/strategy_syntax_method_registrar.rb
challah-1.0.0.beta vendor/bundle/gems/factory_girl-4.2.0/lib/factory_girl/strategy_syntax_method_registrar.rb
factory_girl-4.2.0 lib/factory_girl/strategy_syntax_method_registrar.rb