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.
-
#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) ⇒ Any
Returns one or several random items from an Array.
Instance Method Details
#concat_or_append(input, concatenable) ⇒ Object
Join arrays or append item to array
29 30 31 32 33 34 35 36 37 |
# File 'lib/jekyll/filters/arrays.rb', line 29 def concat_or_append(input, concatenable) input = [] unless array? input if concatenable.is_a? Array input + concatenable else input.dup << concatenable end 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.
48 49 50 51 52 53 54 55 |
# File 'lib/jekyll/filters/arrays.rb', line 48 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.
66 67 68 69 70 71 72 73 |
# File 'lib/jekyll/filters/arrays.rb', line 66 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.
82 83 84 85 86 87 88 89 |
# File 'lib/jekyll/filters/arrays.rb', line 82 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.
100 101 102 103 104 105 106 107 |
# File 'lib/jekyll/filters/arrays.rb', line 100 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) ⇒ Any
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 |