Class: ComicVine::CVObjectList

Inherits:
CVList
  • Object
show all
Defined in:
lib/comic_vine/cv_list.rb

Overview

A paginated list of resources (e.g. from ComicVine::API.characters). #next_page and #prev_page update the list in place, preserving the original options (:filter, :sort, :field_list, ...).

Instance Attribute Summary collapse

Attributes inherited from CVList

#cvos, #limit, #offset, #page_count, #total_count

Instance Method Summary collapse

Methods inherited from CVList

#each, #last, #page

Constructor Details

#initialize(resp, resc, opts = {}) ⇒ CVObjectList

Returns a new instance of CVObjectList.

Parameters:

  • resp (Hash)

    a list response from the API

  • resc (String)

    the plural resource name

  • opts (Hash) (defaults to: {})

    the options the list was fetched with



66
67
68
69
70
71
72
# File 'lib/comic_vine/cv_list.rb', line 66

def initialize(resp, resc, opts = {})
  super(resp)

  @resource = resc
  @opts = opts || {}
  @cvos = resp['results'].map { |r| ComicVine::CVObject.new(r) }
end

Instance Attribute Details

#resourceString (readonly)

Returns the plural resource name this list was fetched from.

Returns:

  • (String)

    the plural resource name this list was fetched from



61
62
63
# File 'lib/comic_vine/cv_list.rb', line 61

def resource
  @resource
end

Instance Method Details

#next_pageCVObjectList?

Advances to the next page, updating the list in place.

Returns:

  • (CVObjectList, nil)

    self, or nil if already on the last page

Raises:



78
79
80
81
82
83
# File 'lib/comic_vine/cv_list.rb', line 78

def next_page
  return nil if (@offset + @page_count) >= @total_count

  update_ivals(ComicVine::API.send(@resource, @opts.merge(:limit => @limit, :offset => (@offset + @limit))))
  self
end

#prev_pageCVObjectList?

Steps back to the previous page, updating the list in place.

Returns:

  • (CVObjectList, nil)

    self, or nil if already on the first page

Raises:



89
90
91
92
93
94
# File 'lib/comic_vine/cv_list.rb', line 89

def prev_page
  return nil if @offset == 0

  update_ivals(ComicVine::API.send(@resource, @opts.merge(:limit => @limit, :offset => [@offset - @limit, 0].max)))
  self
end