class Blacksheep::Decorators::JsonTransformer
Attributes
Public Instance Methods
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
# 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
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
# 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
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 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 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
# 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