Class: LlmCostTracker::Pagination

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

Constant Summary collapse

DEFAULT_PER =
50
MAX_PER =
200
MIN_PAGE =
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.



29
30
31
32
33
# File 'app/services/llm_cost_tracker/pagination.rb', line 29

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

Instance Attribute Details

#pageObject (readonly)

Returns the value of attribute page.



9
10
11
# File 'app/services/llm_cost_tracker/pagination.rb', line 9

def page
  @page
end

#perObject (readonly)

Returns the value of attribute per.



9
10
11
# File 'app/services/llm_cost_tracker/pagination.rb', line 9

def per
  @per
end

Class Method Details

.call(params) ⇒ Object



11
12
13
14
15
16
17
# File 'app/services/llm_cost_tracker/pagination.rb', line 11

def self.call(params)
  params = LlmCostTracker::ParameterHash.with_indifferent_access(params)
  new(
    page: integer_param(params, :page, default: MIN_PAGE, min: MIN_PAGE),
    per: integer_param(params, :per, default: DEFAULT_PER, min: 1, max: MAX_PER)
  )
end

Instance Method Details

#limitObject



35
36
37
# File 'app/services/llm_cost_tracker/pagination.rb', line 35

def limit
  per
end

#next_page?(total_count) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#offsetObject



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

def offset
  (page - 1) * per
end

#prev_page?Boolean

Returns:

  • (Boolean)


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

def prev_page?
  page > MIN_PAGE
end

#total_pages(total_count) ⇒ Object



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

def total_pages(total_count)
  return MIN_PAGE if total_count.to_i <= 0

  [(total_count.to_f / per).ceil, MIN_PAGE].max
end