Module: PipeDream::Utils

Included in:
Request
Defined in:
lib/pipe_dream/utils.rb

Instance Method Summary collapse

Instance Method Details

#camelize_keys(hash) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/pipe_dream/utils.rb', line 4

def camelize_keys(hash)
  hash.tap do |h|
    affected_keys = h.each_key.select { |key| key.to_s.include?('_') }
    return h if affected_keys.empty?

    camelized_keys = affected_keys.map do |key|
      key.to_s.split('_').map.with_index do |k, index|
        next k if index.zero?
        k.capitalize
      end.join.to_sym
    end

    Hash[affected_keys.zip(camelized_keys)].each do |old, new|
      h[new] = h.delete(old)
    end
  end
end