Class: RestFtpDaemon::Paginate

Inherits:
Object
  • Object
show all
Defined in:
lib/rest-ftp-daemon/paginate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Paginate

Returns a new instance of Paginate.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rest-ftp-daemon/paginate.rb', line 7

def initialize data
  # Defaults
  @pages = 0
  @total = 0
  @data = []
  @only = nil
  @page = 1
  @pages = 1
  @all = false

  # Ensure data set is countable
  return unless data.is_a? Enumerable
  @data = data

  # Count elements
  @total = @data.count

  # Count pages
  @pages = (@total.to_f / DEFAULT_PAGE_SIZE).ceil
  @pages = 1 if @pages < 1
end

Instance Attribute Details

#allObject

Returns the value of attribute all.



5
6
7
# File 'lib/rest-ftp-daemon/paginate.rb', line 5

def all
  @all
end

#only=(value) ⇒ Object (writeonly)

Sets the attribute only

Parameters:

  • value

    the value to set the attribute only to.



4
5
6
# File 'lib/rest-ftp-daemon/paginate.rb', line 4

def only=(value)
  @only = value
end

Instance Method Details

#browserObject



33
34
35
36
37
38
39
40
41
# File 'lib/rest-ftp-daemon/paginate.rb', line 33

def browser
  return if @all

  out = []
  1.upto(@pages) do |p|
    out << link(p)
  end
  out.join()
end

#page=(raw_page) ⇒ Object



29
30
31
# File 'lib/rest-ftp-daemon/paginate.rb', line 29

def page= raw_page
  @page = [1, raw_page.to_i, @pages].sort[1]
end

#subsetObject



43
44
45
46
47
48
49
# File 'lib/rest-ftp-daemon/paginate.rb', line 43

def subset
  return @data if @all

  size = DEFAULT_PAGE_SIZE.to_i
  offset = (@page-1) * size
  @data[offset, size]
end