Class: Infoboxer::Tree::Nodes
- Inherits:
-
Array
- Object
- Array
- Infoboxer::Tree::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
-
#-(other) ⇒ Object
Just like Array#-, but returns Nodes.
-
#compact ⇒ Object
Just like Array#compact, but returns Nodes.
-
#fetch ⇒ Object
Fetches by name(s) variables for all templates inside.
-
#fetch_hashes(*args) ⇒ Array<Hash>
By list of variable names, fetches hashes of
{name => value}
from all templates inside. -
#first(n = nil) ⇒ Object
Just like Array#first, but returns Nodes, if provided with
n
of elements. -
#flatten ⇒ Object
Just like Array#flatten, but returns Nodes.
-
#follow ⇒ Nodes<MediaWiki::Page>
Fetches pages by ALL wikilinks inside in ONE query to MediaWiki API.
- #inspect ⇒ Object
-
#last(n = nil) ⇒ Object
Just like Array#last, but returns Nodes, if provided with
n
of elements. -
#map ⇒ Object
Just like Array#map, but returns Nodes, if all map results are Node.
-
#next_siblings ⇒ Object
Next siblings (flat list) of all nodes inside.
-
#prev_siblings ⇒ Object
Previous siblings (flat list) of all nodes inside.
-
#reject(&block) ⇒ Object
Just like Array#reject, but returns Nodes.
-
#select(&block) ⇒ Object
Just like Array#select, but returns Nodes.
-
#siblings ⇒ Object
Siblings (flat list) of all nodes inside.
-
#sort_by(&block) ⇒ Object
Just like Array#sort_by, but returns Nodes.
-
#text ⇒ Object
Just join of all Node#texts inside.
-
#to_tree ⇒ Object
Just join of all Node#to_tree strings inside.
Methods included from Navigation::Sections::Nodes
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 |
#compact ⇒ Object
Just like Array#compact, but returns Nodes
|
# File 'lib/infoboxer/tree/nodes.rb', line 38
|
#fetch ⇒ Object
Fetches by name(s) variables for all templates inside.
See Templates::Base#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 Templates::Base#fetch_hash for explanation.
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 |
#flatten ⇒ Object
Just like Array#flatten, but returns Nodes
|
# File 'lib/infoboxer/tree/nodes.rb', line 35
|
#follow ⇒ Nodes<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.
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 |
#inspect ⇒ Object
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 |
#map ⇒ Object
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_siblings ⇒ Object
Next siblings (flat list) of all nodes inside.
|
# File 'lib/infoboxer/tree/nodes.rb', line 81
|
#prev_siblings ⇒ Object
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
|
#siblings ⇒ Object
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
|
#text ⇒ Object
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_tree ⇒ Object
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 |