Class: SimplyPaginate::Page
- Inherits:
-
Object
- Object
- SimplyPaginate::Page
- Defined in:
- lib/simply_paginate/page.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #elements ⇒ Object
-
#initialize(page, collection, size = Paginator.per_page) ⇒ Page
constructor
A new instance of Page.
- #next ⇒ Object
- #previous ⇒ Object
Constructor Details
#initialize(page, collection, size = Paginator.per_page) ⇒ Page
Returns a new instance of Page.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/simply_paginate/page.rb', line 5 def initialize(page, collection, size = Paginator.per_page) @index = page @collection = collection @size = size @move_page = lambda do |number| new_index = index + number Page.new(new_index, collection, size) unless (new_index <= 0) || (new_index > (collection.count.to_f / size.to_f).ceil) end end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
3 4 5 |
# File 'lib/simply_paginate/page.rb', line 3 def collection @collection end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
3 4 5 |
# File 'lib/simply_paginate/page.rb', line 3 def index @index end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
3 4 5 |
# File 'lib/simply_paginate/page.rb', line 3 def size @size end |
Instance Method Details
#==(other) ⇒ Object
31 32 33 34 35 |
# File 'lib/simply_paginate/page.rb', line 31 def ==(other) return false unless other.respond_to?(:index) && other.respond_to?(:elements) (index == other.index) && (elements == other.elements) end |
#elements ⇒ Object
24 25 26 27 28 29 |
# File 'lib/simply_paginate/page.rb', line 24 def elements first = (@index - 1) * @size last = first + size - 1 collection[first..last] end |
#next ⇒ Object
16 17 18 |
# File 'lib/simply_paginate/page.rb', line 16 def next @move_page.call(1) end |
#previous ⇒ Object
20 21 22 |
# File 'lib/simply_paginate/page.rb', line 20 def previous @move_page.call(-1) end |