Module: Fattura24::Utils

Defined in:
lib/fattura24/utils.rb

Class Method Summary collapse

Class Method Details

.crush(obj) ⇒ Object

This function deeply removes any nil object/value from objects.



8
9
10
11
12
13
14
# File 'lib/fattura24/utils.rb', line 8

def self.crush(obj)
  return crush_hash(obj) if obj.is_a?(Hash)

  return crush_array(obj) if obj.is_a?(Array)

  obj
end

.crush_array(array) ⇒ Object

Deeply removes any nil value from arrays.



18
19
20
21
22
23
24
# File 'lib/fattura24/utils.rb', line 18

def self.crush_array(array)
  r = array.map do |obj|
    crush(obj)
  end.compact

  r.empty? ? nil : r
end

.crush_hash(hash) ⇒ Object

Deeply removes any nil value from hashes.



28
29
30
31
32
33
34
35
36
# File 'lib/fattura24/utils.rb', line 28

def self.crush_hash(hash)
  r = hash.each_with_object({}) do |(k, v), h|
    if (crushed_v = crush(v))
      h[k] = crushed_v
    end
  end

  r.empty? ? nil : r
end