Class: Finix::Pagination

Inherits:
Object
  • Object
show all
Includes:
Enumerable, HalResource
Defined in:
lib/finix/pagination.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HalResource

#load_page_from_response!, #method_missing

Constructor Details

#initialize(*args) ⇒ Pagination

Returns a new instance of Pagination.



16
17
18
19
20
21
22
23
24
25
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Finix::HalResource

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



13
14
15
# File 'lib/finix/pagination.rb', line 13

def attributes
  @attributes
end

Returns the value of attribute hyperlinks.



14
15
16
# File 'lib/finix/pagination.rb', line 14

def hyperlinks
  @hyperlinks
end

#resource_classObject

Returns the value of attribute resource_class.



12
13
14
# File 'lib/finix/pagination.rb', line 12

def resource_class
  @resource_class
end

Instance Method Details

#count(*args) ⇒ Object



42
43
44
45
46
47
48
# 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 = {}) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# 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

#eachObject



33
34
35
36
37
38
39
40
# 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) ⇒ Object

:next, :last, :first, :prev, :self

Raises:

  • (StopIteration)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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

#first_pageObject



113
114
115
116
# File 'lib/finix/pagination.rb', line 113

def first_page
  fetch :first
  self
end

#init!(*args) ⇒ Object



27
28
29
30
31
# File 'lib/finix/pagination.rb', line 27

def init!(*args)
  opts = args.slice(0) || {}
  extract_opts opts
  self
end

#last_pageObject



108
109
110
111
# File 'lib/finix/pagination.rb', line 108

def last_page
  fetch :last
  self
end

#loadedObject



129
130
131
# File 'lib/finix/pagination.rb', line 129

def loaded
  not self.items.nil?
end

#next_pageObject



98
99
100
101
# File 'lib/finix/pagination.rb', line 98

def next_page
  fetch :next
  self
end

#num_pagesObject



123
124
125
126
127
# File 'lib/finix/pagination.rb', line 123

def num_pages
  num = total / limit
  num += 1 if total % limit > 0
  num
end

#previous_pageObject



103
104
105
106
# File 'lib/finix/pagination.rb', line 103

def previous_page
  fetch :prev
  self
end

#refreshObject Also known as: retrieve, load!



79
80
81
82
# File 'lib/finix/pagination.rb', line 79

def refresh
  fetch
  self
end

#totalObject



118
119
120
121
# File 'lib/finix/pagination.rb', line 118

def total
  # refresh unless loaded
  self.page.count
end