Module: TelegramOnSteroids::Keyboard::Paginatable

Defined in:
lib/telegram_on_steroids/keyboard/paginatable.rb

Constant Summary collapse

DEFAULT_PER_PAGE =
2

Instance Method Summary collapse

Instance Method Details

#current_pageObject



30
31
32
33
34
# File 'lib/telegram_on_steroids/keyboard/paginatable.rb', line 30

def current_page
  return action.session.read(:current_page) || 1 unless request.params.callback_data =~ /page/

  request.params.callback_data.scan(/\d+/).first.to_i
end

#first_page?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/telegram_on_steroids/keyboard/paginatable.rb', line 44

def first_page?
  current_page == 1
end

#last_page?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/telegram_on_steroids/keyboard/paginatable.rb', line 40

def last_page?
  current_page * per_page >= buttons.size
end


15
16
17
18
19
20
# File 'lib/telegram_on_steroids/keyboard/paginatable.rb', line 15

def navigation_buttons
  return [prev_button] if last_page?
  return [next_button] if first_page?

  [prev_button, next_button]
end

#next_buttonObject



52
53
54
# File 'lib/telegram_on_steroids/keyboard/paginatable.rb', line 52

def next_button
  { text: 'Next >>', callback_data: "page_#{current_page + 1}" }
end

#offsetObject



36
37
38
# File 'lib/telegram_on_steroids/keyboard/paginatable.rb', line 36

def offset
  (current_page - 1) * per_page
end

#paginatableObject



4
5
6
# File 'lib/telegram_on_steroids/keyboard/paginatable.rb', line 4

def paginatable
  true
end

#per_pageObject



22
23
24
# File 'lib/telegram_on_steroids/keyboard/paginatable.rb', line 22

def per_page
  @per_page || DEFAULT_PER_PAGE
end

#per_page=(val) ⇒ Object



26
27
28
# File 'lib/telegram_on_steroids/keyboard/paginatable.rb', line 26

def per_page=(val)
  @per_page = val
end

#prev_buttonObject



48
49
50
# File 'lib/telegram_on_steroids/keyboard/paginatable.rb', line 48

def prev_button
  { text: '<< Prev', callback_data: "page_#{current_page - 1}" }
end

#to_telegram_formatObject



8
9
10
11
12
13
# File 'lib/telegram_on_steroids/keyboard/paginatable.rb', line 8

def to_telegram_format
  action.session.write(:current_page, current_page)
  return buttons if first_page? && last_page?

  buttons[offset...offset + per_page].push(navigation_buttons)
end