Sha256: f092ebc66893dc8e142bd9bcdf3b27da9e7f38f8199947620cb20364bc44c645

Contents?: true

Size: 763 Bytes

Versions: 3

Compression:

Stored size: 763 Bytes

Contents

require 'forwardable'
require 'taketo/support'

module Taketo
  module Support
    class NamedNodesCollection
      include Enumerable
      extend Forwardable

      def_delegators :@nodes, :each, :length, :size, :empty?

      def initialize(nodes = [])
        @nodes = nodes
      end

      def push(node)
        @nodes << node unless find_by_name(node.name)
      end
      alias :<< :push

      def [](index)
        if index.is_a?(Symbol)
          node = find_by_name(index) or raise KeyError, "Element with name #{index} not found"
          return node
        end
        @nodes[index] or raise KeyError, "Element ##{index} not found"
      end

      def find_by_name(name)
        @nodes.detect { |n| n.name == name }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
taketo-0.0.5 lib/taketo/support/named_nodes_collection.rb
taketo-0.0.4 lib/taketo/support/named_nodes_collection.rb
taketo-0.0.3 lib/taketo/support/named_nodes_collection.rb