forked from thoughtbot/factory_bot
/
trait.rb
46 lines (38 loc) · 995 Bytes
/
trait.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
module FactoryGirl
class Trait
attr_reader :name
def initialize(name, &block) #:nodoc:
@name = name
@attribute_list = AttributeList.new
@block = block
proxy = FactoryGirl::DefinitionProxy.new(self)
proxy.instance_eval(&@block) if block_given?
end
def declare_attribute(declaration)
@attribute_list.declare_attribute(declaration)
declaration
end
def add_callback(name, &block)
@attribute_list.add_callback(Callback.new(name, block))
end
def attributes
AttributeList.new.tap do |list|
@attribute_list.declarations.each do |declaration|
declaration.to_attributes.each do |attribute|
list.define_attribute(attribute)
end
end
list.apply_attributes @attribute_list
end
end
def names
[@name]
end
def ==(other)
name == other.name &&
block == other.block
end
protected
attr_reader :block
end
end