Class: Infoboxer::Tree::Nodes

Inherits:
Array
  • Object
show all
Includes:
Navigation::Lookup::Nodes, Navigation::Sections::Nodes, Navigation::Shortcuts::Nodes
Defined in:
lib/infoboxer/tree/nodes.rb,
lib/infoboxer/navigation.rb

Overview

List of nodes, which tries to be useful both as array, and as proxy to its contents.

Many of Infoboxer's methods (especially Navigation's) return Nodes, and in most cases you don't have to think about it. Same approach can be seen in jQuery or Nokogiri. You just do things like those:

document.sections.                  # => Nodes returned, 
  select{|section|                  #    you can treat them as array, but also...
    section.text.length > 1000      #
  }.                                #
  lookup(:Wikilink, text: /Chile/). #    ...use Infoboxer's methods
  follow.                           #    ...even to receive lists of other pages
  infoboxes.                        #    ...and use methods on them
  fetch('leader_name1').            #    ...including those which only some node types support
  map(&:text)                       #    ...and still have full-functioning Array

Instance Method Summary collapse

Methods included from Navigation::Sections::Nodes

#in_sections, #sections

Methods included from Navigation::Shortcuts::Nodes

#categories, #external_links, #headings, #images, #infoboxes, #lists, #paragraphs, #tables, #templates, #wikilinks

Methods included from Navigation::Lookup::Nodes

#_find, #_lookup, #_lookup_children, #_lookup_next_siblings, #_lookup_parents, #_lookup_prev_siblings, #_lookup_siblings, #find, #lookup, #lookup_children, #lookup_next_siblings, #lookup_parents, #lookup_prev_siblings, #lookup_siblings

Instance Method Details

#-(other) ⇒ Object

Just like Array#-, but returns Nodes



44
45
46
47
48
# File 'lib/infoboxer/tree/nodes.rb', line 44

[:select, :reject, :sort_by, :flatten, :compact, :-].each do |sym|
  define_method(sym){|*args, &block|
    Nodes[*super(*args, &block)]
  }
end

#compactObject

Just like Array#compact, but returns Nodes



# File 'lib/infoboxer/tree/nodes.rb', line 38

#fetchObject

Fetches by name(s) variables for all templates inside.

See Template#fetch for explanation.



92
93
94
95
96
97
98
99
# File 'lib/infoboxer/tree/nodes.rb', line 92

[
  :prev_siblings, :next_siblings, :siblings,
  :fetch
].each do |sym|
  define_method(sym){|*args|
    make_nodes map{|n| n.send(sym, *args)}
  }
end

#fetch_hashes(*args) ⇒ Array<Hash>

By list of variable names, fetches hashes of {name => value} from all templates inside.

See Template#fetch_hash for explanation.

Returns:

  • (Array<Hash>)


107
108
109
# File 'lib/infoboxer/tree/nodes.rb', line 107

def fetch_hashes(*args)
  map{|t| t.fetch_hash(*args)}
end

#first(n = nil) ⇒ Object

Just like Array#first, but returns Nodes, if provided with n of elements.



51
52
53
54
55
56
57
# File 'lib/infoboxer/tree/nodes.rb', line 51

def first(n = nil)
  if n.nil?
    super()
  else
    Nodes[*super(n)]
  end
end

#flattenObject

Just like Array#flatten, but returns Nodes



# File 'lib/infoboxer/tree/nodes.rb', line 35

#followNodes<MediaWiki::Page>

Fetches pages by ALL wikilinks inside in ONE query to MediaWiki API.

NB: for now, if there's more then 50 wikilinks (limitation for one request to API), Infoboxer will not try to do next page. It will be fixed in next releases.

Returns:



140
141
142
143
144
145
146
147
# File 'lib/infoboxer/tree/nodes.rb', line 140

def follow
  links = select{|n| n.respond_to?(:link)}.map(&:link)
  return Nodes[] if links.empty?
  page = first.lookup_parents(MediaWiki::Page).first or
    fail("Not in a page from real source")
  page.client or fail("MediaWiki client not set")
  page.client.get(*links)
end

#inspectObject



116
117
118
119
120
121
122
123
124
# File 'lib/infoboxer/tree/nodes.rb', line 116

def inspect
  '[' + 
    case
    when count > MAX_CHILDREN
      self[0...MAX_CHILDREN].map(&:inspect).join(', ') + ", ...#{count - MAX_CHILDREN} more nodes"
    else
      map(&:inspect).join(', ')
    end + ']'
end

#last(n = nil) ⇒ Object

Just like Array#last, but returns Nodes, if provided with n of elements.



60
61
62
63
64
65
66
# File 'lib/infoboxer/tree/nodes.rb', line 60

def last(n = nil)
  if n.nil?
    super()
  else
    Nodes[*super(n)]
  end
end

#mapObject

Just like Array#map, but returns Nodes, if all map results are Node



69
70
71
72
73
74
75
76
# File 'lib/infoboxer/tree/nodes.rb', line 69

def map
  res = super
  if res.all?{|n| n.is_a?(Node) || n.is_a?(Nodes)}
    Nodes[*res]
  else
    res
  end
end

#next_siblingsObject

Next siblings (flat list) of all nodes inside.



# File 'lib/infoboxer/tree/nodes.rb', line 81

#prev_siblingsObject

Previous siblings (flat list) of all nodes inside.



# File 'lib/infoboxer/tree/nodes.rb', line 78

#reject(&block) ⇒ Object

Just like Array#reject, but returns Nodes



# File 'lib/infoboxer/tree/nodes.rb', line 29

#select(&block) ⇒ Object

Just like Array#select, but returns Nodes



# File 'lib/infoboxer/tree/nodes.rb', line 26

#siblingsObject

Siblings (flat list) of all nodes inside.



# File 'lib/infoboxer/tree/nodes.rb', line 84

#sort_by(&block) ⇒ Object

Just like Array#sort_by, but returns Nodes



# File 'lib/infoboxer/tree/nodes.rb', line 32

#textObject

Just join of all Node#texts inside.



127
128
129
# File 'lib/infoboxer/tree/nodes.rb', line 127

def text
  map(&:text).join
end

#to_treeObject

Just join of all Node#to_tree strings inside.



112
113
114
# File 'lib/infoboxer/tree/nodes.rb', line 112

def to_tree
  map(&:to_tree).join("\n")
end