Class: SlackBot::Pager

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

Constant Summary collapse

DEFAULT_PAGE =
1
DEFAULT_LIMIT =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_cursor, args:, limit: nil, page: nil) ⇒ Pager

Returns a new instance of Pager.



7
8
9
10
11
12
# File 'lib/slack_bot/pager.rb', line 7

def initialize(source_cursor, args:, limit: nil, page: nil)
  @source_cursor = source_cursor
  @args = args
  @limit = limit || @args[:per_page]&.to_i || DEFAULT_LIMIT
  @page = page || @args[:page]&.to_i || DEFAULT_PAGE
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



6
7
8
# File 'lib/slack_bot/pager.rb', line 6

def args
  @args
end

#limitObject (readonly)

Returns the value of attribute limit.



6
7
8
# File 'lib/slack_bot/pager.rb', line 6

def limit
  @limit
end

#pageObject (readonly)

Returns the value of attribute page.



6
7
8
# File 'lib/slack_bot/pager.rb', line 6

def page
  @page
end

#source_cursorObject (readonly)

Returns the value of attribute source_cursor.



6
7
8
# File 'lib/slack_bot/pager.rb', line 6

def source_cursor
  @source_cursor
end

Instance Method Details

#cursorObject



26
27
28
# File 'lib/slack_bot/pager.rb', line 26

def cursor
  source_cursor.limit(limit).offset(offset)
end

#offsetObject



22
23
24
# File 'lib/slack_bot/pager.rb', line 22

def offset
  (page - 1) * limit
end

#pages_countObject



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

def pages_count
  (source_cursor.count.to_f / limit).ceil
end

#total_countObject



14
15
16
# File 'lib/slack_bot/pager.rb', line 14

def total_count
  source_cursor.count
end