Sha256: 855f15b1ad5dab36c684313e8540c45ffdb135fd46f0518a3decdc4c2d045d5d

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

require "tsort"

module ROM
  module Factory
    # @api private
    class AttributeRegistry
      include Enumerable
      include TSort

      # @api private
      attr_reader :elements

      # @api private
      def initialize(elements = [])
        @elements = elements
      end

      # @api private
      def each(&block)
        elements.each(&block)
      end

      # @api private
      def [](name)
        detect { |e| e.name.equal?(name) }
      end

      # @api private
      def <<(element)
        existing = self[element.name]
        elements.delete(existing) if existing
        elements << element
        self
      end

      # @api private
      def dup
        self.class.new(elements.dup)
      end

      # @api private
      def values
        self.class.new(elements.select(&:value?))
      end

      # @api private
      def associations
        self.class.new(elements.select { |e| e.is_a?(Attributes::Association::Core) })
      end

      def reject(&block)
        self.class.new(elements.reject(&block))
      end

      private

      # @api private
      def tsort_each_node(&block)
        each(&block)
      end

      # @api private
      def tsort_each_child(attr, &block)
        attr.dependency_names.map { |name| self[name] }.compact.each(&block)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rom-factory-0.12.0 lib/rom/factory/attribute_registry.rb