Class: RestFtpDaemon::Paginate
- Inherits:
-
Object
- Object
- RestFtpDaemon::Paginate
- Defined in:
- lib/rest-ftp-daemon/paginate.rb
Instance Attribute Summary collapse
-
#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.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/rest-ftp-daemon/paginate.rb', line 6 def initialize data # Defaults @pages = 0 @total = 0 @data = [] @only = nil @page = 1 @pages = 1 # 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
#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
31 32 33 34 35 36 37 |
# File 'lib/rest-ftp-daemon/paginate.rb', line 31 def browser out = [] 1.upto(@pages) do |p| out << link(p) end out.join() end |
#page=(raw_page) ⇒ Object
27 28 29 |
# File 'lib/rest-ftp-daemon/paginate.rb', line 27 def page= raw_page @page = [1, raw_page.to_i, @pages].sort[1] end |
#subset ⇒ Object
39 40 41 42 43 |
# File 'lib/rest-ftp-daemon/paginate.rb', line 39 def subset size = DEFAULT_PAGE_SIZE.to_i offset = (@page-1) * size @data[offset, size] end |