Class: Balanced::Pager

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

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(uri, 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



17
18
19
20
21
22
# File 'lib/balanced/pager.rb', line 17

def initialize uri, options = {}
  @uri = uri
  @options = options
  @page = nil
  @resource_class = nil
end

Instance Method Details

#all(options = {}) ⇒ Object



135
136
137
# File 'lib/balanced/pager.rb', line 135

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

#current_pageObject



57
58
59
# File 'lib/balanced/pager.rb', line 57

def current_page
  (offset / limit) + 1
end

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

Returns Iterates through the current page of records.

Yields:

  • (record)

Returns:

  • (Array)

    Iterates through the current page of records.



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/balanced/pager.rb', line 69

def each
  return enum_for :each unless block_given?

  load! unless @page
  loop do
    @page[:items].each do |r|
      yield resource_class.construct_from_response r
    end
    raise StopIteration if @page[:next_uri].nil?
    self.next
  end
end

#find(uri) ⇒ Object



139
140
141
142
143
144
145
146
# File 'lib/balanced/pager.rb', line 139

def find uri
  if resource_class.respond_to? :find
    raise NoMethodError,
        "#find must be called on #{resource_class} directly"
  end

  resource_class.find uri
end

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

Yields:

  • (record)

Returns:

  • (nil)

See Also:

  • Resource.find_each


85
86
87
88
89
90
# File 'lib/balanced/pager.rb', line 85

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

#firstObject



30
31
32
33
# File 'lib/balanced/pager.rb', line 30

def first
  load! unless @page
  items.first.nil? ? nil : resource_class.construct_from_response(items.first)
end

#itemsObject



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

def items
  load! unless @page
  @page[:items]
end

#limitObject Also known as: limit_value



40
41
42
43
# File 'lib/balanced/pager.rb', line 40

def limit
  load! unless @page
  @page[:limit]
end

#load!Array? Also known as: reload

Returns Load (or reload) the pager’s collection from the original, supplied options.

Returns:

  • (Array, nil)

    Load (or reload) the pager’s collection from the original, supplied options.



118
119
120
# File 'lib/balanced/pager.rb', line 118

def load!
  load_from @uri, @options
end

#nextArray?

Returns Refreshes the pager’s collection of records with the next page.

Returns:

  • (Array, nil)

    Refreshes the pager’s collection of records with the next page.



94
95
96
97
98
# File 'lib/balanced/pager.rb', line 94

def next
  load! unless @page
  next_uri = @page[:next_uri]
  load_from next_uri, nil unless next_uri.nil?
end

#num_pagesObject



61
62
63
64
65
# File 'lib/balanced/pager.rb', line 61

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

#offsetObject Also known as: offset_value



46
47
48
49
# File 'lib/balanced/pager.rb', line 46

def offset
  load! unless @page
  @page[:offset]
end

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

Returns Duplicates the pager, updating it with the options supplied. Useful for resource scopes.

Returns:

  • (Pager)

    Duplicates the pager, updating it with the options supplied. Useful for resource scopes.

See Also:



126
127
128
129
130
131
# File 'lib/balanced/pager.rb', line 126

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

#prevArray?

Returns Refreshes the pager’s collection of records with the previous page.

Returns:

  • (Array, nil)

    Refreshes the pager’s collection of records with the previous page.



102
103
104
105
106
# File 'lib/balanced/pager.rb', line 102

def prev
  load! unless @page
  prev_uri = @page[:prev_uri]
  load_from prev_uri, nil unless prev_uri.nil?
end

#resource_classObject



24
25
26
27
28
# File 'lib/balanced/pager.rb', line 24

def resource_class
  return @resource_class unless @resource_class.nil?
  load! unless @page
  @resource_class = Balanced.from_uri items.first[:uri]
end

#startArray?

Returns Refreshes the pager’s collection of records with the first page.

Returns:

  • (Array, nil)

    Refreshes the pager’s collection of records with the first page.



110
111
112
113
114
# File 'lib/balanced/pager.rb', line 110

def start
  load! unless @page
  first_page = @page[:first_page]
  load_from first_page, nil unless first_page.nil?
end

#totalObject



35
36
37
38
# File 'lib/balanced/pager.rb', line 35

def total
  load! unless @page
  @page[:total]
end