Class: Jekyll::Plugins::PaginateV3::Query::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-paginate-v3/query/filter/core.rb,
lib/jekyll-paginate-v3/query/filter/evaluation.rb,
lib/jekyll-paginate-v3/query/filter/formatting.rb,
lib/jekyll-paginate-v3/query/filter/normalisation.rb

Overview

Filter-normalisation helpers that convert public filter shorthand into one canonical internal tree model. Structure: each supported shorthand form is parsed and wrapped into consistent scalar/range/group nodes.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nested_separator:, equivalents:, split_delimiter:, now_keyword:, today_keyword:, log_lambda: nil) ⇒ Filter

Builds an engine configured for nested key and equivalent-key rules.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/jekyll-paginate-v3/query/filter/core.rb', line 49

def initialize(nested_separator:, equivalents:, split_delimiter:, now_keyword:, today_keyword:, log_lambda: nil)
	@nested_separator = nested_separator
	@split_delimiter = Utils.normalise_split_delimiter(split_delimiter, ',')
	@now_keyword = now_keyword.to_s.strip
	@now_keyword = 'now' if @now_keyword.empty?
	@today_keyword = today_keyword.to_s.strip
	@today_keyword = 'today' if @today_keyword.empty?
	@frontmatter_path = Jekyll::Plugins::PaginateV3::Support::FrontmatterPath.new(
		separator: @nested_separator,
		arrays: :expand,
		equivalents: equivalents
	)
	@string_array = Jekyll::Plugins::PaginateV3::Support::StringArray.new(delimiter: @split_delimiter)
	@log_lambda = log_lambda
end

Class Method Details

.filter_items(items, filters, nested_separator:, equivalents:, split_delimiter: ',', now_keyword: 'now', today_keyword: 'today', log_lambda: nil, context_label: nil) ⇒ Object

Class helper that instantiates a configured engine per call.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jekyll-paginate-v3/query/filter/core.rb', line 20

def self.filter_items(items, filters, nested_separator:, equivalents:, split_delimiter: ',', now_keyword: 'now', today_keyword: 'today', log_lambda: nil, context_label: nil)
	engine = new(
		nested_separator: nested_separator,
		equivalents: equivalents,
		split_delimiter: split_delimiter,
		now_keyword: now_keyword,
		today_keyword: today_keyword,
		log_lambda: log_lambda
	)
	engine.filter_items(items, filters, context_label: context_label)
end

.filter_to_s(filter, split_delimiter: ',', now_keyword: 'now', today_keyword: 'today') ⇒ Object

Human-readable formatter used in logs/debug output.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/jekyll-paginate-v3/query/filter/core.rb', line 33

def self.filter_to_s(filter, split_delimiter: ',', now_keyword: 'now', today_keyword: 'today')
	formatter = new(
		nested_separator: '.',
		equivalents: [],
		split_delimiter: split_delimiter,
		now_keyword: now_keyword,
		today_keyword: today_keyword
	)

	normalised = formatter.send(:normalise_filter, filter)
	return '[invalid filter]' if normalised == false

	formatter.send(:filter_to_s_internal, normalised)
end

Instance Method Details

#filter_items(items, filters, context_label: nil) ⇒ Object

Applies all configured filters sequentially (logical AND across keys).



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/jekyll-paginate-v3/query/filter/core.rb', line 66

def filter_items(items, filters, context_label: nil)
	unless filters.is_a?(Hash)
		log_debug("#{filter_context_prefix(context_label)}No valid filter hash provided; retaining #{items.length} item(s).")
		return items
	end

	current_items = items
	Utils.safe_hash(filters).each do |raw_key, raw_filter|
		normalised = normalise_filter(raw_filter)
		key = raw_key.to_s
		if normalised == false
			log_warning("#{filter_context_prefix(context_label)}Ignoring invalid filter for key='#{key}': #{raw_filter.inspect}.")
			next
		end

		filtered_items = []
		missing_value_items = []
		excluded_items = []

		current_items.each do |item|
			value = extract_item_value(item, key)
			if check_filter_definition(normalised, value)
				filtered_items << item
			elsif value.nil?
				missing_value_items << item
			else
				excluded_items << {
					'item' => item,
					'values' => value
				}
			end
		end

		log_filter_result(
			key: key,
			normalised_filter: normalised,
			total_items: current_items.length,
			filtered_items: filtered_items,
			missing_value_items: missing_value_items,
			excluded_items: excluded_items,
			context_label: context_label
		)
		current_items = filtered_items
	end

	current_items
end