Sha256: 222ab2028afac75687b2f1877ec05b81108b7b3c9e9fc4a29f5c10a3237b234e

Contents?: true

Size: 1009 Bytes

Versions: 1

Compression:

Stored size: 1009 Bytes

Contents

require 'delegate'

module Mashed
  class StringyHash < SimpleDelegator
    def self.stringify(object)
      if object.is_a?(Array)
        object.map { |value| StringyHash.stringify(value) }
      elsif object.is_a?(Hash)
        StringyHash.new(object.each_with_object({}) do |(k,v), h|
          h[k.to_s] = StringyHash.stringify(v)
        end)
      else
        object
      end
    end

    def stringify
      dup
    end

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

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

    def key?(key)
      super(key.to_s)
    end

    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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mashed-1.0.1 lib/mashed/stringy_hash.rb