Class: Finix::Pagination
- Inherits:
-
Object
show all
- Includes:
- Enumerable, HalResource
- Defined in:
- lib/finix/pagination.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
#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
opts
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class Finix::HalResource
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
13
14
15
|
# File 'lib/finix/pagination.rb', line 13
def attributes
@attributes
end
|
#hyperlinks ⇒ Object
Returns the value of attribute hyperlinks.
14
15
16
|
# File 'lib/finix/pagination.rb', line 14
def hyperlinks
@hyperlinks
end
|
#resource_class ⇒ Object
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
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
|
#each ⇒ Object
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
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)
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_page ⇒ Object
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) || {}
opts
self
end
|
#last_page ⇒ Object
108
109
110
111
|
# File 'lib/finix/pagination.rb', line 108
def last_page
fetch :last
self
end
|
#loaded ⇒ Object
129
130
131
|
# File 'lib/finix/pagination.rb', line 129
def loaded
not self.items.nil?
end
|
#next_page ⇒ Object
98
99
100
101
|
# File 'lib/finix/pagination.rb', line 98
def next_page
fetch :next
self
end
|
#num_pages ⇒ Object
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_page ⇒ Object
103
104
105
106
|
# File 'lib/finix/pagination.rb', line 103
def previous_page
fetch :prev
self
end
|
#refresh ⇒ Object
Also known as:
retrieve, load!
79
80
81
82
|
# File 'lib/finix/pagination.rb', line 79
def refresh
fetch
self
end
|
#total ⇒ Object
118
119
120
121
|
# File 'lib/finix/pagination.rb', line 118
def total
self.page.count
end
|