Sha256: 3f04d833ea15750539d16e04592e45d342464549cc4d3b4fbbe660d2b5b0a3bc

Contents?: true

Size: 978 Bytes

Versions: 18

Compression:

Stored size: 978 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

18 entries across 17 versions & 3 rubygems

Version Path
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.1.0/gems/factory_bot-6.5.0/lib/factory_bot/declaration_list.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/factory_bot-6.5.0/lib/factory_bot/declaration_list.rb
factory_bot-6.5.0 lib/factory_bot/declaration_list.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/factory_bot-6.4.6/lib/factory_bot/declaration_list.rb
factory_bot-6.4.6 lib/factory_bot/declaration_list.rb
factory_bot-6.4.5 lib/factory_bot/declaration_list.rb
factory_bot-6.4.4 lib/factory_bot/declaration_list.rb
factory_bot-6.4.3 lib/factory_bot/declaration_list.rb
factory_bot-6.4.2 lib/factory_bot/declaration_list.rb
factory_bot-6.4.1 lib/factory_bot/declaration_list.rb
factory_bot-6.4.0 lib/factory_bot/declaration_list.rb
factory_bot-6.3.0 lib/factory_bot/declaration_list.rb
factory_bot-6.2.1 lib/factory_bot/declaration_list.rb
factory_bot-6.2.0 lib/factory_bot/declaration_list.rb
factory_bot-6.1.0 lib/factory_bot/declaration_list.rb
factory_bot-6.0.2 lib/factory_bot/declaration_list.rb
factory_bot-6.0.1 lib/factory_bot/declaration_list.rb
factory_bot-6.0.0 lib/factory_bot/declaration_list.rb