Sha256: 70db22956702d66bd79438a4088c1c8fddfa736dbb9745b2af5f42872f8d52fe
Contents?: true
Size: 1.25 KB
Versions: 3
Compression:
Stored size: 1.25 KB
Contents
class CleanHash def self.pollute! ::Hash.class_eval do # merge but to not overwrite keys def deep_merge hash klass = ::CleanHash::DeepMerge klass[deep_clone].deep_merge klass[hash] end # true clone of the hash with 0 references to the old one def deep_clone Marshal.load(Marshal.dump(self)) end def to_ch mode=nil if mode.is_a?(Array) for key in keys raise ArgumentError, 'CleanHash key "%s" not allowed' % key unless mode.include?(key) end for key in mode self[key] = nil unless key?(key) end ::CleanHash.new self, :strict else supported = %i{safe mutex strict} raise ArgumentError, 'Unsupported type "%s", supported: %s' % [mode, supported] if mode && !supported.include?(mode) if mode == :mutex ::CleanHash::MutexHash.new self else ::CleanHash.new self, mode end end end end ::Array.class_eval do # coverts keys to empty hash methods and returns read_only hash def to_ch strict=false hash = inject({}) { |h, key| h[key] = nil; h } ::CleanHash.new hash, :strict end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
clean-hash-0.4.0 | ./lib/clean-hash/hash_pollute.rb |
clean-hash-0.3.1 | ./lib/clean-hash/hash_pollute.rb |
clean-hash-0.3.0 | ./lib/clean-hash/hash_pollute.rb |