Class: Videojuicer::Resource::Collection

Inherits:
Array
  • Object
show all
Defined in:
lib/videojuicer/resource/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(objects, total, offset, limit) ⇒ Collection

Returns a new instance of Collection.



16
17
18
19
20
21
22
# File 'lib/videojuicer/resource/collection.rb', line 16

def initialize(objects, total, offset, limit)
  clear
  objects.each {|o| self << o }
  self.total = total
  self.offset = offset
  self.limit = limit
end

Instance Attribute Details

#limitObject

Returns the value of attribute limit.



12
13
14
# File 'lib/videojuicer/resource/collection.rb', line 12

def limit
  @limit
end

#offsetObject

Returns the value of attribute offset.



13
14
15
# File 'lib/videojuicer/resource/collection.rb', line 13

def offset
  @offset
end

#totalObject

Returns the value of attribute total.



14
15
16
# File 'lib/videojuicer/resource/collection.rb', line 14

def total
  @total
end

Class Method Details

.offset_from_page_number(page, limit) ⇒ Integer

Provides an offset when given a page number

Parameters:

  • page (Integer)
  • limit (Integer)

Returns:

  • (Integer)


35
36
37
38
39
# File 'lib/videojuicer/resource/collection.rb', line 35

def self.offset_from_page_number page, limit
  page = page.to_i
  return 0 if page == 1
  (page - 1) * limit.to_i
end

Instance Method Details

#page_countObject



41
42
43
44
# File 'lib/videojuicer/resource/collection.rb', line 41

def page_count
  return 1 if limit.nil? or total.nil? or total < 1
  (total.to_f/limit.to_f).ceil
end

#page_numberObject



46
47
48
49
# File 'lib/videojuicer/resource/collection.rb', line 46

def page_number
  return 1 if limit.nil? or offset.nil? or offset < 1
  ((offset+1).to_f/limit.to_f).ceil
end