Class: Jekyll::Plugins::PaginateV3::Pagination::Paginator

Inherits:
Liquid::Drop
  • Object
show all
Defined in:
lib/jekyll-paginate-v3/pagination/paginator/core.rb,
lib/jekyll-paginate-v3/pagination/paginator/compatibility_aliases.rb

Overview

Backwards-compatible aliases so grouped payload classes remain reachable under Paginator::... as expected by callers.

Constant Summary collapse

GroupPayload =
Pagination::GroupPayload
GroupReference =
Pagination::GroupReference

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(per_page:, items:, current_page:, total_pages:, item_keyword:, compatibility: nil, page_windows: nil) ⇒ Paginator

Returns a new instance of Paginator.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 18

def initialize(per_page:, items:, current_page:, total_pages:, item_keyword:, compatibility: nil, page_windows: nil)
	@per_page_pattern = Utils.normalise_per_page_pattern(per_page)
	@current_index_number = current_page.to_i
	@item_keyword = normalise_item_keyword(item_keyword)
	@compatibility_mode = normalise_compatibility_mode(compatibility)
	@total_item_count = items.size
	@page_windows = normalise_page_windows(page_windows, @total_item_count)
	requested_total_indexes = [total_pages.to_i, 1].max

	if @current_index_number > requested_total_indexes
		raise ArgumentError, "page number cannot be greater than total pages (#{@current_index_number} > #{requested_total_indexes})"
	end

	if requested_total_indexes != @page_windows.length
		raise ArgumentError, "total pages does not match page windows (#{requested_total_indexes} != #{@page_windows.length})"
	end
	@total_indexes = requested_total_indexes

	current_window = window_for_page_number(@current_index_number)
	@per_page = current_window['page_size']
	@page_items = items[current_window['offset_start']...current_window['offset_end']] || []
	@trail = nil
	@groups = []

	@current_page_object = nil

	initialise_index_references!
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



16
17
18
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 16

def current
  @current
end

#firstObject (readonly)

Returns the value of attribute first.



16
17
18
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 16

def first
  @first
end

#groupsObject

Returns the value of attribute groups.



16
17
18
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 16

def groups
  @groups
end

#lastObject (readonly)

Returns the value of attribute last.



16
17
18
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 16

def last
  @last
end

#nextObject (readonly)

Returns the value of attribute next.



16
17
18
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 16

def next
  @next
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



16
17
18
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 16

def per_page
  @per_page
end

#previousObject (readonly)

Returns the value of attribute previous.



16
17
18
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 16

def previous
  @previous
end

#total_indexesObject (readonly)

Returns the value of attribute total_indexes.



16
17
18
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 16

def total_indexes
  @total_indexes
end

#total_item_countObject (readonly)

Returns the value of attribute total_item_count.



16
17
18
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 16

def total_item_count
  @total_item_count
end

#trailObject

Returns the value of attribute trail.



16
17
18
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 16

def trail
  @trail
end

Instance Method Details

#bind_pages(current_page_object:, previous_page_object:, next_page_object:, first_page_object:, last_page_object:) ⇒ Object

Attaches actual generated page/document objects to neighbour links.

We intentionally keep current.page unset so templates can detect the current index without comparing object identities.



51
52
53
54
55
56
57
58
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 51

def bind_pages(current_page_object:, previous_page_object:, next_page_object:, first_page_object:, last_page_object:)
	@current_page_object = current_page_object
	@current = build_index_reference(@current_index_number, nil)
	@previous = @current_index_number > 1 ? build_index_reference(@current_index_number - 1, previous_page_object) : nil
	@next = @current_index_number < @total_indexes ? build_index_reference(@current_index_number + 1, next_page_object) : nil
	@first = build_index_reference(1, first_page_object, allow_current_page_object: true)
	@last = build_index_reference(@total_indexes, last_page_object, allow_current_page_object: true)
end

#build_trail_reference(page_number:, page_object:, current:, distance:) ⇒ Object

Builds one trail entry for a page number.

Used by the model so trail windows can reuse canonical index maths.



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 85

