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 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
- #create(options = {}) ⇒ Object
- #current_page ⇒ Object
-
#each {|record| ... } ⇒ Array
Iterates through the current page of records.
- #fetch(uri) ⇒ Object
- #fetch_each {|record| ... } ⇒ nil
- #first ⇒ Object
-
#initialize(href, 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(href, options = {}) ⇒ Pager
A pager for paginating through resource records.
20 21 22 23 24 25 |
# File 'lib/balanced/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/balanced/pager.rb', line 10 def href @href end |
#options ⇒ Object
Returns the value of attribute options.
11 12 13 |
# File 'lib/balanced/pager.rb', line 11 def @options end |
Instance Method Details
#all(options = {}) ⇒ Object
156 157 158 |
# File 'lib/balanced/pager.rb', line 156 def all( = {}) paginate().to_a end |
#create(options = {}) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/balanced/pager.rb', line 169 def create(={}) opts = Balanced::Utils.indifferent_read_access opts[:href] = @href # if we don't have a media type for the href, # let's try to inspect the url to look it up from the # registry if @resource_class.nil? @resource_class = Balanced.from_href(@href) end @resource_class.new(opts).save end |
#current_page ⇒ Object
73 74 75 |
# File 'lib/balanced/pager.rb', line 73 def current_page (offset / limit) + 1 end |
#each {|record| ... } ⇒ Array
Returns Iterates through the current page of records.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/balanced/pager.rb', line 85 def each return enum_for :each unless block_given? load! unless @page loop do items.each do |r| envelope = { :meta => @page[:meta], :links => @page[:links], @resource_class.collection_name.to_sym => [r] } yield resource_class.construct_from_response(envelope) end raise StopIteration if @page[:meta][:next].nil? self.next end end |
#fetch(uri) ⇒ Object
160 161 162 163 164 165 166 167 |
# File 'lib/balanced/pager.rb', line 160 def fetch(uri) if resource_class.respond_to? :find raise NoMethodError, "#find must be called on #{resource_class} directly" end resource_class.fetch uri end |
#fetch_each {|record| ... } ⇒ nil
106 107 108 109 110 111 |
# File 'lib/balanced/pager.rb', line 106 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/balanced/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 |
#items ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/balanced/pager.rb', line 64 def items load! unless @page if @resource_class.nil? [] else @page[@resource_class.collection_name] end end |
#limit ⇒ Object Also known as: limit_value
52 53 54 55 |
# File 'lib/balanced/pager.rb', line 52 def limit load! unless @page @page[:meta][:limit] end |
#load! ⇒ Array? Also known as: reload
Returns Load (or reload) the pager’s collection from the original, supplied options.
139 140 141 |
# File 'lib/balanced/pager.rb', line 139 def load! load_from @href, @options end |
#next ⇒ Array?
Returns Refreshes the pager’s collection of records with the next page.
115 116 117 118 119 |
# File 'lib/balanced/pager.rb', line 115 def next load! unless @page next_uri = @page[:meta][:next] load_from next_uri, nil unless next_uri.nil? end |
#num_pages ⇒ Object
77 78 79 80 81 |
# File 'lib/balanced/pager.rb', line 77 def num_pages num = total / limit num += 1 if total % limit > 0 num end |
#offset ⇒ Object Also known as: offset_value
58 59 60 61 |
# File 'lib/balanced/pager.rb', line 58 def offset load! unless @page @page[:meta][:offset] end |
#paginate(options = {}) ⇒ Pager Also known as: scoped, where
Returns Duplicates the pager, updating it with the options supplied. Useful for resource scopes.
147 148 149 150 151 152 |
# File 'lib/balanced/pager.rb', line 147 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.
123 124 125 126 127 |
# File 'lib/balanced/pager.rb', line 123 def prev load! unless @page prev_uri = @page[:meta][:prev] load_from prev_uri, nil unless prev_uri.nil? end |
#resource_class ⇒ Object
27 28 29 30 31 |
# File 'lib/balanced/pager.rb', line 27 def resource_class return @resource_class unless @resource_class.nil? load! unless @page @resource_class end |
#start ⇒ Array?
Returns Refreshes the pager’s collection of records with the first page.
131 132 133 134 135 |
# File 'lib/balanced/pager.rb', line 131 def start load! unless @page first_page = @page[:meta][:first] load_from first_page, nil unless first_page.nil? end |
#total ⇒ Object
47 48 49 50 |
# File 'lib/balanced/pager.rb', line 47 def total load! unless @page @page[:meta][:total] end |