Class: RedBlocks::Paginator

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#headObject (readonly)

Returns the value of attribute head.



6
7
8
# File 'lib/red_blocks/paginator.rb', line 6

def head
  @head
end

#pageObject (readonly)

Returns the value of attribute page.



6
7
8
# File 'lib/red_blocks/paginator.rb', line 6

def page
  @page
end

#perObject (readonly)

Returns the value of attribute per.



6
7
8
# File 'lib/red_blocks/paginator.rb', line 6

def per
  @per
end

#tailObject (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

.allObject



35
36
37
# File 'lib/red_blocks/paginator.rb', line 35

def self.all
  All.new
end

Instance Method Details

#sizeObject



26
27
28
# File 'lib/red_blocks/paginator.rb', line 26

def size
  @per || (@tail - @head + 1)
end