Sha256: 4e2e1260d003f669584175e99aac528bce2b358e28fc770d87c01390472cb69d

Contents?: true

Size: 673 Bytes

Versions: 3

Compression:

Stored size: 673 Bytes

Contents

Hash.class_eval do

  def self.convert_keys(hash, method = :underscore)
    if method.is_a? Proc
      converter = method
    else
      converter = -> (str) { str.to_s.send method }
    end

    if hash.is_a?(Array)
      hash.collect {|h| convert_keys(h, converter) }
    elsif hash.is_a?(Hash)
      hash_array = hash.collect do |key,value|
        [ converter.call(key), convert_keys(value, converter) ]
      end

      Hash[hash_array]
    else
      hash
    end
  end

  def convert_keys(method = :underscore)
    Hash.convert_keys self, method
  end

end

Array.class_eval do

  def convert_keys(method = :underscore)
    Hash.convert_keys self, method
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hash-pipe-0.5.0 lib/hash_pipe/key_conversion.rb
hash-pipe-0.4.1 lib/hash_pipe/key_conversion.rb
hash-pipe-0.4.0 lib/hash_pipe/key_conversion.rb