Sha256: 30ff3ed8decd331e636b11e8b24a02c2732f4c7f923d892d8d255d336a66e0a7

Contents?: true

Size: 936 Bytes

Versions: 6

Compression:

Stored size: 936 Bytes

Contents

module FactoryBot
  class Callback
    attr_reader :name

    def initialize(name, block)
      @name  = name.to_sym
      @block = block
      ensure_valid_callback_name!
    end

    def run(instance, evaluator)
      case block.arity
      when 1, -1 then syntax_runner.instance_exec(instance, &block)
      when 2 then syntax_runner.instance_exec(instance, evaluator, &block)
      else        syntax_runner.instance_exec(&block)
      end
    end

    def ==(other)
      name == other.name &&
        block == other.block
    end

    protected
    attr_reader :block

    private

    def ensure_valid_callback_name!
      unless FactoryBot.callback_names.include?(name)
        raise InvalidCallbackNameError, "#{name} is not a valid callback name. " +
          "Valid callback names are #{FactoryBot.callback_names.inspect}"
      end
    end

    def syntax_runner
      @syntax_runner ||= SyntaxRunner.new
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
factory_bot-4.11.1 lib/factory_bot/callback.rb
factory_bot-4.11.0 lib/factory_bot/callback.rb
factory_bot-4.10.0 lib/factory_bot/callback.rb
factory_bot-4.8.2 lib/factory_bot/callback.rb
factory_bot-1.0.1.alpha lib/factory_bot/callback.rb
factory_bot-1.0.0.alpha lib/factory_bot/callback.rb