Module: RedmineCrm::Liquid::Filters::Arrays
- Defined in:
- lib/redmine_crm/liquid/filters/arrays.rb
Instance Method Summary collapse
-
#first(array, count = 1) ⇒ Object
Get the first element of the passed in array.
-
#group_by(input, property) ⇒ Object
Group an array of items by a property.
-
#jsonify(input) ⇒ Object
Convert the input into json string.
- #pop(array, input = 1) ⇒ Object
- #push(array, input) ⇒ Object
- #shift(array, input = 1) ⇒ Object
-
#sort(input, property = nil, nils = "first") ⇒ Object
Sort an array of objects.
-
#tagged_with(input, tags, match = 'all') ⇒ Object
Filter an array of objects by tags.
- #unshift(array, input) ⇒ Object
-
#where(input, property, value, operator = '==') ⇒ Object
Filter an array of objects.
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/redmine_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/redmine_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/redmine_crm/liquid/filters/arrays.rb', line 22 def jsonify(input) as_liquid(input).to_json end |
#pop(array, input = 1) ⇒ Object
152 153 154 155 156 157 |
# File 'lib/redmine_crm/liquid/filters/arrays.rb', line 152 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
159 160 161 162 163 164 |
# File 'lib/redmine_crm/liquid/filters/arrays.rb', line 159 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
166 167 168 169 170 171 |
# File 'lib/redmine_crm/liquid/filters/arrays.rb', line 166 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
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/redmine_crm/liquid/filters/arrays.rb', line 132 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
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/redmine_crm/liquid/filters/arrays.rb', line 100 def tagged_with(input, , match='all') return input unless input.respond_to?(:select) input = input.values if input.is_a?(Hash) tag_list = .is_a?(Array) ? .sort : .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
173 174 175 176 177 178 |
# File 'lib/redmine_crm/liquid/filters/arrays.rb', line 173 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 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/redmine_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_value = item_property(object, property) item_property_value && item_property_value > value end || [] elsif operator == '<' input.select do |object| item_property_value = item_property(object, property) item_property_value && item_property_value < 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 || [] elsif operator == 'any' input.select do |object| item_property(object, property).present? end || [] elsif operator == 'none' input.select do |object| item_property(object, property).blank? end || [] else [] end end |