Class: Balanced::Pager

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/balanced_ach/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.



11
12
13
14
15
16
# File 'lib/balanced_ach/pager.rb', line 11

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

Instance Method Details

#all(options = {}) ⇒ Object



129
130
131
# File 'lib/balanced_ach/pager.rb', line 129

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

#current_pageObject



51
52
53
# File 'lib/balanced_ach/pager.rb', line 51

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.



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/balanced_ach/pager.rb', line 63

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



133
134
135
136
137
138
139
140
# File 'lib/balanced_ach/pager.rb', line 133

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


79
80
81
82
83
84
# File 'lib/balanced_ach/pager.rb', line 79

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

#firstObject



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

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

#itemsObject



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

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

#limitObject Also known as: limit_value



34
35
36
37
# File 'lib/balanced_ach/pager.rb', line 34

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.



112
113
114
# File 'lib/balanced_ach/pager.rb', line 112

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.



88
89
90
91
92
# File 'lib/balanced_ach/pager.rb', line 88

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

#num_pagesObject



55
56
57
58
59
# File 'lib/balanced_ach/pager.rb', line 55

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

#offsetObject Also known as: offset_value



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

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:



120
121
122
123
124
125
# File 'lib/balanced_ach/pager.rb', line 120

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.



96
97
98
99
100
# File 'lib/balanced_ach/pager.rb', line 96

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

#resource_classObject



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

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.



104
105
106
107
108
# File 'lib/balanced_ach/pager.rb', line 104

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

#totalObject



29
30
31
32
# File 'lib/balanced_ach/pager.rb', line 29

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