Sha256: f4dbc997159586ff39afff3e1d7b71893be149a8883310ef0ac28786e870c727

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

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(self.class.stringify(other_hash), &blk)
    end

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

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

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