Class: Data::Page

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(total_entries = 0, entries_per_page = 10, current_page = 1) ⇒ Page

Returns a new instance of Page.



3
4
5
# File 'lib/datapage.rb', line 3

def initialize(total_entries=0, entries_per_page=10, current_page=1)
  @total_entries, @entries_per_page, @current_page = total_entries, entries_per_page, current_page
end

Instance Attribute Details

#current_page(*current_page) ⇒ Object (readonly)

Returns the value of attribute current_page.



2
3
4
# File 'lib/datapage.rb', line 2

def current_page
  @current_page
end

#entries_per_page(*entries_per_page) ⇒ Object (readonly)

Returns the value of attribute entries_per_page.



2
3
4
# File 'lib/datapage.rb', line 2

def entries_per_page
  @entries_per_page
end

#total_entries(*total_entries) ⇒ Object (readonly)

Returns the value of attribute total_entries.



2
3
4
# File 'lib/datapage.rb', line 2

def total_entries
  @total_entries
end

Instance Method Details

#change_entries_per_page(new_epp) ⇒ Object



87
88
89
90
91
# File 'lib/datapage.rb', line 87

def change_entries_per_page(new_epp)
  new_page = 1 + (self.first / new_epp)
  self.entries_per_page(new_epp)
  self.current_page(new_page)
end

#entries_on_this_pageObject



80
81
82
83
84
85
86
# File 'lib/datapage.rb', line 80

def entries_on_this_page
  if @total_entries == 0
      return 0
  else
      return self.last - self.first + 1
  end
end

#firstObject



33
34
35
36
37
38
39
# File 'lib/datapage.rb', line 33

def first
  if @total_entries == 0
      return 0
  else
      return ( ( @current_page - 1 ) * @entries_per_page ) + 1
  end
end

#first_pageObject



18
19
20
# File 'lib/datapage.rb', line 18

def first_page
  return 1
end

#lastObject



40
41
42
43
44
45
46
# File 'lib/datapage.rb', line 40

def last
  if @current_page == self.last_page
      return @total_entries
  else
      return @current_page * @entries_per_page
  end
end

#last_pageObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/datapage.rb', line 21

def last_page
  pages = @total_entries.to_f / @entries_per_page.to_f
  if pages == pages.floor
      last_page = pages
  else
      last_page = 1 + pages.floor
  end
  if last_page < 1
      last_page = 1
  end
  return last_page
end

#next_pageObject



54
55
56
57
58
59
60
# File 'lib/datapage.rb', line 54

def next_page
  if @current_page < self.last_page
      return @current_page + 1
  else
      return nil
  end
end

#previous_pageObject



47
48
49
50
51
52
53
# File 'lib/datapage.rb', line 47

def previous_page
  if @current_page > 1
      return @current_page - 1
  else
      return nil
  end
end

#skippedObject



72
73
74
75
76
77
78
79
# File 'lib/datapage.rb', line 72

def skipped
  skipped = self.first - 1
  if skipped < 0
      return 0
  else
      return skipped
  end
end

#splice(array) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/datapage.rb', line 61

def splice(array)
  if array.length > self.last
      top = self.last
  else
      top = array.length
  end
  if top == 0
      return []
  end
  return array.slice(self.first-1, self.last - self.first + 1)
end