Module: BananaBits::Concerns::Hashable

Included in:
BananaBits::Client
Defined in:
lib/banana_bits/concerns/hashable.rb

Instance Method Summary collapse

Instance Method Details

#remove_blank_values(collection) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/banana_bits/concerns/hashable.rb', line 4

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

#remove_blank_values!(collection) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/banana_bits/concerns/hashable.rb', line 24

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

#remove_blank_values_from_array(arr) ⇒ Object



42
43
44
# File 'lib/banana_bits/concerns/hashable.rb', line 42

def remove_blank_values_from_array(arr)
  arr.map{|o| remove_blank_values(o)}
end

#remove_blank_values_from_array!(arr) ⇒ Object



46
47
48
# File 'lib/banana_bits/concerns/hashable.rb', line 46

def remove_blank_values_from_array!(arr)
  arr.map{|o| remove_blank_values!(o)}
end

#symbolize_keys(collection) ⇒ Object



50
51
52
# File 'lib/banana_bits/concerns/hashable.rb', line 50

def symbolize_keys(collection)
  collection.inject({}){|result,(k,v)| result[k.to_sym] = v; result}
end