Class: Balanced::Pager
- Inherits:
-
Object
- Object
- Balanced::Pager
- Includes:
- Enumerable
- Defined in:
- lib/balanced/pager.rb
Constant Summary collapse
- DEFAULT_SEP =
/[&;] */n
- DEFAULT_LIMIT =
10
Instance Method Summary collapse
- #all(options = {}) ⇒ Object
- #current_page ⇒ Object
-
#each {|record| ... } ⇒ Array
Iterates through the current page of records.
- #find(uri) ⇒ Object
- #find_each {|record| ... } ⇒ nil
- #first ⇒ Object
-
#initialize(uri, options = {}) ⇒ Pager
constructor
A pager for paginating through resource records.
- #items ⇒ Object
- #limit ⇒ Object (also: #limit_value)
-
#load! ⇒ Array?
(also: #reload)
Load (or reload) the pager’s collection from the original, supplied options.
-
#next ⇒ Array?
Refreshes the pager’s collection of records with the next page.
- #num_pages ⇒ Object
- #offset ⇒ Object (also: #offset_value)
-
#paginate(options = {}) ⇒ Pager
(also: #scoped, #where)
Duplicates the pager, updating it with the options supplied.
-
#prev ⇒ Array?
Refreshes the pager’s collection of records with the previous page.
- #resource_class ⇒ Object
-
#start ⇒ Array?
Refreshes the pager’s collection of records with the first page.
- #total ⇒ Object
Constructor Details
#initialize(uri, options = {}) ⇒ Pager
A pager for paginating through resource records.
17 18 19 20 21 22 |
# File 'lib/balanced/pager.rb', line 17 def initialize uri, = {} @uri = uri @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 = {} paginate().to_a end |
#current_page ⇒ Object
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.
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
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 |
#first ⇒ Object
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 |
#items ⇒ Object
52 53 54 55 |
# File 'lib/balanced/pager.rb', line 52 def items load! unless @page @page[:items] end |
#limit ⇒ Object 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.
118 119 120 |
# File 'lib/balanced/pager.rb', line 118 def load! load_from @uri, @options end |
#next ⇒ Array?
Returns 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_pages ⇒ Object
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 |
#offset ⇒ Object 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.
126 127 128 129 130 131 |
# File 'lib/balanced/pager.rb', line 126 def paginate = {} dup.instance_eval { @page = nil @options.update and self } end |
#prev ⇒ Array?
Returns 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_class ⇒ Object
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 |
#start ⇒ Array?
Returns 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 |
#total ⇒ Object
35 36 37 38 |
# File 'lib/balanced/pager.rb', line 35 def total load! unless @page @page[:total] end |