Sha256: 4d6a8a34f65553d5e514b1d175f355822588a15de585019360522474b31e32a4

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

module Searchgasm
  module CoreExt # :nodoc: all
    module Hash
      def deep_dup
        new_hash = {}
        
        self.each do |k, v|
          case v
          when Hash
            new_hash[k] = v.deep_dup
          else
            new_hash[k] = v
          end
        end
        
        new_hash
      end
      
      def deep_delete_duplicates(hash)
        hash.each do |k, v|
          if v.is_a?(Hash) && self[k]
            self[k].deep_delete_duplicates(v)
            self.delete(k) if self[k].blank?
          else
            self.delete(k)
          end
        end
        
        self
      end
      
      # assert_valid_keys was killing performance. Array.flatten was the culprit, so I rewrote this method, got a 35% performance increase
      def fast_assert_valid_keys(valid_keys)
        unknown_keys = keys - valid_keys
        raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
      end
    end
  end
end

Hash.send(:include, Searchgasm::CoreExt::Hash)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
searchgasm-1.2.0 lib/searchgasm/core_ext/hash.rb