Skip to content

Instantly share code, notes, and snippets.

@Jetroid Jetroid/union.rb
Created Jul 29, 2016

Embed
What would you like to do?
A plugin to allow union of arrays with Jekyll.
module Jekyll
module UnionFilter
def union(input, array)
unless array.respond_to?(:to_ary)
array = Array.new
end
InputIterator.new(input).concat(array).uniq
end
class InputIterator
include Enumerable
def initialize(input)
@input = if input.is_a?(Array)
input.flatten
elsif input.is_a?(Hash)
[input]
elsif input.is_a?(Enumerable)
input
else
Array(input)
end
end
def concat(args)
to_a.concat(args)
end
def each
@input.each do |e|
yield(e.respond_to?(:to_liquid) ? e.to_liquid : e)
end
end
end
end
end
Liquid::Template.register_filter(Jekyll::UnionFilter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.