class Blacksheep::Decorators::JsonTransformer

@class Blacksheep::Decorators::JsonTransformer

Attributes

case[R]
params[R]

Public Instance Methods

as_transformed_action_result(obj) click to toggle source

Transform the obj with key in snake_case to the source case. NOTE: leading underscored are preserved (e.g. _my_laptop => _myLaptop)

@param obj [Array, Hash, ActionResult] A result structure @return [Array, Hash] The rsult structure with keys converted to source caseing @see camelize_keys

# File lib/blacksheep/decorators/json_transformer.rb, line 52
def as_transformed_action_result(obj)
  is_action_result, data = if obj.kind_of?(Blacksheep::ActionResult)
    [ true, obj.data ]
  else
    [ false, obj ]
  end
  converted_data = case @case
    when 'snake', 'as_is'
      data
    when 'camel'
      camelize_keys(data)
    else
      raise Blacksheep::Error, "unknown_case #{@case}"
  end

  is_action_result ? obj.set_data(converted_data) : ActionResult.new(converted_data, :ok)
end
call(params, current_user: nil, **options) click to toggle source
Calls superclass method
# File lib/blacksheep/decorators/json_transformer.rb, line 8
def call(params, current_user: nil, **options)
  detect_case(params)

  transformed_params = self.transform_params(params)

  result = super(transformed_params, **options)

  as_transformed_action_result(result)
end
camelize_keys(obj) click to toggle source

Camlize keys - but keep leading underscores

@param obj [Array, hash] … @return [Array, Hash] The passed in obj with keys transferred to camel_case.

# File lib/blacksheep/decorators/json_transformer.rb, line 84
def camelize_keys(obj)
  deep_transform_keys(obj) { |k|
    key = k.to_s
    match = key.match(/^(?<underscores>_*)(?<attribute>\w*)/)
    converted = match[:attribute].camelize(:lower)

    match[:underscores].present? ? "#{match[:underscores]}#{converted}" : converted
  }
end
perform(params, current_user: nil, **options, &block) click to toggle source
Calls superclass method
# File lib/blacksheep/decorators/json_transformer.rb, line 18
def perform(params, current_user: nil, **options, &block)
  detect_case(params)

  transformed_params = self.transform_params(params)

  result = super(transformed_params, current_user: current_user, **options, &block)

  as_transformed_action_result(result)
end
snakecase_keys(obj) click to toggle source

Make all keys in the passed object snake_case

@param obj [Array, Hash] … @return [Array, Hash] The source obj with kyes transformed into snake_case

# File lib/blacksheep/decorators/json_transformer.rb, line 75
def snakecase_keys(obj)
  deep_transform_keys(obj) { |k| k.to_s.underscore.to_sym }.with_indifferent_access
end
transform_params(params) click to toggle source

Transform the params in the instance into snake_case - if detected - from source.

@return [Array, Hash] The params converted into snake_case @see snakecase_keys

# File lib/blacksheep/decorators/json_transformer.rb, line 34
def transform_params(params)
  case @case
    when 'snake', 'as_is'
      params
    when 'camel'
      snakecase_keys(params)
    else
      raise Blacksheep::Error, "unknown_case #{@case}"
  end
end

Private Instance Methods

deep_transform_keys(obj) { |key| ... } click to toggle source

Deep transform the keys in the obj structure as descibed in the block passed

@param obj [Array, Hash] … @yield key [String] A block transforming the key passed into @yield_param key [String, Symbol] The key to be transformed @yield_return [String] The transfomed key @return [Array, Hash] The obj structure with transformed keys.

# File lib/blacksheep/decorators/json_transformer.rb, line 111
def deep_transform_keys(obj, &block)
  case obj
    when  Hash, ActionController::Parameters
      res = {}.with_indifferent_access
      obj.each do |key, value|
        res[yield(key)] = deep_transform_keys(value, &block)
      end
      res
    when Array
      obj.map{ |each| deep_transform_keys(each, &block) }
    else
      obj
  end
end
detect_case(params) click to toggle source
# File lib/blacksheep/decorators/json_transformer.rb, line 96
def detect_case(params)
  @params ||= params
  @case ||= params ? params.fetch(:_case, 'as_is').to_s : 'as_is'

  @case
end