2017-10-20 15:20:28 -04:00
|
|
|
module FactoryBot
|
2012-05-05 02:31:31 -04:00
|
|
|
# @api private
|
2011-08-12 16:16:17 -04:00
|
|
|
class Trait
|
2012-05-06 16:56:37 -04:00
|
|
|
attr_reader :name, :definition
|
2011-08-12 14:38:33 -04:00
|
|
|
|
2012-05-05 02:31:31 -04:00
|
|
|
def initialize(name, &block)
|
2019-02-22 15:53:39 -05:00
|
|
|
@name = name.to_s
|
2011-10-14 15:14:43 -04:00
|
|
|
@block = block
|
2012-05-11 11:43:08 -04:00
|
|
|
@definition = Definition.new(@name)
|
2017-10-20 15:20:28 -04:00
|
|
|
proxy = FactoryBot::DefinitionProxy.new(@definition)
|
2019-09-10 19:24:20 -04:00
|
|
|
|
|
|
|
if block_given?
|
|
|
|
proxy.instance_eval(&@block)
|
|
|
|
end
|
2011-08-09 20:29:02 -04:00
|
|
|
end
|
|
|
|
|
2012-05-11 11:43:08 -04:00
|
|
|
delegate :add_callback, :declare_attribute, :to_create, :define_trait, :constructor,
|
2012-03-09 17:20:38 -05:00
|
|
|
:callbacks, :attributes, to: :@definition
|
2011-08-12 14:38:33 -04:00
|
|
|
|
2011-08-10 14:11:53 -04:00
|
|
|
def names
|
|
|
|
[@name]
|
|
|
|
end
|
2011-10-14 15:14:43 -04:00
|
|
|
|
|
|
|
def ==(other)
|
|
|
|
name == other.name &&
|
|
|
|
block == other.block
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
2018-09-27 21:35:05 -04:00
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
attr_reader :block
|
2011-08-09 20:29:02 -04:00
|
|
|
end
|
|
|
|
end
|