Class: LlmCostTracker::Dashboard::Pagination

Inherits:
Object
  • Object
show all
Defined in:
app/services/llm_cost_tracker/dashboard/pagination.rb

Constant Summary collapse

DEFAULT_PER =
50
MAX_PER =
200
MIN_PAGE =
1
MAX_PAGE =
100_000
MIN_PER =
1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page:, per:) ⇒ Pagination

Returns a new instance of Pagination.



32
33
34
35
36
# File 'app/services/llm_cost_tracker/dashboard/pagination.rb', line 32

def initialize(page:, per:)
  @page = page
  @per = per
  freeze
end

Instance Attribute Details

#pageObject (readonly)

Returns the value of attribute page.



12
13
14
# File 'app/services/llm_cost_tracker/dashboard/pagination.rb', line 12

def page
  @page
end

#perObject (readonly)

Returns the value of attribute per.



12
13
14
# File 'app/services/llm_cost_tracker/dashboard/pagination.rb', line 12

def per
  @per
end

Class Method Details

.call(params) ⇒ Object



14
15
16
17
18
19
20
# File 'app/services/llm_cost_tracker/dashboard/pagination.rb', line 14

def self.call(params)
  params = Params.to_hash(params).symbolize_keys
  new(
    page: integer_param(params, :page, default: MIN_PAGE, min: MIN_PAGE, max: MAX_PAGE),
    per: integer_param(params, :per, default: DEFAULT_PER, min: MIN_PER, max: MAX_PER)
  )
end

Instance Method Details

#next_page?(total_count) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
# File 'app/services/llm_cost_tracker/dashboard/pagination.rb', line 46

def next_page?(total_count)
  total_count = total_count.to_i
  offset + per < total_count
end

#offsetObject



38
39
40
# File 'app/services/llm_cost_tracker/dashboard/pagination.rb', line 38

def offset
  (page - 1) * per
end

#prev_page?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'app/services/llm_cost_tracker/dashboard/pagination.rb', line 42

def prev_page?
  page > MIN_PAGE
end

#total_pages(total_count) ⇒ Object



51
52
53
54
55
56
# File 'app/services/llm_cost_tracker/dashboard/pagination.rb', line 51

def total_pages(total_count)
  total_count = total_count.to_i
  return MIN_PAGE unless total_count.positive?

  ((total_count + per - 1) / per)
end