Class: Jekyll::Plugins::PaginateV3::Support::StringArray

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-paginate-v3/support/string_array.rb

Overview

Interprets loose config/frontmatter values as arrays.

Use one instance per delimiter configuration, then call with to derive a nearby variant without restating the full configuration.

Constant Summary collapse

DEFAULT_DELIMITER =
','
UNSET =
Object.new.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delimiter: DEFAULT_DELIMITER) ⇒ StringArray

Builds a string-array helper for one default delimiter.



21
22
23
# File 'lib/jekyll-paginate-v3/support/string_array.rb', line 21

def initialize(delimiter: DEFAULT_DELIMITER)
	@delimiter = self.class.normalise_delimiter(delimiter, DEFAULT_DELIMITER)
end

Instance Attribute Details

#delimiterObject (readonly)

Returns the value of attribute delimiter.



18
19
20
# File 'lib/jekyll-paginate-v3/support/string_array.rb', line 18

def delimiter
  @delimiter
end

Class Method Details

.normalise_delimiter(raw_delimiter, default_delimiter = DEFAULT_DELIMITER) ⇒ Object

Normalises one configured delimiter.

Blank or unsupported values fall back to default_delimiter. false disables string splitting.



36
37
38
39
40
41
42
43
44
# File 'lib/jekyll-paginate-v3/support/string_array.rb', line 36

def self.normalise_delimiter(raw_delimiter, default_delimiter = DEFAULT_DELIMITER)
	return false if raw_delimiter == false
	return default_delimiter unless raw_delimiter.is_a?(String)

	delimiter = raw_delimiter
	return false if delimiter.strip.downcase == 'false'

	delimiter.empty? ? default_delimiter : delimiter
end

Instance Method Details

#interpret(value, split: true, flatten: false, delimiter: UNSET) ⇒ Object

Interprets a scalar or array as an array with configurable string splitting depth.

split modes:

  • true / 0: split only a top-level string
  • false: never split strings, only wrap
  • positive integer: split strings found at that array depth
  • -1: split strings at any depth

flatten: true promotes split values into the surrounding array. flatten: false keeps split values nested at the point they arose.



57
58
59
60
61
# File 'lib/jekyll-paginate-v3/support/string_array.rb', line 57

def interpret(value, split: true, flatten: false, delimiter: UNSET)
	active_delimiter = effective_delimiter(delimiter)
	active_split_depth = normalise_split_depth(split)
	interpret_value(value, split_depth: active_split_depth, flatten: !!flatten, current_level: 0, delimiter: active_delimiter)
end

#with(delimiter: UNSET) ⇒ Object

Builds a clone with selected configuration overrides.



26
27
28
29
30
# File 'lib/jekyll-paginate-v3/support/string_array.rb', line 26

def with(delimiter: UNSET)
	self.class.new(
		delimiter: delimiter.equal?(UNSET) ? @delimiter : delimiter
	)
end