Class: FreshBooks::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/freshbooks/list_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, per_page, total, total_in_array = total) ⇒ Page

Returns a new instance of Page.



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/freshbooks/list_proxy.rb', line 42

def initialize(page, per_page, total, total_in_array = total)
  @page = page.to_i
  @per_page = per_page.to_i
  @total = total.to_i
  
  # Detect if response has pagination
  if @per_page == 0 && @total == 0 && total_in_array != 0
    # No pagination so fake it
    @page = 1
    @per_page = @total = total_in_array
  end
end

Instance Attribute Details

#pageObject (readonly)

Returns the value of attribute page.



40
41
42
# File 'lib/freshbooks/list_proxy.rb', line 40

def page
  @page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



40
41
42
# File 'lib/freshbooks/list_proxy.rb', line 40

def per_page
  @per_page
end

#totalObject (readonly)

Returns the value of attribute total.



40
41
42
# File 'lib/freshbooks/list_proxy.rb', line 40

def total
  @total
end

Instance Method Details

#next_page?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/freshbooks/list_proxy.rb', line 76

def next_page?
  page < pages
end

#page_number(position) ⇒ Object

Get the page number that this element is on given the number of elements per page



56
57
58
# File 'lib/freshbooks/list_proxy.rb', line 56

def page_number(position)
  (position / per_page) + 1
end

#pagesObject



65
66
67
68
69
70
71
72
73
74
# File 'lib/freshbooks/list_proxy.rb', line 65

def pages
  return 0 if per_page == 0
  
  pages = total / per_page
  
  if (total % per_page) != 0
    pages += 1
  end
  pages
end

#position_number(position) ⇒ Object

Get the position number of this element based relative to the current page



61
62
63
# File 'lib/freshbooks/list_proxy.rb', line 61

def position_number(position)
  position - ((page - 1) * per_page)
end