module Ecoportal module API module Common module GraphQL module HashHelpers module InstanceMethods def transform_keys_deep(value, &block) case value when Hash value.dup.each_with_object({}) do |(k, v), out| k_val = block_given?? yield(k) : k out[k_val] = keys_to_sym_deep(v) end when Enumerable value.map {|v| keys_to_sym_deep(v)} else value end end def keys_to_sym_deep(value) transform_keys_deep(value) {|k| k.to_sym} end def keys_to_s_deep(value) transform_keys_deep(value) {|k| k.to_s} end def remove_nil_keys_deep(value, target: []) target = [target].flatten.compact unless target.is_a?(Array) return value if target.empty? case value when Hash value.dup.each_with_object({}) do |(k, v), out| next unless v || !target.include?(k) out[k] = remove_nil_keys_deep(v, target: target) end when Enumerable value.map {|v| remove_nil_keys_deep(v, target: target)} else value end end def deep_dup(data) case data when Hash data.each_with_object({}) do |(k,v), copy| if k.is_a?(::String) || k.is_a?(::Symbol) copy[k] = deep_dup(v) else copy.delete(k) copy[deep_dup(k)] = deep_dup(v) end end when Enumerable data.map {|d| deep_dup(d)} else data.dup end end end module ClassMethods end class << self include InstanceMethods def included(base) base.send(:include, InstanceMethods) base.extend(ClassMethods) end end end end end end end