Class: ParamsReady::Pagination::OffsetPagination
Instance Attribute Summary
#definition
Instance Method Summary
collapse
-
#can_yield_page?(delta, count: nil) ⇒ Boolean
-
#current_page_value ⇒ Object
-
#first_page_value ⇒ Object
-
#has_next?(delta = 1, count:) ⇒ Boolean
-
#has_page?(delta, count: nil) ⇒ Boolean
-
#has_previous?(delta = 1) ⇒ Boolean
-
#last_page_value(count:) ⇒ Object
-
#limit ⇒ Object
-
#limit=(lmt) ⇒ Object
-
#limit_key ⇒ Object
-
#new_offset(delta) ⇒ Object
-
#next_page_value(delta = 1, count: nil) ⇒ Object
-
#offset ⇒ Object
-
#offset=(off) ⇒ Object
-
#page_no ⇒ Object
-
#page_value(delta, count: nil) ⇒ Object
-
#paginate_query(query, _, _, _) ⇒ Object
-
#paginate_relation(relation, _, _) ⇒ Object
-
#previous_page_value(delta = 1) ⇒ Object
#first_page, #last_page, #num_pages
#method_missing, #respond_to_missing?
#marshal
#[], #[]=
#update_child
#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
#==, #dup, #initialize, #inspect, intent_for_children, #match?, #populate, #to_hash, #update_if_applicable, #update_in
#freeze_variable, #freeze_variables, #variables_to_freeze
#set_from_hash
#freeze
Instance Method Details
#can_yield_page?(delta, count: nil) ⇒ 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_value ⇒ Object
48
49
50
|
# File 'lib/params_ready/pagination/offset_pagination.rb', line 48
def current_page_value
page_value(0)
end
|
#first_page_value ⇒ Object
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
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
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
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
|
#limit ⇒ Object
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_key ⇒ Object
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
|
#offset ⇒ Object
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_no ⇒ Object
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
|