module YeshouaCrm::Liquid::Filters::Arrays

Public Instance Methods

first(array, count=1) click to toggle source

Get the first element of the passed in array

Example:

{{ product.images | first | to_img }}

{{ product.images | first: 3 }}
# File lib/yeshoua_crm/liquid/filters/arrays.rb, line 13
def first(array, count=1)
  (count > 1 ? array.first(count) : array.first) if array.respond_to?(:first)
end
group_by(input, property) click to toggle source

Group an array of items by a property

input - the inputted Enumerable property - the property

Returns an array of Hashes, each looking something like this:

{"name"  => "larry"
 "items" => [...] } # all the items where `property` == "larry"
# File lib/yeshoua_crm/liquid/filters/arrays.rb, line 34
def group_by(input, property)
  if groupable?(input)
    input.group_by { |item| item_property(item, property).to_s }.each_with_object([]) do |item, array|
        array << {
          "name"  => item.first,
          "items" => item.last,
          "size"  => item.last.size
        }
      end
  else
    input
  end
end
jsonify(input) click to toggle source

Convert the input into json string

input - The Array or Hash to be converted

Returns the converted json string

# File lib/yeshoua_crm/liquid/filters/arrays.rb, line 22
def jsonify(input)
  as_liquid(input).to_json
end
pop(array, input = 1) click to toggle source
# File lib/yeshoua_crm/liquid/filters/arrays.rb, line 142
def pop(array, input = 1)
  return array unless array.is_a?(Array)
  new_ary = array.dup
  new_ary.pop(input.to_i || 1)
  new_ary
end
push(array, input) click to toggle source
# File lib/yeshoua_crm/liquid/filters/arrays.rb, line 149
def push(array, input)
  return array unless array.is_a?(Array)
  new_ary = array.dup
  new_ary.push(input)
  new_ary
end
shift(array, input = 1) click to toggle source
# File lib/yeshoua_crm/liquid/filters/arrays.rb, line 156
def shift(array, input = 1)
  return array unless array.is_a?(Array)
  new_ary = array.dup
  new_ary.shift(input.to_i || 1)
  new_ary
end
sort(input, property = nil, nils = "first") click to toggle source

Sort an array of objects

input - the object array property - property within each object to filter by nils ('first' | 'last') - nils appear before or after non-nil values

Returns the filtered array of objects

# File lib/yeshoua_crm/liquid/filters/arrays.rb, line 122
def sort(input, property = nil, nils = "first")
  if input.nil?
    raise ArgumentError, "Cannot sort a null object."
  end
  if property.nil?
    input.sort
  else
    if nils == "first"
      order = - 1
    elsif nils == "last"
      order = + 1
    else
      raise ArgumentError, "Invalid nils order: " \
        "'#{nils}' is not a valid nils order. It must be 'first' or 'last'."
    end

    sort_input(input, property, order)
  end
end
tagged_with(input, tags, match='all') click to toggle source

Filter an array of objects by tags

input - the object array tags - quoted tags list divided by comma match - (all- defaut, any, exclude)

Returns the filtered array of objects

# File lib/yeshoua_crm/liquid/filters/arrays.rb, line 90
def tagged_with(input, tags, match='all')
  return input unless input.respond_to?(:select)
  input = input.values if input.is_a?(Hash)
  tag_list = input.is_a?(Array) ? tags.sort : tags.split(',').map(&:strip).sort
  case match
  when "all"
    input.select do |object|
      object.respond_to?(:tag_list) &&
      (tag_list - Array(item_property(object, 'tag_list')).map(&:to_s).sort).empty?
    end || []
  when "any"
    input.select do |object|
      object.respond_to?(:tag_list) &&
      (tag_list & Array(item_property(object, 'tag_list')).map(&:to_s).sort).any?
    end || []
  when "exclude"
    input.select do |object|
      object.respond_to?(:tag_list) &&
      (tag_list & Array(item_property(object, 'tag_list')).map(&:to_s).sort).empty?
    end || []
  else
    []
  end
end
unshift(array, input) click to toggle source
# File lib/yeshoua_crm/liquid/filters/arrays.rb, line 163
def unshift(array, input)
  return array unless array.is_a?(Array)
  new_ary = array.dup
  new_ary.unshift(input)
  new_ary
end
where(input, property, value, operator='==') click to toggle source

Filter an array of objects

input - the object array property - property within each object to filter by value - desired value

Returns the filtered array of objects

# File lib/yeshoua_crm/liquid/filters/arrays.rb, line 55
def where(input, property, value, operator='==')
  return input unless input.respond_to?(:select)
  input = input.values if input.is_a?(Hash)
  if operator == '=='
    input.select do |object|
      Array(item_property(object, property)).map(&:to_s).include?(value.to_s)
    end || []
  elsif operator == '<>'
    input.select do |object|
      !Array(item_property(object, property)).map(&:to_s).include?(value.to_s)
    end || []
  elsif operator == '>'
    input.select do |object|
      item_property(object, property) > value
    end || []
  elsif operator == '<'
    input.select do |object|
      item_property(object, property) < value
    end || []
  elsif operator == 'match'
    input.select do |object|
      Array(item_property(object, property)).map(&:to_s).any?{|i| i.match(value.to_s)}
    end || []
  else
    []
  end
end