Sha256: ab26a5175df529b7e44c1702713cff7b944701243dc277f5f1eebaebdf3241bc

Contents?: true

Size: 987 Bytes

Versions: 9

Compression:

Stored size: 987 Bytes

Contents

module FactoryBot
  # @api private
  class DeclarationList
    include Enumerable

    def initialize(name = nil)
      @declarations = []
      @name         = name
      @overridable  = false
    end

    def declare_attribute(declaration)
      delete_declaration(declaration) if overridable?

      @declarations << declaration
      declaration
    end

    def overridable
      @overridable = true
    end

    def attributes
      @attributes ||= AttributeList.new(@name).tap do |list|
        to_attributes.each do |attribute|
          list.define_attribute(attribute)
        end
      end
    end

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

    private

    def delete_declaration(declaration)
      @declarations.delete_if { |decl| decl.name == declaration.name }
    end

    def to_attributes
      @declarations.reduce([]) { |result, declaration| result + declaration.to_attributes }
    end

    def overridable?
      @overridable
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
factory_bot-5.2.0 lib/factory_bot/declaration_list.rb
factory_bot-5.1.2 lib/factory_bot/declaration_list.rb
factory_bot-5.1.1 lib/factory_bot/declaration_list.rb
factory_bot-5.1.0 lib/factory_bot/declaration_list.rb
factory_bot-5.0.2 lib/factory_bot/declaration_list.rb
factory_bot-5.0.1 lib/factory_bot/declaration_list.rb
factory_bot-5.0.0 lib/factory_bot/declaration_list.rb
factory_bot-5.0.0.rc2 lib/factory_bot/declaration_list.rb
factory_bot-5.0.0.rc1 lib/factory_bot/declaration_list.rb