module BananaBits module Concerns module Hashable def remove_blank_values(collection) if collection.is_a?(Hash) collection.each_with_object({}) do |(k, v), new_hash| if v.blank? new_hash.delete(k) else if v.is_a?(Hash) new_hash[k] = remove_blank_values(v) elsif v.is_a?(Array) new_hash[k] = remove_blank_values_from_array!(v) else new_hash[k] = v unless collection.blank? end end end elsif collection.is_a?(Array) remove_blank_values_from_array(collection) end end def remove_blank_values!(collection) if collection.is_a?(Hash) collection.each_pair do |k, v| if v.blank? collection.delete(k) elsif v.is_a?(Hash) remove_blank_values!(v) collection.delete(k) if collection[k].blank? elsif v.is_a?(Array) remove_blank_values_from_array!(v) end end elsif collection.is_a?(Array) remove_blank_values_from_array!(collection) end return collection end def remove_blank_values_from_array(arr) arr.map{|o| remove_blank_values(o)} end def remove_blank_values_from_array!(arr) arr.map{|o| remove_blank_values!(o)} end def symbolize_keys(collection) collection.inject({}){|result,(k,v)| result[k.to_sym] = v; result} end end end end