Class: GScraper::Page

Inherits:
Array
  • Object
show all
Defined in:
lib/gscraper/page.rb

Direct Known Subclasses

Search::Page

Instance Method Summary collapse

Constructor Details

#initialize(elements = []) {|page| ... } ⇒ Page

Creates a new Page object.

Parameters:

  • elements (Array) (defaults to: [])

    The elements to populate the page with.

Yields:

  • (page)

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

Yield Parameters:

  • page (Page)

    The newly created page.



36
37
38
39
40
# File 'lib/gscraper/page.rb', line 36

def initialize(elements=[])
  super(elements)

  yield self if block_given?
end

Instance Method Details

#map {|element| ... } ⇒ Array, Enumerator

Maps the elements within the page.

Examples:

page.map
# => Page
page.map { |element| element.field }
# => [...]

Yields:

  • (element)

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

Returns:

  • (Array, Enumerator)

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



60
61
62
63
64
65
66
67
# 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 {|element| ... } ⇒ Array, Enumerator

Selects the elements within the page.

Examples:

page.select { |element| element.field =~ /ruby/i }

Yields:

  • (element)

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

Returns:

  • (Array, Enumerator)

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



82
83
84
85
86
87
88
# File 'lib/gscraper/page.rb', line 82

def select(&block)
  unless block
    enum_for(:select)
  else
    self.class.new(super(&block))
  end
end