Sha256: ce6eff0e94c82f8cdbd40e53ee57a15b42fba087cb4789fb5313a26356f88708

Contents?: true

Size: 861 Bytes

Versions: 4

Compression:

Stored size: 861 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

4 entries across 4 versions & 1 rubygems

Version Path
phrase-0.1.2 lib/phrase/extensions/hash.rb
phrase-0.1.1 lib/phrase/extensions/hash.rb
phrase-0.1 lib/phrase/extensions/hash.rb
phrase-0.0.8 lib/phrase/extensions/hash.rb