class Sparrow::Strategies::KeyTransformation::CamelizeKey

Strategy class for converting JSON to a camelized format. Meaning snake_case => snakeCase

Attributes

camelize_ignore_uppercase_keys[RW]

@return [Boolean] Defines whether complete uppercased keys will be

transformed

@see initialize

strategy[RW]

@return [Symbol] the camelizing strategy @see initialize

Public Class Methods

new(options = {}) click to toggle source

Initialize a new CamelizeKey strategy @param [Hash] options camelize options @option options [Symbol] :strategy ('lower') defines the camelizing

strategy type. Defines how to camelize, which comes down to starting
with a lowercased character or with an uppercased character.
Thus possible values are :lower and :upper.

@option options [Boolean] :camelize_ignore_uppercase_keys (true)

Defines if already completely uppercased keys should not be
transformed. I.e. JSON stays JSON if
this is set to true. If it is set to false JSON will be transformed
to Json.
# File lib/sparrow/strategies/key_transformation/camelize_key.rb, line 32
def initialize(options = {})
  self.strategy                       = options.fetch(:strategy, :lower)
  self.camelize_ignore_uppercase_keys =
      options.fetch(:camelize_ignore_uppercase_keys, true)
end

Public Instance Methods

transform_key(key) click to toggle source

Transform the given key to camelCase based on the configuration options set on initialization. @see initialize @param [String, to_s] key the key value to be transformed @return [String] the transformed key

# File lib/sparrow/strategies/key_transformation/camelize_key.rb, line 44
def transform_key(key)
  key = key.to_s
  if camelize_ignore_uppercase_keys && key.upcase == key
    key
  else
    key.camelize(strategy)
  end
end