Sha256: 01ec4e0bc07b121b7a28936b605718c3c9736c46e5d2711fd44db8882d9fa6db

Contents?: true

Size: 988 Bytes

Versions: 6

Compression:

Stored size: 988 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.inject([]) { |result, declaration| result += declaration.to_attributes }
    end

    def overridable?
      @overridable
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

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