Sha256: 4032005d901724f838ab754663553141858ce377f095f85991ba9c7a7b2d1d15

Contents?: true

Size: 1.28 KB

Versions: 148

Compression:

Stored size: 1.28 KB

Contents

module FactoryGirl
  class AttributeList
    include Enumerable

    def initialize(name = nil)
      @name       = name
      @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 associations
      @attributes.select(&:association?)
    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

148 entries across 93 versions & 7 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute_list.rb
classiccms-0.7.5 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/attribute_list.rb
classiccms-0.7.4 vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute_list.rb
classiccms-0.7.4 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/attribute_list.rb
classiccms-0.7.3 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/attribute_list.rb
classiccms-0.7.3 vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute_list.rb
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/factory_girl-2.6.4/lib/factory_girl/attribute_list.rb
classiccms-0.7.2 vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute_list.rb
classiccms-0.7.2 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/attribute_list.rb
classiccms-0.7.1 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/attribute_list.rb
classiccms-0.7.1 vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute_list.rb
classiccms-0.7.0 vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute_list.rb
classiccms-0.7.0 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/attribute_list.rb
classiccms-0.6.9 vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute_list.rb
classiccms-0.6.9 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/attribute_list.rb
classiccms-0.6.8 vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute_list.rb
classiccms-0.6.8 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/attribute_list.rb
classiccms-0.6.7 vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute_list.rb
classiccms-0.6.7 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/attribute_list.rb
classiccms-0.6.6 vendor/bundle/gems/factory_girl-3.2.0/lib/factory_girl/attribute_list.rb