lib/finix/pagination.rb in finix-0.16 vs lib/finix/pagination.rb in finix-1.0.0
- old
+ new
@@ -30,21 +30,23 @@
self
end
def each
return enum_for :each unless block_given?
- fetch_first
+ fetch :first
loop do
items.each { |item| yield item }
fetch :next
end
end
def count(*args)
refresh # always refresh to get last items
- return super *args if block_given?
- self.send :method_missing, :count, args
+
+ cnt = (block_given?) ? super(*args) : self.send(:method_missing, :count, args)
+ cnt = 0 if cnt.nil?
+ cnt
end
def fetch(scope = nil) # :next, :last, :first, :prev, :self
opts = {}
if scope.is_a? Hash
@@ -52,12 +54,22 @@
scope = nil
end
scope = :self if scope.nil?
scope = scope.to_s.to_sym unless scope.nil?
- if @hyperlinks[scope]
- load_from @hyperlinks[scope], opts
+
+ href = @hyperlinks[scope]
+ unless href
+ if scope == :first
+ @attributes['page']['offset'] = 0 unless @attributes['page']['offset'].nil?
+ href = @hyperlinks[:self]
+ href = href[/[^\?]+/]
+ end
+ end
+
+ if href
+ load_from href, opts
return self.items
end
raise StopIteration
end
@@ -67,10 +79,13 @@
def refresh
fetch
self
end
+ alias load! refresh
+ alias retrieve refresh
+
def create(attrs={})
attrs = attrs.attributes if attrs.is_a?(Resource)
attrs = Finix::Utils.indifferent_read_access attrs
href = @hyperlinks[:self]
@@ -78,17 +93,51 @@
attrs[:href] = href
@resource_class.new(attrs).save
end
- private
+ def next_page
+ fetch :next
+ self
+ end
- def fetch_first
- @attributes['page']['offset'] = 0 unless @attributes['page']['offset'].nil?
- fetch(@hyperlinks[:first] ? :first : nil)
+ def previous_page
+ fetch :prev
+ self
end
+ def last_page
+ fetch :last
+ self
+ end
+
+ def first_page
+ fetch :first
+ self
+ end
+
+ def total
+ # refresh unless loaded
+ self.page.count
+ end
+
+ def num_pages
+ num = total / limit
+ num += 1 if total % limit > 0
+ num
+ end
+
+ def loaded
+ not self.items.nil?
+ end
+
+ private
+
+ # def current_page
+ # (offset / limit) + 1
+ # end
+
def extract_opts(opts={})
opts = Finix::Utils.indifferent_read_access opts
limit = opts.delete('limit')
offset = opts.delete('offset')
@attributes['page'] = @attributes['page'] || {}
@@ -123,24 +172,9 @@
params.merge! opts unless opts.empty?
params.delete('count') # remove count from previous query
response = Finix.get parsed_url.to_s, params
load_page_from_response! response
- # body = Finix::Utils.indifferent_read_access response.body
- #
- # @hyperlinks = Finix::Utils.eval_class(self, IndifferentHash).new
- # links = body.delete('_links')
- # links.each { |key, link| @hyperlinks[key.to_sym] = link[:href] }
- #
- # @attributes = {'items' => [], 'page' => body.delete('page')} # clear attributes
- # if body.has_key? '_embedded'
- # resource_name, resources = body.delete('_embedded').first
- # @resource_class = Finix.from_hypermedia_registry resource_name
- # @attributes['items'] = resources.map do |attrs|
- # cls = Finix.from_hypermedia_registry resource_name, attrs
- # cls.construct_from_response attrs
- # end
- # end
end
# Stolen from Mongrel, with some small modifications:
# Parses a query string by breaking it up at the '&'
# and ';' characters. You can also use this to parse