Class: Taketo::Support::NamedNodesCollection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/taketo/support/named_nodes_collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(nodes = []) ⇒ NamedNodesCollection

Returns a new instance of NamedNodesCollection.



13
14
15
# File 'lib/taketo/support/named_nodes_collection.rb', line 13

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

Instance Method Details

#==(other) ⇒ Object



35
36
37
38
# File 'lib/taketo/support/named_nodes_collection.rb', line 35

def ==(other)
  return true if other.equal?(self)
  @nodes == other.is_a?(NamedNodesCollection) ? other.nodes : other
end

#[](index) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/taketo/support/named_nodes_collection.rb', line 23

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

#find_by_name(name) ⇒ Object



31
32
33
# File 'lib/taketo/support/named_nodes_collection.rb', line 31

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

#push(node) ⇒ Object Also known as: <<



17
18
19
20
# File 'lib/taketo/support/named_nodes_collection.rb', line 17

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