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_girl/callback.rb

36 lines
747 B
Ruby
Raw Normal View History

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
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