Sha256: bbd947622c679ee7100e29ef8272889907f0b7f7de96f7f4931fe93ea8edd8b1

Contents?: true

Size: 878 Bytes

Versions: 4

Compression:

Stored size: 878 Bytes

Contents

require 'delegate'

module Mashed
  module ExtendHash
    def stringify
      StringyHash.new(dup.each_with_object({}) do |(k,v), h|
        v = v.stringify if v.respond_to?(:stringify)
        h[k.to_s] = v
      end)
    end
  end

  class StringyHash < SimpleDelegator
    def stringify
      dup
    end

    def [](key)
      super(key.to_s)
    end

    def []=(key, value)
      super(key.to_s, value)
    end
    alias store []=

    def delete(key, &blk)
      super(key.to_s, &blk)
    end

    def merge(other_hash, &blk)
      super(other_hash.stringify, &blk)
    end

    def merge!(other_hash, &blk)
      super(other_hash.stringify, &blk)
    end

    def replace(other_hash, &blk)
      super(other_hash.stringify, &blk)
    end

    def update(other_hash, &blk)
      super(other_hash.stringify, &blk)
    end
  end
end

Hash.send :include, Mashed::ExtendHash

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mashed-0.9.1 lib/mashed/stringy_hash.rb
mashed-0.9.0 lib/mashed/stringy_hash.rb
mashed-0.5.1 lib/mashed/stringy_hash.rb
mashed-0.5.0 lib/mashed/stringy_hash.rb