2011-09-16 16:06:32 -04:00
|
|
|
module FactoryGirl
|
|
|
|
class Callback
|
2011-09-16 16:19:42 -04:00
|
|
|
VALID_NAMES = [:after_build, :after_create, :after_stub].freeze
|
|
|
|
|
2011-09-16 16:06:32 -04:00
|
|
|
attr_reader :name, :block
|
|
|
|
|
|
|
|
def initialize(name, block)
|
|
|
|
@name = name.to_sym
|
|
|
|
@block = block
|
2011-09-16 16:19:42 -04:00
|
|
|
check_name
|
|
|
|
end
|
|
|
|
|
2011-09-16 16:24:16 -04:00
|
|
|
def run(instance, proxy)
|
|
|
|
case block.arity
|
|
|
|
when 1 then block.call(instance)
|
|
|
|
when 2 then block.call(instance, proxy)
|
|
|
|
else block.call
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-14 22:50:37 -04:00
|
|
|
def ==(other)
|
|
|
|
name == other.name &&
|
|
|
|
block == other.block
|
|
|
|
end
|
|
|
|
|
2011-09-16 16:19:42 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def check_name
|
|
|
|
unless VALID_NAMES.include?(name)
|
|
|
|
raise InvalidCallbackNameError, "#{name} is not a valid callback name. " +
|
|
|
|
"Valid callback names are #{VALID_NAMES.inspect}"
|
|
|
|
end
|
2011-09-16 16:06:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|