Parent

Scrubyt::ResultIndexer

Selecting results based on indices

If the results is list-like (as opposed to a 'hard' result, like a price or a title), probably with a variable count of results (like tags, authors etc.), you may need just specific elements - like the last one, every third one, or at specific indices. In this case you should use the select_indices syntax.

Attributes

indices_to_extract[R]

Public Class Methods

new(*args) click to toggle source
# File lib/scrubyt/core/scraping/result_indexer.rb, line 12
def initialize(*args)
  select_indices(*args)
end

Public Instance Methods

select_indices_to_extract(ary) click to toggle source

Perform selection of the desires result instances, based on their indices

# File lib/scrubyt/core/scraping/result_indexer.rb, line 18
def select_indices_to_extract(ary)
  return ary if @indices_to_extract == nil
  to_keep = []
  @indices_to_extract.each {|e|
    if e.is_a? Symbol
      case e
      when :first
        to_keep << 0
      when :last
        to_keep << ary.size-1
      when :all_but_last
       (0..ary.size-2).each {|i| to_keep << i} 
      when :all_but_first
       (1..ary.size-1).each {|i| to_keep << i} 
      when :every_even
       (0..ary.size).each {|i| to_keep << i if (i % 2 == 1)}
      when :every_odd
       (0..ary.size).each {|i| to_keep << i if (i % 2 == 0)}
      when :every_second
       (0..ary.size).each {|i| to_keep << i if (i % 2 == 0)}
      when :every_third
       (0..ary.size).each {|i| to_keep << i if (i % 3 == 0)}
      when :every_fourth
       (0..ary.size).each {|i| to_keep << i if (i % 4 == 0)}
      end
    end
  }
  @indices_to_extract.each {|i| to_keep << i if !i.is_a? Symbol}
  to_keep.sort!
  ary.reject! {|e| !to_keep.include? ary.index(e)}
  ary
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.