lib/infoboxer/tree/nodes.rb in infoboxer-0.2.7 vs lib/infoboxer/tree/nodes.rb in infoboxer-0.2.8

- old
+ new

@@ -1,6 +1,7 @@ # encoding: utf-8 + module Infoboxer module Tree # List of nodes, which tries to be useful both as array, and as proxy # to its contents. # @@ -8,11 +9,11 @@ # `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: # # ```ruby - # document.sections. # => Nodes returned, + # 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 @@ -20,11 +21,10 @@ # fetch('leader_name1'). # ...including those which only some node types support # map(&:text) # ...and still have full-functioning Array # ``` # class Nodes < Array - # @!method select(&block) # Just like Array#select, but returns Nodes # @!method reject(&block) # Just like Array#reject, but returns Nodes @@ -39,14 +39,14 @@ # Just like Array#compact, but returns Nodes # @!method -(other) # Just like Array#-, but returns Nodes - [:select, :reject, :sort_by, :flatten, :compact, :-].each do |sym| - define_method(sym){|*args, &block| + %i[select reject sort_by flatten compact -].each do |sym| + define_method(sym) do |*args, &block| Nodes[*super(*args, &block)] - } + end end # Just like Array#first, but returns Nodes, if provided with `n` of elements. def first(n = nil) if n.nil? @@ -66,60 +66,61 @@ end # Just like Array#map, but returns Nodes, **if** all map results are Node def map res = super - if res.all?{|n| n.is_a?(Node) || n.is_a?(Nodes)} + if res.all? { |n| n.is_a?(Node) || n.is_a?(Nodes) } Nodes[*res] else res end end # @!method prev_siblings # Previous siblings (flat list) of all nodes inside. - + # @!method next_siblings # Next siblings (flat list) of all nodes inside. # @!method siblings # Siblings (flat list) of all nodes inside. # @!method fetch # Fetches by name(s) variables for all templates inside. # # See {Tree::Template#fetch} for explanation. - - [ - :prev_siblings, :next_siblings, :siblings, - :fetch + + %i[ + prev_siblings next_siblings siblings + fetch ].each do |sym| - define_method(sym){|*args| - make_nodes map{|n| n.send(sym, *args)} - } + define_method(sym) do |*args| + make_nodes(map { |n| n.send(sym, *args) }) + end end # By list of variable names, fetches hashes of `{name => value}` # from all templates inside. # # See {Tree::Template#fetch_hash} for explanation. # # @return [Array<Hash>] def fetch_hashes(*args) - map{|t| t.fetch_hash(*args)} + map { |t| t.fetch_hash(*args) } end # Just join of all {Node#to_tree Node#to_tree} strings inside. def to_tree map(&:to_tree).join("\n") end def inspect - '[' + + '[' + case when count > MAX_CHILDREN - self[0...MAX_CHILDREN].map(&:inspect).join(', ') + ", ...#{count - MAX_CHILDREN} more nodes" + self[0...MAX_CHILDREN].map(&:inspect).join(', ') + + ", ...#{count - MAX_CHILDREN} more nodes" else map(&:inspect).join(', ') end + ']' end @@ -136,23 +137,23 @@ # 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 = select{|n| n.respond_to?(:link)}.map(&:link) + 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") + fail('Not in a page from real source') + page.client or fail('MediaWiki client not set') page.client.get(*links) end # @private # Internal, used by {Parser} def <<(node) - if node.kind_of?(Array) - node.each{|n| self << n} + if node.is_a?(Array) + node.each { |n| self << n } elsif last && last.can_merge?(node) last.merge!(node) else return if !node || node.empty? node = Text.new(node) if node.is_a?(String) @@ -170,10 +171,10 @@ end # @private # Internal, used by {Parser} def flow_templates - make_nodes map{|n| n.is_a?(Paragraph) ? n.to_templates? : n} + make_nodes(map { |n| n.is_a?(Paragraph) ? n.to_templates? : n }) end private # @private For inspect shortening