Sha256: 71a88967730e0f39e00e208b0a9ff5ee250f9c874da951b881b74b84601ce97e

Contents?: true

Size: 870 Bytes

Versions: 2

Compression:

Stored size: 870 Bytes

Contents

# Refine the Hash class with new methods and functionality.
module CarrotRpc::HashExtensions
  refine Hash do
    # Utility method to rename keys in a hash
    # @param [String] find the text to look for in a keys
    # @param [String] replace the text to replace the found text
    # @return [Hash] a new hash
    def rename_keys(find, replace, new_hash = {}) # rubocop:disable Metrics/MethodLength
      each do |k, v|
        new_key = k.to_s.gsub(find, replace)

        new_hash[new_key] = if v.is_a? Array
                              v.map { |t| t.rename_keys(find, replace) }
                            elsif v.is_a? Hash
                              v.rename_keys(find, replace)
                            else
                              v
                            end
      end

      new_hash
    end # rubocop:enable Metrics/MethodLength
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
carrot_rpc-1.1.0 lib/carrot_rpc/hash_extensions.rb
carrot_rpc-1.0.0 lib/carrot_rpc/hash_extensions.rb