Module: Jekyll::Filters::Arrays
- Defined in:
- lib/jekyll/filters/arrays.rb
Instance Method Summary collapse
-
#concat_or_append(input, concatenable) ⇒ Object
Join arrays or append item to array.
-
#flatten(input, level = nil) ⇒ Array
Flattens nested arrays.
-
#infinite_next(input, posts, amount = 1) ⇒ Array<Jekyll::Document,Drop>
Finds the next posts in a collection, starting from current post and restarts from the beginning to always return posts.
-
#infinite_prev(input, posts, amount = 1) ⇒ Array<Jekyll::Document,Drop>
Finds the previous posts in a collection, starting from current post and restarts from the beginning to always return posts.
-
#next(input, posts) ⇒ Drop?
Return next post from an array or nothing if post is last.
-
#prev(input, posts) ⇒ Drop?
(also: #previous)
Return previous post from an array, or nothing if post is the first item.
-
#sample(input, amount = 1) ⇒ Array
Returns one or several random items from an Array.
-
#shuffle(input) ⇒ Array
Returns list of random items from an Array.
Instance Method Details
#concat_or_append(input, concatenable) ⇒ Object
Join arrays or append item to array
51 52 53 54 55 56 57 58 59 |
# File 'lib/jekyll/filters/arrays.rb', line 51 def concat_or_append(input, concatenable) input = [] unless array? input if concatenable.is_a? Array input + concatenable else input.dup << concatenable end end |
#flatten(input, level = nil) ⇒ Array
Flattens nested arrays
38 39 40 |
# File 'lib/jekyll/filters/arrays.rb', line 38 def flatten(input, level = nil) [input].flatten(level) end |
#infinite_next(input, posts, amount = 1) ⇒ Array<Jekyll::Document,Drop>
Finds the next posts in a collection, starting from current post and restarts from the beginning to always return posts.
70 71 72 73 74 75 76 77 |
# File 'lib/jekyll/filters/arrays.rb', line 70 def infinite_next(input, posts, amount = 1) posts = [] unless array? posts liquid_input = input.to_liquid liquid_posts = posts.map(&:to_liquid) index = find_in_stack(liquid_input, liquid_posts) liquid_posts.rotate(index).slice(1, amount) if index end |
#infinite_prev(input, posts, amount = 1) ⇒ Array<Jekyll::Document,Drop>
Finds the previous posts in a collection, starting from current post and restarts from the beginning to always return posts.
88 89 90 91 92 93 94 95 |
# File 'lib/jekyll/filters/arrays.rb', line 88 def infinite_prev(input, posts, amount = 1) posts = [] unless array? posts liquid_input = input.to_liquid liquid_posts = posts.map(&:to_liquid) index = find_in_stack(liquid_input, liquid_posts) liquid_posts.rotate(index).reverse.slice(0, amount).reverse if index end |
#next(input, posts) ⇒ Drop?
Return next post from an array or nothing if post is last.
104 105 106 107 108 109 110 111 |
# File 'lib/jekyll/filters/arrays.rb', line 104 def next(input, posts) posts = [] unless array? posts liquid_input = input.to_liquid liquid_posts = posts.map(&:to_liquid) index = find_in_stack(liquid_input, liquid_posts) liquid_posts[index + 1] if index end |
#prev(input, posts) ⇒ Drop? Also known as: previous
Return previous post from an array, or nothing if post is the first item.
122 123 124 125 126 127 128 129 |
# File 'lib/jekyll/filters/arrays.rb', line 122 def prev(input, posts) posts = [] unless array? posts liquid_input = input.to_liquid liquid_posts = posts.map(&:to_liquid) index = find_in_stack(liquid_input, liquid_posts) liquid_posts[index - 1] if index&.positive? end |
#sample(input, amount = 1) ⇒ Array
Returns one or several random items from an Array.
14 15 16 17 18 |
# File 'lib/jekyll/filters/arrays.rb', line 14 def sample(input, amount = 1) input = [] unless array? input input.sample(amount) end |
#shuffle(input) ⇒ Array
Returns list of random items from an Array.
26 27 28 29 |
# File 'lib/jekyll/filters/arrays.rb', line 26 def shuffle(input) input = [] unless array? input input.shuffle end |