Module: Jekyll::Plugins::PaginateV3::Support::LooseScalar

Defined in:
lib/jekyll-paginate-v3/support/loose_scalar.rb

Overview

Forgiving scalar coercion helpers shared by filtering, grouped-index detection, and loose configuration parsing.

Structure: callers can ask for one specific scalar family without committing the whole pipeline to one feature's semantics.

Class Method Summary collapse

Class Method Details

.boolean(value) ⇒ Object

Interprets one value as a strict boolean or a loose true/false string. Returns nil when no boolean meaning can be inferred.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/jekyll-paginate-v3/support/loose_scalar.rb', line 17

def self.boolean(value)
	return value if value == true || value == false
	return nil unless value.is_a?(String)

	case value.strip.downcase
	when 'true'
		true
	when 'false'
		false
	else
		nil
	end
end

.comparable(value, must_cast: false) ⇒ Object

Converts one value into the loose comparable scalar form used by filtering. Strings become numbers or datetimes when possible, or a stripped string otherwise.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/jekyll-paginate-v3/support/loose_scalar.rb', line 77

def self.comparable(value, must_cast: false)
	return value if value.is_a?(Integer) || value.is_a?(Float) || value.is_a?(DateTime)
	return value.to_datetime if value.is_a?(Time)
	return value.to_datetime if value.is_a?(Date)

	unless value.is_a?(String)
		return nil if must_cast

		return value
	end

	stripped = value.strip

	numeric_value = number(stripped)
	return numeric_value unless numeric_value.nil?

	datetime_value = datetime(stripped)
	return datetime_value unless datetime_value.nil?

	return nil if must_cast

	stripped
end

.comparable_values?(left, right) ⇒ Boolean

Safe comparability check for mixed scalar types.

Returns:

  • (Boolean)


102
103
104
105
106
107
108
109
# File 'lib/jekyll-paginate-v3/support/loose_scalar.rb', line 102

def self.comparable_values?(left, right)
	return true if left.class == right.class
	return true if (left.is_a?(Integer) || left.is_a?(Float)) && (right.is_a?(Integer) || right.is_a?(Float))

	!((left <=> right).nil?)
rescue ArgumentError, NoMethodError
	false
end

.date(value) ⇒ Object

Alias retained because public filter config uses date language.



70
71
72
# File 'lib/jekyll-paginate-v3/support/loose_scalar.rb', line 70

def self.date(value)
	datetime(value)
end

.datetime(value) ⇒ Object

Interprets one value as a datetime in the same loose way used by filter range comparisons.



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/jekyll-paginate-v3/support/loose_scalar.rb', line 55

def self.datetime(value)
	return value if value.is_a?(DateTime)
	return value.to_datetime if value.is_a?(Time)
	return value.to_datetime if value.is_a?(Date)
	return nil unless value.is_a?(String)

	stripped = value.strip
	return nil if stripped.empty?

	DateTime.parse(stripped)
rescue ArgumentError
	nil
end

.integral_number?(value) ⇒ Boolean

Reports whether one value is numeric with no fractional component.

Returns:

  • (Boolean)


45
46
47
48
49
50
51
# File 'lib/jekyll-paginate-v3/support/loose_scalar.rb', line 45

def self.integral_number?(value)
	numeric_value = number(value)
	return false if numeric_value.nil?
	return true if numeric_value.is_a?(Integer)

	(numeric_value % 1).zero?
end

.number(value) ⇒ Object

Interprets one value as an integer or float when possible.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/jekyll-paginate-v3/support/loose_scalar.rb', line 32

def self.number(value)
	return value if value.is_a?(Integer) || value.is_a?(Float)
	return nil unless value.is_a?(String)

	stripped = value.strip
	return nil if stripped.empty?
	return stripped.to_i if stripped.match?(/\A[+-]?\d+\z/)
	return stripped.to_f if stripped.match?(/\A[+-]?\d+\.\d+\z/)

	nil
end