Class: FactoryGirl::AttributeList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/factory_girl/attribute_list.rb

Instance Method Summary collapse

Constructor Details

#initializeAttributeList

Returns a new instance of AttributeList.



5
6
7
# File 'lib/factory_girl/attribute_list.rb', line 5

def initialize
  @attributes = {}
end

Instance Method Details

#add_callback(name, &block) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/factory_girl/attribute_list.rb', line 17

def add_callback(name, &block)
  unless valid_callback_names.include?(name.to_sym)
    raise InvalidCallbackNameError, "#{name} is not a valid callback name. Valid callback names are #{valid_callback_names.inspect}"
  end

  add_attribute Attribute::Callback.new(name.to_sym, block)
end

#apply_attributes(attributes_to_apply) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/factory_girl/attribute_list.rb', line 36

def apply_attributes(attributes_to_apply)
  new_attributes = []

  attributes_to_apply.each do |attribute|
    if attribute_defined?(attribute.name)
      @attributes.each_value do |attributes|
        attributes.delete_if do |attrib|
          new_attributes << attrib.clone if attrib.name == attribute.name
        end
      end
    else
      new_attributes << attribute.clone
    end
  end

  prepend_attributes new_attributes
end

#attribute_defined?(attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
# File 'lib/factory_girl/attribute_list.rb', line 29

def attribute_defined?(attribute_name)
  !@attributes.values.flatten.detect do |attribute|
    attribute.name == attribute_name &&
      !attribute.is_a?(FactoryGirl::Attribute::Callback)
  end.nil?
end

#define_attribute(attribute) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/factory_girl/attribute_list.rb', line 9

def define_attribute(attribute)
  if attribute_defined?(attribute.name)
    raise AttributeDefinitionError, "Attribute already defined: #{attribute.name}"
  end

  add_attribute attribute
end

#each(&block) ⇒ Object



25
26
27
# File 'lib/factory_girl/attribute_list.rb', line 25

def each(&block)
  flattened_attributes.each(&block)
end