class HashKeyTransformer

Constants

VERSION

Public Class Methods

camel_to_underscore(key, options = {}) click to toggle source

'keyName' -> 'key_name'

# File lib/hash_key_transformer.rb, line 23
def camel_to_underscore(key, options = {})
  key.to_s.gsub(/([A-Z])/) { "_#{$1}" }.downcase
end
dash_to_underscore(key, options = {}) click to toggle source

'key-name' -> 'key_name'

# File lib/hash_key_transformer.rb, line 38
def dash_to_underscore(key, options = {})
  key.to_s.downcase.gsub(/-([a-z0-9])/) { "_#{$1}" }
end
transform_camel_to_underscore(subject, options={}) click to toggle source
# File lib/hash_key_transformer.rb, line 6
def transform_camel_to_underscore(subject, options={})
  deep_transform_hash_keys(subject, :camel_to_underscore, options)
end
transform_dash_to_underscore(subject, options={}) click to toggle source
# File lib/hash_key_transformer.rb, line 14
def transform_dash_to_underscore(subject, options={})
  deep_transform_hash_keys(subject, :dash_to_underscore, options)
end
transform_underscore_to_camel(subject, options={}) click to toggle source
# File lib/hash_key_transformer.rb, line 10
def transform_underscore_to_camel(subject, options={})
  deep_transform_hash_keys(subject, :underscore_to_camel, options)
end
transform_underscore_to_dash(subject, options={}) click to toggle source
# File lib/hash_key_transformer.rb, line 18
def transform_underscore_to_dash(subject, options={})
  deep_transform_hash_keys(subject, :underscore_to_dash, options)
end
underscore_to_camel(key, options = {}) click to toggle source

'key_name' -> 'keyName'

# File lib/hash_key_transformer.rb, line 28
def underscore_to_camel(key, options = {})
  if !!options[:keep_lead_underscore]
    key.to_s.gsub(/(?!^)_([a-z0-9])/) { $1.upcase }
  else
    first_word, *rest_words = key.to_s.split('_').reject(&:empty?).map(&:downcase)
    ([first_word] + rest_words.map(&:capitalize)).join
  end
end
underscore_to_dash(key, options = {}) click to toggle source

'key_name' -> 'key-name'

# File lib/hash_key_transformer.rb, line 43
def underscore_to_dash(key, options = {})
  key.to_s.downcase.gsub(/_([a-z0-9])/) { "-#{$1}" }
end

Private Class Methods

deep_transform_hash_keys(subject, output_key_strategy, options = {}) click to toggle source

Recursively walk the JSON-like data structure (hash, array) and transform the hash keys using the given strategy

# File lib/hash_key_transformer.rb, line 50
def deep_transform_hash_keys(subject, output_key_strategy, options = {})
  if subject.is_a?(Hash)
    subject.inject({}) do |memo, (key, value)|
      new_key = send(output_key_strategy, key, options).to_sym
      memo[new_key] = deep_transform_hash_keys(value, output_key_strategy, options)
      memo
    end
  elsif subject.is_a?(Array)
    subject.inject([]) do |memo, item|
      memo << deep_transform_hash_keys(item, output_key_strategy, options)
      memo
    end
  else
    # not a hash or array - do nothing
    subject
  end
end