Class: RedBlocks::Paginator
- Inherits:
-
Object
- Object
- RedBlocks::Paginator
- Defined in:
- lib/red_blocks/paginator.rb
Defined Under Namespace
Classes: All
Constant Summary collapse
- DEFAULT_PER =
10
- DEFAULT_PAGE =
1
Instance Attribute Summary collapse
-
#head ⇒ Object
readonly
Returns the value of attribute head.
-
#page ⇒ Object
readonly
Returns the value of attribute page.
-
#per ⇒ Object
readonly
Returns the value of attribute per.
-
#tail ⇒ Object
readonly
Returns the value of attribute tail.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(per: DEFAULT_PER, page: DEFAULT_PAGE, head: nil, tail: nil) ⇒ Paginator
constructor
A new instance of Paginator.
- #size ⇒ Object
Constructor Details
#initialize(per: DEFAULT_PER, page: DEFAULT_PAGE, head: nil, tail: nil) ⇒ Paginator
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/red_blocks/paginator.rb', line 8 def initialize(per: DEFAULT_PER, page: DEFAULT_PAGE, head: nil, tail: nil) if head && tail unless head >= 0 && tail >= 0 && head <= tail raise ArgumentError.new("head and tail is out of range (head=#{head}, tail=#{tail})") end @head = head @tail = tail return end @per = per.to_i @page = page.to_i @head = @per * (@page - 1) @tail = @head + @per - 1 end |
Instance Attribute Details
#head ⇒ Object (readonly)
Returns the value of attribute head.
6 7 8 |
# File 'lib/red_blocks/paginator.rb', line 6 def head @head end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
6 7 8 |
# File 'lib/red_blocks/paginator.rb', line 6 def page @page end |
#per ⇒ Object (readonly)
Returns the value of attribute per.
6 7 8 |
# File 'lib/red_blocks/paginator.rb', line 6 def per @per end |
#tail ⇒ Object (readonly)
Returns the value of attribute tail.
6 7 8 |
# File 'lib/red_blocks/paginator.rb', line 6 def tail @tail end |
Class Method Details
Instance Method Details
#size ⇒ Object
26 27 28 |
# File 'lib/red_blocks/paginator.rb', line 26 def size @per || (@tail - @head + 1) end |