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



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

#-(other) ⇒ Object

Just like Array#-, but returns Nodes



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

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



124
125
126
127
128
129
130
131
# File 'lib/infoboxer/tree/nodes.rb', line 124

%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>)


139
140
141
# File 'lib/infoboxer/tree/nodes.rb', line 139

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.



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

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



96
97
98
99
100
101
102
103
# File 'lib/infoboxer/tree/nodes.rb', line 96

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



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/infoboxer/tree/nodes.rb', line 179

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 41

#grep_v(pattern) ⇒ Object

Just like Array#grep_v, but returns Nodes



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

#group_byObject

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



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

def group_by
  super.transform_values { |group| Nodes[*group] }
end

#inspectObject



148
149
150
151
152
153
154
155
156
157
# File 'lib/infoboxer/tree/nodes.rb', line 148

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.



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

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



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

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 113

#prev_siblingsObject

Previous siblings (flat list) of all nodes inside.



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

#reject(&block) ⇒ Object

Just like Array#reject, but returns Nodes



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

#select(&block) ⇒ Object Also known as: filter

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 116

#sort_by(&block) ⇒ Object

Just like Array#sort_by, but returns Nodes



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

#textObject Also known as: to_s

Just join of all Node#texts inside.



160
161
162
# File 'lib/infoboxer/tree/nodes.rb', line 160

def text
  map(&:text).join
end

#to_treeObject

Just join of all Node#to_tree strings inside.



144
145
146
# File 'lib/infoboxer/tree/nodes.rb', line 144

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

#unwrapObject



166
167
168
# File 'lib/infoboxer/tree/nodes.rb', line 166

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