class ParamsReady::Pagination::OffsetPagination

Public Instance Methods

can_yield_page?(delta, count: nil) click to toggle source
# File lib/params_ready/pagination/offset_pagination.rb, line 103
def can_yield_page?(delta, count: nil)
  return true if delta >= 0 && count.nil?

  has_page?(delta, count: count)
end
current_page_value() click to toggle source
# File lib/params_ready/pagination/offset_pagination.rb, line 48
def current_page_value
  page_value(0)
end
first_page_value() click to toggle source
# File lib/params_ready/pagination/offset_pagination.rb, line 60
def first_page_value
  [0, limit]
end
has_next?(delta = 1, count:) click to toggle source
# File lib/params_ready/pagination/offset_pagination.rb, line 88
def has_next?(delta = 1, count:)
  raise ParamsReadyError, 'Nil count unexpected' if count.nil?
  raise ParamsReadyError, 'Negative delta unexpected' if delta < 0

  offset + (delta * limit) < count
end
has_page?(delta, count: nil) click to toggle source
# File lib/params_ready/pagination/offset_pagination.rb, line 95
def has_page?(delta, count: nil)
  if delta > 0
    has_next? delta, count: count
  else
    has_previous? -delta
  end
end
has_previous?(delta = 1) click to toggle source
# File lib/params_ready/pagination/offset_pagination.rb, line 81
def has_previous?(delta = 1)
  raise ParamsReadyError, 'Negative delta unexpected' if delta < 0
  return false if offset == 0

  delta * limit < offset + limit
end
last_page_value(count:) click to toggle source
# File lib/params_ready/pagination/offset_pagination.rb, line 64
def last_page_value(count:)
  num_pages = num_pages(count: count)
  return nil if num_pages == 0

  new_offset = (num_pages - 1) * limit
  [new_offset, limit]
end
limit() click to toggle source
# File lib/params_ready/pagination/offset_pagination.rb, line 30
def limit
  second.unwrap
end
limit=(lmt) click to toggle source
# File lib/params_ready/pagination/offset_pagination.rb, line 26
def limit=(lmt)
  self.second.set_value lmt
end
limit_key() click to toggle source
# File lib/params_ready/pagination/offset_pagination.rb, line 34
def limit_key
  1
end
new_offset(delta) click to toggle source
# File lib/params_ready/pagination/offset_pagination.rb, line 72
def new_offset(delta)
  shift = delta * limit
  no = offset + shift
  return no if no >= 0
  return 0 if shift.abs < offset + limit

  nil
end
next_page_value(delta = 1, count: nil) click to toggle source
# File lib/params_ready/pagination/offset_pagination.rb, line 56
def next_page_value(delta = 1, count: nil)
  page_value(delta, count: count)
end
offset() click to toggle source
# File lib/params_ready/pagination/offset_pagination.rb, line 22
def offset
  first.unwrap
end
offset=(off) click to toggle source
# File lib/params_ready/pagination/offset_pagination.rb, line 18
def offset=(off)
  self.first.set_value off
end
page_no() click to toggle source
# File lib/params_ready/pagination/offset_pagination.rb, line 38
def page_no
  ((offset + limit - 1) / limit) + 1
end
page_value(delta, count: nil) click to toggle source
# File lib/params_ready/pagination/offset_pagination.rb, line 42
def page_value(delta, count: nil)
  return nil unless can_yield_page?(delta, count: count)

  [new_offset(delta), limit]
end
paginate_query(query, _, _, _) click to toggle source
# File lib/params_ready/pagination/offset_pagination.rb, line 14
def paginate_query(query, _, _, _)
  query.skip(offset).take(limit)
end
paginate_relation(relation, _, _) click to toggle source
# File lib/params_ready/pagination/offset_pagination.rb, line 10
def paginate_relation(relation, _, _)
  relation.offset(offset).limit(limit)
end
previous_page_value(delta = 1) click to toggle source
# File lib/params_ready/pagination/offset_pagination.rb, line 52
def previous_page_value(delta = 1)
  page_value(-delta)
end