Sha256: 951e4f982892d52abfa55ff5e02507a13bdfac0a052fde72d2de1c9ed0144156

Contents?: true

Size: 782 Bytes

Versions: 1

Compression:

Stored size: 782 Bytes

Contents

require_relative "nested_hash_cleaner/version"

module NestedHashCleaner
  module_function

  def clean(x,key)
    case x
    when Hash then x = x.inject({}) {|m, (k, v)| m[k] = clean(v,key) unless k == key ; m }
    when Array then x.map! {|e| clean(e,key)}
    end
    x
  end

  def clean_with_value(x, value)
    case x
    when Hash then x = x.inject({}) {|m, (k, v)| m[k] = clean_with_value(v,value) unless v == value ; m }
    when Array then x.map! {|e| clean_with_value(e,value)}.compact!
    else x = ((x.respond_to?(:empty?) && x.empty?) ? nil : x)
    end
    x
  end

  def clean_with_values(x, values)
    values.each do |value|
      x = clean_with_value(x, value)
    end
    x
  end

  def clean_empty_values(x)
    clean_with_values(x, [nil, {}, [], ''])
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nested_hash_cleaner-0.2.0 lib/nested_hash_cleaner.rb