Sha256: 4e07c571c7622088cafc4f7e7a5001606d498d1b8696709938cb9aac6bc8cbcf

Contents?: true

Size: 860 Bytes

Versions: 7

Compression:

Stored size: 860 Bytes

Contents

# -*- encoding : utf-8 -*-
module Phrase::Extensions::Hash
  extend ActiveSupport::Concern
  
  module InstanceMethods
    def to_shallow_hash
      self.inject({}) do |shallow_hash, (key, value)|
        if value.is_a?(Hash)
          value.to_shallow_hash.each do |sub_key, sub_value|
            shallow_hash[[key, sub_key].join(".")] = sub_value
          end
        else
          shallow_hash[key.to_s] = value
        end
        shallow_hash
      end
    end
    
    def deep_stringify_keys
      inject({}) { |result, (key, value)|
        value = value.deep_stringify_keys if value.is_a?(Hash)
        unless value.is_a? Proc
          result[(key.to_s rescue key) || key] = value
        else
          result[(key.to_s rescue key) || key] = nil
        end
        result
      }
    end 
  end
end

Hash.send(:include, Phrase::Extensions::Hash)

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
phrase-0.0.7 lib/phrase/extensions/hash.rb
phrase-0.0.6 lib/phrase/extensions/hash.rb
phrase-0.0.5 lib/phrase/extensions/hash.rb
phrase-0.0.4 lib/phrase/extensions/hash.rb
phrase-0.0.3 lib/phrase/extensions/hash.rb
phrase-0.0.2 lib/phrase/extensions/hash.rb
phrase-0.0.1 lib/phrase/extensions/hash.rb