Sha256: d6736db394411fab64e6a3d7885d9fc0595fac68416d236ebf32cad5fcaee1bb

Contents?: true

Size: 939 Bytes

Versions: 20

Compression:

Stored size: 939 Bytes

Contents

module FactoryGirl
  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 FactoryGirl.callback_names.include?(name)
        raise InvalidCallbackNameError, "#{name} is not a valid callback name. " +
          "Valid callback names are #{FactoryGirl.callback_names.inspect}"
      end
    end

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

Version data entries

20 entries across 17 versions & 8 rubygems

Version Path
ffactory_girl-4.8.2 lib/factory_girl/callback.rb
factory_girl-4.9.0 lib/factory_girl/callback.rb
ffactory_girl-4.8.1 lib/factory_girl/callback.rb
factory_girl-4.8.1 lib/factory_girl/callback.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/factory_girl-4.8.0/lib/factory_girl/callback.rb
factory_girl-4.8.0 lib/factory_girl/callback.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/factory_girl-4.7.0/lib/factory_girl/callback.rb
factory_girl-4.7.0 lib/factory_girl/callback.rb
opal-factory_girl-4.5.0.3 factory_girl/lib/factory_girl/callback.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/callback.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/factory_girl-4.5.0/lib/factory_girl/callback.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/factory_girl-4.5.0/lib/factory_girl/callback.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/factory_girl-4.5.0/lib/factory_girl/callback.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/factory_girl-4.4.0/lib/factory_girl/callback.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/factory_girl-4.4.0/lib/factory_girl/callback.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/factory_girl-4.4.0/lib/factory_girl/callback.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/factory_girl-4.4.0/lib/factory_girl/callback.rb
factory_girl-4.5.0 lib/factory_girl/callback.rb
factory_girl-4.4.0 lib/factory_girl/callback.rb
factory_girl-4.3.0 lib/factory_girl/callback.rb