Sha256: e067e0ad4dd09769c8edfec42a5776b50db313815255cb619d3f6a5ec177a2a7

Contents?: true

Size: 1.52 KB

Versions: 6

Compression:

Stored size: 1.52 KB

Contents

module FactoryBot
  # @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

6 entries across 6 versions & 1 rubygems

Version Path
factory_bot-4.11.1 lib/factory_bot/attribute_list.rb
factory_bot-4.11.0 lib/factory_bot/attribute_list.rb
factory_bot-4.10.0 lib/factory_bot/attribute_list.rb
factory_bot-4.8.2 lib/factory_bot/attribute_list.rb
factory_bot-1.0.1.alpha lib/factory_bot/attribute_list.rb
factory_bot-1.0.0.alpha lib/factory_bot/attribute_list.rb