class GScraper::Page

Public Class Methods

new(elements=[]) { |self| ... } click to toggle source

Creates a new Page object.

@param [Array] elements

The elements to populate the page with.

@yield [page]

If a block is given, it will be passed the newly created page.

@yieldparam [Page] page

The newly created page.
Calls superclass method
# File lib/gscraper/page.rb, line 36
def initialize(elements=[])
  super(elements)

  yield self if block_given?
end

Public Instance Methods

map() { |element| ... } click to toggle source

Maps the elements within the page.

@yield [element]

The given block will be passed each element in the page.

@return [Array, Enumerator]

The mapped result. If no block was given, an Enumerator object will
be returned.

@example

page.map
# => Page

@example

page.map { |element| element.field }
# => [...]
# File lib/gscraper/page.rb, line 60
def map
  return enum_for(:map) unless block_given?

  mapped = []

  each { |element| mapped << yield(element) }
  return mapped
end
select(&block) click to toggle source

Selects the elements within the page.

@yield [element]

The given block will be passed each element in the page.

@return [Array, Enumerator]

The selected elements. If no block was given, an Enumerator object
is returned.

@example

page.select { |element| element.field =~ /ruby/i }
Calls superclass method
# File lib/gscraper/page.rb, line 82
def select(&block)
  unless block
    enum_for(:select)
  else
    self.class.new(super(&block))
  end
end