Class: ParamsReady::Pagination::OffsetPagination

Inherits:
ParamsReady::Parameter::TupleParameter show all
Includes:
AbstractPagination
Defined in:
lib/params_ready/pagination/offset_pagination.rb

Instance Attribute Summary

Attributes inherited from ParamsReady::Parameter::AbstractParameter

#definition

Instance Method Summary collapse

Methods included from AbstractPagination

#first_page, #last_page, #num_pages

Methods inherited from ParamsReady::Parameter::TupleParameter

#method_missing, #respond_to_missing?

Methods included from Marshaller::ParameterModule

#marshal

Methods included from ParamsReady::Parameter::ArrayParameter::ArrayLike

#[], #[]=

Methods included from ParamsReady::Parameter::ComplexParameter

#update_child

Methods inherited from ParamsReady::Parameter::Parameter

#allows_undefined?, #definite_default?, #eligible_for_output?, #find_in_hash, #format, #format_self_permitted, #freeze, #hash, #hash_key, #initialize, #inspect_content, #is_default?, #is_definite?, #is_nil?, #is_undefined?, #memo, #memo!, #nil_default?, #populate_other, #set_from_input, #set_value, #to_hash_if_eligible, #unwrap, #unwrap_or, #wrap_output

Methods inherited from ParamsReady::Parameter::AbstractParameter

#==, #dup, #initialize, #inspect, intent_for_children, #match?, #populate, #to_hash, #update_if_applicable, #update_in

Methods included from Extensions::Freezer

#freeze_variable, #freeze_variables, #variables_to_freeze

Methods included from ParamsReady::Parameter::FromHash

#set_from_hash

Methods included from Extensions::Freezer::InstanceMethods

#freeze

Constructor Details

This class inherits a constructor from ParamsReady::Parameter::Parameter

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ParamsReady::Parameter::TupleParameter

Instance Method Details

#can_yield_page?(delta, count: nil) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
106
107
# 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_valueObject



48
49
50
# File 'lib/params_ready/pagination/offset_pagination.rb', line 48

def current_page_value
  page_value(0)
end

#first_page_valueObject



60
61
62
# File 'lib/params_ready/pagination/offset_pagination.rb', line 60

def first_page_value
  [0, limit]
end

#has_next?(delta = 1, count:) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



88
89
90
91
92
93
# 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) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
98
99
100
101
# 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) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



81
82
83
84
85
86
# 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:) ⇒ Object



64
65
66
67
68
69
70
# 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

#limitObject



30
31
32
# File 'lib/params_ready/pagination/offset_pagination.rb', line 30

def limit
  second.unwrap
end

#limit=(lmt) ⇒ Object



26
27
28
# File 'lib/params_ready/pagination/offset_pagination.rb', line 26

def limit=(lmt)
  self.second.set_value lmt
end

#limit_keyObject



34
35
36
# File 'lib/params_ready/pagination/offset_pagination.rb', line 34

def limit_key
  1
end

#new_offset(delta) ⇒ Object



72
73
74
75
76
77
78
79
# 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) ⇒ Object



56
57
58
# File 'lib/params_ready/pagination/offset_pagination.rb', line 56

def next_page_value(delta = 1, count: nil)
  page_value(delta, count: count)
end

#offsetObject



22
23
24
# File 'lib/params_ready/pagination/offset_pagination.rb', line 22

def offset
  first.unwrap
end

#offset=(off) ⇒ Object



18
19
20
# File 'lib/params_ready/pagination/offset_pagination.rb', line 18

def offset=(off)
  self.first.set_value off
end

#page_noObject



38
39
40
# File 'lib/params_ready/pagination/offset_pagination.rb', line 38

def page_no
  ((offset + limit - 1) / limit) + 1
end

#page_value(delta, count: nil) ⇒ Object



42
43
44
45
46
# 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, _, _, _) ⇒ Object



14
15
16
# File 'lib/params_ready/pagination/offset_pagination.rb', line 14

def paginate_query(query, _, _, _)
  query.skip(offset).take(limit)
end

#paginate_relation(relation, _, _) ⇒ Object



10
11
12
# 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) ⇒ Object



52
53
54
# File 'lib/params_ready/pagination/offset_pagination.rb', line 52

def previous_page_value(delta = 1)
  page_value(-delta)
end