Sha256: 539dc567f85420e78aa264a0aa69823179b84568d12deb21125244a1db9d4dbd

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 KB

Contents

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.kind_of?(Attributes::Association::Core) })
      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

3 entries across 3 versions & 1 rubygems

Version Path
rom-factory-0.7.0 lib/rom/factory/attribute_registry.rb
rom-factory-0.6.0 lib/rom/factory/attribute_registry.rb
rom-factory-0.5.0 lib/rom/factory/attribute_registry.rb