Class: ElvantoAPI::Pager

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/elvanto/pager.rb

Constant Summary collapse

DEFAULT_SEP =
/[&;] */n
DEFAULT_LIMIT =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(href, options = {}) ⇒ Pager

A pager for paginating through resource records.

Parameters:

  • uri (String)

    the uri of the resource

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

Options Hash (options):

  • limit (Integer)
  • offset (Integer)
  • per (Integer)

    an alias for the :limit option



20
21
22
23
24
25
# File 'lib/elvanto/pager.rb', line 20

def initialize(href, options = {})
  @href = href
  @options = options
  @page = nil
  @resource_class = nil
end

Instance Attribute Details

#hrefObject

Returns the value of attribute href.



10
11
12
# File 'lib/elvanto/pager.rb', line 10

def href
  @href
end

#optionsObject

Returns the value of attribute options.



11
12
13
# File 'lib/elvanto/pager.rb', line 11

def options
  @options
end

Instance Method Details

#all(options = {}) ⇒ Object



158
159
160
# File 'lib/elvanto/pager.rb', line 158

def all(options = {})
  paginate(options).to_a
end

#current_pageObject



68
69
70
# File 'lib/elvanto/pager.rb', line 68

def current_page
  @page[@resource_class.collection_name][:page]
end

#each {|record| ... } ⇒ Array

Returns Iterates through the current page of records.

Yields:

  • (record)

Returns:

  • (Array)

    Iterates through the current page of records.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/elvanto/pager.rb', line 80

def each
  return enum_for :each unless block_given?

  load! unless @page
  loop do
    items.each do |r|
      envelope = {
        @resource_class.member_name.to_sym => [r]
      }
      yield resource_class.construct_from_response(envelope)
    end
    raise StopIteration if last_page?
    self.next
  end
end

#fetch_each {|record| ... } ⇒ nil

Yields:

  • (record)

Returns:

  • (nil)

See Also:

  • Resource.fetch_each


107
108
109
110
111
112
# File 'lib/elvanto/pager.rb', line 107

def fetch_each
  return enum_for :fetch_each unless block_given?
  begin
    each { |record| yield record }
  end while self.next
end

#firstObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/elvanto/pager.rb', line 33

def first
  load! unless @page
  if items.first.nil?
    nil
  else
    envelope = {
      :meta => @page[:meta],
      :links => @page[:links],
      @resource_class.collection_name.to_sym => [items.first]
    }
    resource_class.construct_from_response(envelope)
  end
end

#first_page?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/elvanto/pager.rb', line 100

def first_page?
  current_page == 1
end

#itemsObject



59
60
61
62
63
64
65
66
# File 'lib/elvanto/pager.rb', line 59

def items
  load! unless @page
  if @resource_class.nil?
    []
  else
    @page[@resource_class.collection_name][@resource_class.member_name]
  end
end

#last_page?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/elvanto/pager.rb', line 96

def last_page?
  current_page == num_pages
end

#limitObject Also known as: limit_value



52
53
54
55
# File 'lib/elvanto/pager.rb', line 52

def limit
  load! unless @page
  @page[@resource_class.collection_name][:per_page]
end

#load!Array? Also known as: reload

original, supplied options.

Returns:

  • (Array, nil)

    Load (or reload) the pager’s collection from the



141
142
143
# File 'lib/elvanto/pager.rb', line 141

def load!
  load_from @href, @options
end

#nextArray?

the next page.

Returns:

  • (Array, nil)

    Refreshes the pager’s collection of records with



116
117
118
119
120
121
# File 'lib/elvanto/pager.rb', line 116

def next
  load! unless @page

  new_options = @options.merge({page: current_page + 1})
  load_from @href, new_options unless last_page?
end

#num_pagesObject



72
73
74
75
76
# File 'lib/elvanto/pager.rb', line 72

def num_pages
  num = total / limit
  num += 1 if total % limit > 0
  num
end

#paginate(options = {}) ⇒ Pager Also known as: scoped, where

supplied. Useful for resource scopes.

Returns:

  • (Pager)

    Duplicates the pager, updating it with the options

See Also:



149
150
151
152
153
154
# File 'lib/elvanto/pager.rb', line 149

def paginate(options = {})
  dup.instance_eval {
    @page = nil
    @options.update options and self
  }
end

#prevArray?

the previous page.

Returns:

  • (Array, nil)

    Refreshes the pager’s collection of records with



125
126
127
128
129
# File 'lib/elvanto/pager.rb', line 125

def prev
  load! unless @page
  new_options = @options.merge({page: current_page - 1})
  load_from @href, new_options unless first_page?
end

#resource_classObject



27
28
29
30
31
# File 'lib/elvanto/pager.rb', line 27

def resource_class
  return @resource_class unless @resource_class.nil?
  load! unless @page
  @resource_class
end

#startArray?

the first page.

Returns:

  • (Array, nil)

    Refreshes the pager’s collection of records with



133
134
135
136
137
# File 'lib/elvanto/pager.rb', line 133

def start
  load! unless @page
  new_options = @options.merge({page: 1})
  load_from @href, new_options
end

#totalObject



47
48
49
50
# File 'lib/elvanto/pager.rb', line 47

def total
  load! unless @page
  @page[@resource_class.collection_name][:total]
end