Class: Infoboxer::Tree::Compound

Inherits:
Node
  • Object
show all
Defined in:
lib/infoboxer/tree/compound.rb

Overview

Base class for all nodes with children.

Instance Attribute Summary collapse

Attributes inherited from Node

#params, #parent

Instance Method Summary collapse

Methods inherited from Node

#==, coder, def_readers, #first?, #index, #inspect, #next_siblings, #prev_siblings, #siblings, #text_, #to_s

Methods included from Navigation::Wikipath

#wikipath

Methods included from Navigation::Sections::Node

#in_sections

Methods included from Navigation::Shortcuts::Node

#bold?, #categories, #external_links, #heading?, #headings, #images, #infobox, #infoboxes, #italic?, #lists, #paragraphs, #tables, #templates, #wikilinks

Methods included from Navigation::Lookup::Node

#_lookup, #_lookup_children, #_lookup_next_siblings, #_lookup_parents, #_lookup_prev_sibling, #_lookup_prev_siblings, #_lookup_siblings, #_matches?, #lookup, #lookup_children, #lookup_next_siblings, #lookup_parents, #lookup_prev_sibling, #lookup_prev_siblings, #lookup_siblings, #matches?, #parent?

Constructor Details

#initialize(children = Nodes.new, **params) ⇒ Compound

Returns a new instance of Compound.



7
8
9
10
11
# File 'lib/infoboxer/tree/compound.rb', line 7

def initialize(children = Nodes.new, **params)
  super(**params)
  @children = Nodes[*children]
  @children.each { |c| c.parent = self }
end

Instance Attribute Details

#childrenNodes (readonly)

List of children

Returns:



16
17
18
# File 'lib/infoboxer/tree/compound.rb', line 16

def children
  @children
end

Instance Method Details

#index_of(child) ⇒ Fixnum

Index of provided node in children list

Returns:

  • (Fixnum)

    or nil if not a child



21
22
23
# File 'lib/infoboxer/tree/compound.rb', line 21

def index_of(child)
  children.index(child)
end

#textObject

See Node#text



34
35
36
# File 'lib/infoboxer/tree/compound.rb', line 34

def text
  children.map(&:text).join(children_separator)
end

#to_tree(level = 0) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/infoboxer/tree/compound.rb', line 39

def to_tree(level = 0)
  if children.count == 1 && children.first.is_a?(Text)
    "#{indent(level)}#{children.first.text} <#{descr}>\n"
  else
    "#{indent(level)}<#{descr}>\n" + children.map { |c| c.to_tree(level + 1) }.join
  end
end