Class: Balanced::Pager
- Inherits:
-
Object
- Object
- Balanced::Pager
- Includes:
- Enumerable
- Defined in:
- lib/balanced_ach/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.
11 12 13 14 15 16 |
# File 'lib/balanced_ach/pager.rb', line 11 def initialize uri, = {} @uri = uri @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 = {} paginate().to_a end |
#current_page ⇒ Object
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.
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
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 |
#first ⇒ Object
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 |
#items ⇒ Object
46 47 48 49 |
# File 'lib/balanced_ach/pager.rb', line 46 def items load! unless @page @page[:items] end |
#limit ⇒ Object 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.
112 113 114 |
# File 'lib/balanced_ach/pager.rb', line 112 def load! load_from @uri, @options end |
#next ⇒ Array?
Returns 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_pages ⇒ Object
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 |
#offset ⇒ Object 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.
120 121 122 123 124 125 |
# File 'lib/balanced_ach/pager.rb', line 120 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.
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_class ⇒ Object
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 |
#start ⇒ Array?
Returns 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 |
#total ⇒ Object
29 30 31 32 |
# File 'lib/balanced_ach/pager.rb', line 29 def total load! unless @page @page[:total] end |