Class: RestFtpDaemon::Paginate
- Inherits:
-
Object
- Object
- RestFtpDaemon::Paginate
- Defined in:
- lib/rest-ftp-daemon/paginate.rb
Instance Attribute Summary collapse
-
#all ⇒ Object
Returns the value of attribute all.
-
#only ⇒ Object
writeonly
Sets the attribute only.
Instance Method Summary collapse
- #browser ⇒ Object
-
#initialize(data) ⇒ Paginate
constructor
A new instance of Paginate.
- #page=(raw_page) ⇒ Object
- #subset ⇒ Object
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
#all ⇒ Object
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
4 5 6 |
# File 'lib/rest-ftp-daemon/paginate.rb', line 4 def only=(value) @only = value end |
Instance Method Details
#browser ⇒ Object
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 |
#subset ⇒ Object
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 |