Class: ElvantoAPI::Pager
- Inherits:
-
Object
- Object
- ElvantoAPI::Pager
- Includes:
- Enumerable
- Defined in:
- lib/elvanto/pager.rb
Constant Summary collapse
- DEFAULT_SEP =
/[&;] */n
- DEFAULT_LIMIT =
10
Instance Attribute Summary collapse
-
#href ⇒ Object
Returns the value of attribute href.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #all(options = {}) ⇒ Object
- #current_page ⇒ Object
-
#each {|record| ... } ⇒ Array
Iterates through the current page of records.
- #fetch_each {|record| ... } ⇒ nil
- #first ⇒ Object
- #first_page? ⇒ Boolean
-
#initialize(href, options = {}) ⇒ Pager
constructor
A pager for paginating through resource records.
- #items ⇒ Object
- #last_page? ⇒ Boolean
- #limit ⇒ Object (also: #limit_value)
-
#load! ⇒ Array?
(also: #reload)
original, supplied options.
-
#next ⇒ Array?
the next page.
- #num_pages ⇒ Object
-
#paginate(options = {}) ⇒ Pager
(also: #scoped, #where)
supplied.
-
#prev ⇒ Array?
the previous page.
- #resource_class ⇒ Object
-
#start ⇒ Array?
the first page.
- #total ⇒ Object
Constructor Details
#initialize(href, options = {}) ⇒ Pager
A pager for paginating through resource records.
20 21 22 23 24 25 |
# File 'lib/elvanto/pager.rb', line 20 def initialize(href, = {}) @href = href @options = @page = nil @resource_class = nil end |
Instance Attribute Details
#href ⇒ Object
Returns the value of attribute href.
10 11 12 |
# File 'lib/elvanto/pager.rb', line 10 def href @href end |
#options ⇒ Object
Returns the value of attribute options.
11 12 13 |
# File 'lib/elvanto/pager.rb', line 11 def @options end |
Instance Method Details
#all(options = {}) ⇒ Object
158 159 160 |
# File 'lib/elvanto/pager.rb', line 158 def all( = {}) paginate().to_a end |
#current_page ⇒ Object
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.
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
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 |
#first ⇒ Object
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
100 101 102 |
# File 'lib/elvanto/pager.rb', line 100 def first_page? current_page == 1 end |
#items ⇒ Object
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
96 97 98 |
# File 'lib/elvanto/pager.rb', line 96 def last_page? current_page == num_pages end |
#limit ⇒ Object 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.
141 142 143 |
# File 'lib/elvanto/pager.rb', line 141 def load! load_from @href, @options end |
#next ⇒ Array?
the next page.
116 117 118 119 120 121 |
# File 'lib/elvanto/pager.rb', line 116 def next load! unless @page = @options.merge({page: current_page + 1}) load_from @href, unless last_page? end |
#num_pages ⇒ Object
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.
149 150 151 152 153 154 |
# File 'lib/elvanto/pager.rb', line 149 def paginate( = {}) dup.instance_eval { @page = nil @options.update and self } end |
#prev ⇒ Array?
the previous page.
125 126 127 128 129 |
# File 'lib/elvanto/pager.rb', line 125 def prev load! unless @page = @options.merge({page: current_page - 1}) load_from @href, unless first_page? end |
#resource_class ⇒ Object
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 |
#start ⇒ Array?
the first page.
133 134 135 136 137 |
# File 'lib/elvanto/pager.rb', line 133 def start load! unless @page = @options.merge({page: 1}) load_from @href, end |
#total ⇒ Object
47 48 49 50 |
# File 'lib/elvanto/pager.rb', line 47 def total load! unless @page @page[@resource_class.collection_name][:total] end |