Sha256: 7dc9ab6f09b0dcffbfb65d1df6c64b8c732015dd4f73c57d414fe120b80382e4

Contents?: true

Size: 1006 Bytes

Versions: 3

Compression:

Stored size: 1006 Bytes

Contents

class Hash
  def symbolize_keys!
    keys.each do |key|
      self[(key.to_sym rescue key) || key] = delete(key)
    end
    self
  end

  def symbolize_keys
    dup.symbolize_keys!
  end

  def deep_symbolize_keys!
    values.each do |val|
      val.deep_symbolize_keys! if val.is_a?(Hash)
    end
    symbolize_keys!
  end

  def deep_symbolize_keys
    copy = symbolize_keys
    copy.each do |key, val|
      copy[key] = val.deep_symbolize_keys if val.is_a?(Hash)
    end
    copy
  end

  def stringify_keys!
    keys.each do |key|
      self[key.to_s] = delete(key)
    end
    self
  end

  def stringify_keys
    dup.stringify_keys!
  end
end

if defined?(HashWithIndifferentAccess)
  class HashWithIndifferentAccess
    def to_hash
      Hash.new(default).merge(self).with_meta(meta)
    end

    def symbolize_keys
      to_hash.symbolize_keys!
    end

    def deep_symbolize_keys
      to_hash.deep_symbolize_keys!
    end

    def stringify_keys
      to_hash.stringify_keys!
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rupture-0.3.0 lib/rupture/rails_ext.rb
rupture-0.2.1 lib/rupture/rails_ext.rb
rupture-0.2.0 lib/rupture/rails_ext.rb