class Finix::Pagination

Attributes

attributes[R]
resource_class[RW]

Public Class Methods

new(*args) click to toggle source
# File lib/finix/pagination.rb, line 16
def initialize(*args)
  opts = args.slice!(0) || {}
  href = opts.delete(:href)

  @hyperlinks = Finix::Utils.eval_class(self, IndifferentHash).new
  @hyperlinks[:self] = href
  @attributes = {}
  @resource_class = nil
  extract_opts opts
end

Public Instance Methods

count(*args) click to toggle source
Calls superclass method
# File lib/finix/pagination.rb, line 42
def count(*args)
  refresh # always refresh to get last items

  cnt = (block_given?) ? super(*args) : self.send(:method_missing, :count, args)
  cnt = 0 if cnt.nil?
  cnt
end
create(attrs={}) click to toggle source
# File lib/finix/pagination.rb, line 87
def create(attrs={})
  attrs = attrs.attributes if attrs.is_a?(Resource)
  attrs = Finix::Utils.indifferent_read_access attrs

  href = @hyperlinks[:self]
  @resource_class = Finix.from_hypermedia_registry href, attrs

  attrs[:href] = href
  @resource_class.new(attrs).save
end
each() { |item| ... } click to toggle source
# File lib/finix/pagination.rb, line 33
def each
  return enum_for :each unless block_given?
  fetch :first
  loop do
    items.each { |item| yield item }
    fetch :next
  end
end
fetch(scope = nil) click to toggle source
# File lib/finix/pagination.rb, line 50
def fetch(scope = nil) # :next, :last, :first, :prev, :self
  opts = {}
  if scope.is_a? Hash
    opts = Finix::Utils.indifferent_read_access scope
    scope = nil
  end

  scope = :self if scope.nil?
  scope = scope.to_s.to_sym unless scope.nil?

  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
Also aliased as: retrieve
first_page() click to toggle source
# File lib/finix/pagination.rb, line 113
def first_page
  fetch :first
  self
end
init!(*args) click to toggle source
# File lib/finix/pagination.rb, line 27
def init!(*args)
  opts = args.slice(0) || {}
  extract_opts opts
  self
end
last_page() click to toggle source
# File lib/finix/pagination.rb, line 108
def last_page
  fetch :last
  self
end
load!()
Alias for: refresh
loaded() click to toggle source
# File lib/finix/pagination.rb, line 129
def loaded
  not self.items.nil?
end
next_page() click to toggle source
# File lib/finix/pagination.rb, line 98
def next_page
  fetch :next
  self
end
num_pages() click to toggle source
# File lib/finix/pagination.rb, line 123
def num_pages
  num = total / limit
  num += 1 if total % limit > 0
  num
end
previous_page() click to toggle source
# File lib/finix/pagination.rb, line 103
def previous_page
  fetch :prev
  self
end
refresh() click to toggle source
# File lib/finix/pagination.rb, line 79
def refresh
  fetch
  self
end
Also aliased as: load!, retrieve
retrieve(scope = nil)
Alias for: fetch
total() click to toggle source
# File lib/finix/pagination.rb, line 118
def total
  # refresh unless loaded
  self.page.count
end

Private Instance Methods

extract_opts(opts={}) click to toggle source

def current_page

(offset / limit) + 1

end

# File lib/finix/pagination.rb, line 139
def extract_opts(opts={})
  opts = Finix::Utils.indifferent_read_access opts
  limit = opts.delete('limit')
  offset = opts.delete('offset')
  @attributes['page'] = @attributes['page'] || {}
  @attributes['page']['limit'] = limit unless limit.nil?
  @attributes['page']['offset'] = offset unless offset.nil?
  @attributes.merge! opts unless opts.empty?

  if not limit.nil? or not offset.nil? # reset @hyperlinks
    @hyperlinks.reject! {|k, v| k.to_s != 'self'}
    parsed_url = URI.parse(@hyperlinks[:self])
    parsed_url.query = nil
    @hyperlinks[:self] = parsed_url.to_s
  end
end
load_from(url, opts = {}) click to toggle source
# File lib/finix/pagination.rb, line 156
def load_from(url, opts = {})
  parsed_url = URI.parse(url)

  params = {}
  params.merge! @attributes['page'] if @attributes.has_key? 'page'
  params.merge! parse_query(parsed_url.query)
  parsed_url.query = nil # remove query

  # params page
  opts ||= {}
  page = opts.delete('page')
  unless page.nil?
    page = Finix::Utils.indifferent_read_access page
    params.merge! page unless page.nil?
  end

  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
end
parse_query(qs, d = nil) click to toggle source

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 cookies by changing the characters used in the second parameter (which defaults to '&;').

# File lib/finix/pagination.rb, line 184
def parse_query(qs, d = nil)
  params = {}
  (qs || '').split(d ? /[#{d}] */n : /[&;] */n).each do |p|
    k, v = p.split('=', 2).map { |x| CGI::unescape(x) }
    if (cur = params[k])
      if cur.class == Array
        params[k] << v
      else
        params[k] = [cur, v]
      end
    else
      params[k] = v
    end
  end

  params
end