def build_trail_reference(page_number:, page_object:, current:, distance:)
	window = window_for_page_number(page_number)

	TrailReference.new(
		num: page_number,
		page_object: page_object,
		item_count: window['count'],
		start_item_index: window['start'],
		end_item_index: window['end'],
		current: current,
		distance: distance
	)
end

#first_pageObject

Legacy v1/v2 alias for first page number.



149
150
151
152
153
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 149

def first_page
	return nil unless compatibility_mode?

	first&.num
end

#first_page_pathObject

Legacy v1/v2 alias for first page URL.



156
157
158
159
160
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 156

def first_page_path
	return nil unless compatibility_mode?

	index_reference_path(first)
end

#groupObject

Shortcut to the deepest grouped-set payload.



71
72
73
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 71

def group
	groups.last
end

#last_pageObject

Legacy v1/v2 alias for last page number.



163
164
165
166
167
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 163

def last_page
	return nil unless compatibility_mode?

	last&.num
end

#last_page_pathObject

Legacy v1/v2 alias for last page URL.



170
171
172
173
174
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 170

def last_page_path
	return nil unless compatibility_mode?

	index_reference_path(last)
end

#liquid_method_missing(method_name) ⇒ Object

Handles dynamic alias keys that are not explicit methods.



214
215
216
217
218
219
220
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 214

def liquid_method_missing(method_name)
	method_key = method_name.to_s
	return @page_items if method_key == @item_keyword
	return @total_item_count if method_key == "total_#{@item_keyword}"

	super
end

#next_pageObject

Legacy v1/v2 alias for next page number.



135
136
137
138
139
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 135

def next_page
	return nil unless compatibility_mode?

	self.next&.num
end

#next_page_pathObject

Legacy v1/v2 alias for next page URL.



142
143
144
145
146
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 142

def next_page_path
	return nil unless compatibility_mode?

	index_reference_path(self.next)
end

#pageObject

Legacy v1/v2 alias for current page number.



100
101
102
103
104
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 100

def page
	return nil unless compatibility_mode?

	current&.num
end

#page_pathObject

Legacy v1/v2 alias for current page URL.



114
115
116
117
118
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 114

def page_path
	return nil unless compatibility_mode?

	index_reference_path(current)
end

#page_trailObject

Legacy v1/v2 alias for trail payload shape.



177
178
179
180
181
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 177

def page_trail
	return nil unless compatibility_mode?

	compatibility_page_trail
end

#prevObject

Backwards-compatible alias for previous index reference.

prev remains documented and available in Liquid templates.



78
79
80
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 78

def prev
	previous
end

#previous_pageObject

Legacy v1/v2 alias for previous page number.



121
122
123
124
125
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 121

def previous_page
	return nil unless compatibility_mode?

	previous&.num
end

#previous_page_pathObject

Legacy v1/v2 alias for previous page URL.



128
129
130
131
132
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 128

def previous_page_path
	return nil unless compatibility_mode?

	index_reference_path(previous)
end

#to_hObject

Hash form used by specs and non-Liquid inspection.

Liquid rendering uses this Drop instance directly.



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 186

def to_h
	payload = standard_payload_hash
	payload[@item_keyword] = @page_items
	payload["total_#{@item_keyword}"] = @total_item_count

	if compatibility_mode?
		add_compatibility_item_keys!(payload)
		payload.merge!(
			'per_page' => per_page,
			'page' => page,
			'total_pages' => total_pages,
			'page_path' => page_path,
			'previous_page' => previous_page,
			'previous_page_path' => previous_page_path,
			'next_page' => next_page,
			'next_page_path' => next_page_path,
			'first_page' => first_page,
			'first_page_path' => first_page_path,
			'last_page' => last_page,
			'last_page_path' => last_page_path,
			'page_trail' => page_trail
		)
	end

	payload
end

#total_pagesObject

Legacy v1/v2 alias for total page count.



107
108
109
110
111
# File 'lib/jekyll-paginate-v3/pagination/paginator/core.rb', line 107

def total_pages
	return nil unless compatibility_mode?

	total_indexes
end