Class: Blacksheep::Decorators::JsonTransformer

Inherits:
ActionDecorator
  • Object
show all
Defined in:
lib/blacksheep/decorators/json_transformer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ActionDecorator

#__class__, #class, #decorators_chain

Instance Attribute Details

#caseObject (readonly)

Returns the value of attribute case.



6
7
8
# File 'lib/blacksheep/decorators/json_transformer.rb', line 6

def case
  @case
end

#paramsObject (readonly)

Returns the value of attribute params.



6
7
8
# File 'lib/blacksheep/decorators/json_transformer.rb', line 6

def params
  @params
end

Instance Method Details

#as_transformed_action_result(obj) ⇒ Array, Hash

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

Parameters:

Returns:

  • (Array, Hash)

    The rsult structure with keys converted to source caseing

See Also:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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) ⇒ Object



8
9
10
11
12
13
14
15
16
# 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) ⇒ Array, Hash

Camlize keys - but keep leading underscores

Parameters:

  • obj (Array, hash)

Returns:

  • (Array, Hash)

    The passed in obj with keys transferred to camel_case.



84
85
86
87
88
89
90
91
92
# 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) ⇒ Object



18
19
20
21
22
23
24
25
26
# 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) ⇒ Array, Hash

Make all keys in the passed object snake_case

Parameters:

  • obj (Array, Hash)

Returns:

  • (Array, Hash)

    The source obj with kyes transformed into snake_case



75
76
77
# 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) ⇒ Array, Hash

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

Returns:

  • (Array, Hash)

    The params converted into snake_case

See Also:



34
35
36
37
38
39
40
41
42
43
# 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