Sha256: 928935499f201c7a8e36818dbb905e3abbd4164c8d32a0932d95d7cd7a4e6e80

Contents?: true

Size: 1.52 KB

Versions: 94

Compression:

Stored size: 1.52 KB

Contents

module FactoryGirl
  # @api private
  class AttributeList
    include Enumerable

    def initialize(name = nil, attributes = [])
      @name       = name
      @attributes = attributes
    end

    def define_attribute(attribute)
      ensure_attribute_not_self_referencing! attribute
      ensure_attribute_not_defined! attribute

      add_attribute attribute
    end

    def each(&block)
      @attributes.each(&block)
    end

    def names
      map(&:name)
    end

    def associations
      AttributeList.new(@name, select(&:association?))
    end

    def ignored
      AttributeList.new(@name, select(&:ignored))
    end

    def non_ignored
      AttributeList.new(@name, reject(&:ignored))
    end

    def apply_attributes(attributes_to_apply)
      attributes_to_apply.each {|attribute| add_attribute(attribute) }
    end

    private

    def add_attribute(attribute)
      @attributes << attribute
      attribute
    end

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

    def ensure_attribute_not_self_referencing!(attribute)
      if attribute.respond_to?(:factory) && attribute.factory == @name
        raise AssociationDefinitionError, "Self-referencing association '#{attribute.name}' in '#{attribute.factory}'"
      end
    end

    def attribute_defined?(attribute_name)
      @attributes.any? do |attribute|
        attribute.name == attribute_name
      end
    end
  end
end

Version data entries

94 entries across 84 versions & 5 rubygems

Version Path
factory_girl-4.0.0.rc1 lib/factory_girl/attribute_list.rb
classiccms-0.5.2 vendor/bundle/gems/factory_girl-3.3.0/lib/factory_girl/attribute_list.rb
factory_girl-3.6.1 lib/factory_girl/attribute_list.rb
classiccms-0.5.1 vendor/bundle/gems/factory_girl-3.3.0/lib/factory_girl/attribute_list.rb
classiccms-0.5.0 vendor/bundle/gems/factory_girl-3.3.0/lib/factory_girl/attribute_list.rb
factory_girl-3.6.0 lib/factory_girl/attribute_list.rb
classiccms-0.4.2 vendor/bundle/gems/factory_girl-3.3.0/lib/factory_girl/attribute_list.rb
classiccms-0.4.1 vendor/bundle/gems/factory_girl-3.3.0/lib/factory_girl/attribute_list.rb
classiccms-0.4.0 vendor/bundle/gems/factory_girl-3.3.0/lib/factory_girl/attribute_list.rb
classiccms-0.3.9 vendor/bundle/gems/factory_girl-3.3.0/lib/factory_girl/attribute_list.rb
challah-0.6.2 vendor/bundle/gems/factory_girl-3.5.0/lib/factory_girl/attribute_list.rb
challah-0.6.2 vendor/bundle/gems/factory_girl-3.3.0/lib/factory_girl/attribute_list.rb
sunrise-cms-0.3.3 vendor/bundle/ruby/1.9.1/gems/factory_girl-3.3.0/lib/factory_girl/attribute_list.rb
factory_girl-3.5.0 lib/factory_girl/attribute_list.rb
sunrise-cms-0.3.2 vendor/bundle/ruby/1.9.1/gems/factory_girl-3.3.0/lib/factory_girl/attribute_list.rb
sunrise-cms-0.3.1 vendor/bundle/ruby/1.9.1/gems/factory_girl-3.3.0/lib/factory_girl/attribute_list.rb
factory_girl-3.4.2 lib/factory_girl/attribute_list.rb
sunrise-cms-0.3.0 vendor/bundle/ruby/1.9.1/gems/factory_girl-3.3.0/lib/factory_girl/attribute_list.rb
factory_girl-3.4.1 lib/factory_girl/attribute_list.rb
factory_girl-3.4.0 lib/factory_girl/attribute_list.rb