Class: Infoboxer::Tree::Nodes

Inherits:
Array
  • Object
show all
Includes:
Navigation::Lookup::Nodes, Navigation::Sections::Nodes, Navigation::Shortcuts::Nodes, Navigation::Wikipath
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::Wikipath

#wikipath

Methods included from Navigation::Sections::Nodes

#in_sections, #lookup_children, #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



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

%i[select reject sort_by flatten compact grep grep_v - +].each do |sym|
  define_method(sym) do |*args, &block|
    Nodes[*super(*args, &block)]
  end
end

#-(other) ⇒ Object

Just like Array#-, but returns Nodes



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

#compactObject

Just like Array#compact, but returns Nodes



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

#fetchObject

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

See Template#fetch for explanation.



114
115
116
117
118
119
120
121
# File 'lib/infoboxer/tree/nodes.rb', line 114

%i[
  prev_siblings next_siblings siblings
  fetch
].each do |sym|
  define_method(sym) do |*args|
    make_nodes(map { |n| n.send(sym, *args) })
  end
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>)


129
130
131
# File 'lib/infoboxer/tree/nodes.rb', line 129

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.



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

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

#flat_mapObject

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



86
87
88
89
90
91
92
93
# File 'lib/infoboxer/tree/nodes.rb', line 86

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

#flattenObject

Just like Array#flatten, but returns Nodes



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

#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:



169
170
171
172
173
174
175
176
177
178
# File 'lib/infoboxer/tree/nodes.rb', line 169

def follow
  links = grep(Linkable)
  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')
  pages = links.group_by(&:interwiki)
               .flat_map { |iw, ls| page.client.get(*ls.map(&:link), interwiki: iw) }
  pages.count == 1 ? pages.first : Nodes[*pages]
end

#grep(pattern) ⇒ Object

Just like Array#grep, but returns Nodes



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

#grep_v(pattern) ⇒ Object

Just like Array#grep_v, but returns Nodes



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

#group_byObject

Just like Array#group, but returns hash with {<grouping variable> => Nodes}



96
97
98
# File 'lib/infoboxer/tree/nodes.rb', line 96

def group_by
  super.map { |title, group| [title, Nodes[*group]] }.to_h
end

#inspectObject



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

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.



67
68
69
70
71
72
73
# File 'lib/infoboxer/tree/nodes.rb', line 67

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



76
77
78
79
80
81
82
83
# File 'lib/infoboxer/tree/nodes.rb', line 76

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 103

#prev_siblingsObject

Previous siblings (flat list) of all nodes inside.



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

#reject(&block) ⇒ Object

Just like Array#reject, but returns Nodes



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

#select(&block) ⇒ Object

Just like Array#select, but returns Nodes



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

#siblingsObject

Siblings (flat list) of all nodes inside.



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

#sort_by(&block) ⇒ Object

Just like Array#sort_by, but returns Nodes



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

#textObject Also known as: to_s

Just join of all Node#texts inside.



150
151
152
# File 'lib/infoboxer/tree/nodes.rb', line 150

def text
  map(&:text).join
end

#to_treeObject

Just join of all Node#to_tree strings inside.



134
135
136
# File 'lib/infoboxer/tree/nodes.rb', line 134

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

#unwrapObject



156
157
158
# File 'lib/infoboxer/tree/nodes.rb', line 156

def unwrap
  map { |n| n.respond_to?(:unwrap) ? n.unwrap : n }
end