Module: YeshouaCrm::Liquid::Filters::Arrays

Defined in:
lib/yeshoua_crm/liquid/filters/arrays.rb

Instance Method Summary collapse

Instance Method Details

#first(array, count = 1) ⇒ Object

Get the first element of the passed in array

Example:

{{ product.images | first | to_img }}

{{ product.images | first: 3 }}


13
14
15
# 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) ⇒ Object

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"


34
35
36
37
38
39
40
41
42
43
44
45
46
# 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) ⇒ Object

Convert the input into json string

input - The Array or Hash to be converted

Returns the converted json string



22
23
24
# File 'lib/yeshoua_crm/liquid/filters/arrays.rb', line 22

def jsonify(input)
  as_liquid(input).to_json
end

#pop(array, input = 1) ⇒ Object



142
143
144
145
146
147
# 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) ⇒ Object



149
150
151
152
153
154
# 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) ⇒ Object



156
157
158
159
160
161
# 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") ⇒ Object

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# 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') ⇒ Object

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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# 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) ⇒ Object



163
164
165
166
167
168
# 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 = '==') ⇒ Object

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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