module Fattura24::Utils

Public Class Methods

crush(obj) click to toggle source

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

# 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) click to toggle source

Deeply removes any nil value from arrays.

# 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) click to toggle source

Deeply removes any nil value from hashes.

# 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