lib/infoboxer/tree/nodes.rb in infoboxer-0.3.0.pre vs lib/infoboxer/tree/nodes.rb in infoboxer-0.3.0

- old
+ new

@@ -36,23 +36,14 @@ # Just like Array#flatten, but returns Nodes # @!method compact # Just like Array#compact, but returns Nodes - # @!method grep(pattern) - # Just like Array#grep, but returns Nodes - - # @!method grep_v(pattern) - # Just like Array#grep_v, but returns Nodes - # @!method -(other) # Just like Array#-, but returns Nodes - # @!method +(other) - # Just like Array#+, but returns Nodes - - %i[select reject sort_by flatten compact grep grep_v - +].each do |sym| + %i[select reject sort_by flatten compact -].each do |sym| define_method(sym) do |*args, &block| Nodes[*super(*args, &block)] end end @@ -82,25 +73,10 @@ else res end end - # Just like Array#flat_map, but returns Nodes, **if** all map results are Node - def flat_map - res = super - if res.all? { |n| n.is_a?(Node) || n.is_a?(Nodes) } - Nodes[*res] - else - res - end - end - - # Just like Array#group, but returns hash with `{<grouping variable> => Nodes}` - def group_by - super.map { |title, group| [title, Nodes[*group]] }.to_h - end - # @!method prev_siblings # Previous siblings (flat list) of all nodes inside. # @!method next_siblings # Next siblings (flat list) of all nodes inside. @@ -161,18 +137,16 @@ # It will be fixed in next releases. # # @return [Nodes<MediaWiki::Page>] It is still `Nodes`, so you # still can process them uniformely. def follow - links = grep(Linkable) + 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') - pages = links.group_by(&:interwiki) - .flat_map { |iw, ls| page.client.get(*ls.map(&:link), interwiki: iw) } - pages.count == 1 ? pages.first : Nodes[*pages] + page.client.get(*links) end # @private # Internal, used by {Parser} def <<(node) @@ -197,12 +171,10 @@ end # @private # Internal, used by {Parser} def flow_templates - # TODO: will it be better?.. - # make_nodes(map { |n| n.is_a?(Paragraph) ? n.to_templates? : n }) - self + make_nodes(map { |n| n.is_a?(Paragraph) ? n.to_templates? : n }) end private # @private For inspect shortening