Class: Blacksheep::Decorators::JsonTransformer
- Inherits:
-
ActionDecorator
- Object
- SimpleDelegator
- ActionDecorator
- Blacksheep::Decorators::JsonTransformer
- Defined in:
- lib/blacksheep/decorators/json_transformer.rb
Instance Attribute Summary collapse
-
#case ⇒ Object
readonly
Returns the value of attribute case.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
-
#as_transformed_action_result(obj) ⇒ Array, Hash
Transform the obj with key in snake_case to the source case.
- #call(params, current_user: nil, **options) ⇒ Object
-
#camelize_keys(obj) ⇒ Array, Hash
Camlize keys - but keep leading underscores.
- #perform(params, current_user: nil, **options, &block) ⇒ Object
-
#snakecase_keys(obj) ⇒ Array, Hash
Make all keys in the passed object snake_case.
-
#transform_params(params) ⇒ Array, Hash
Transform the params in the instance into snake_case - if detected - from source.
Methods inherited from ActionDecorator
#__class__, #class, #decorators_chain
Instance Attribute Details
#case ⇒ Object (readonly)
Returns the value of attribute case.
6 7 8 |
# File 'lib/blacksheep/decorators/json_transformer.rb', line 6 def case @case end |
#params ⇒ Object (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)
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, **) detect_case(params) transformed_params = self.transform_params(params) result = super(transformed_params, **) as_transformed_action_result(result) end |
#camelize_keys(obj) ⇒ Array, Hash
Camlize keys - but keep leading underscores
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, **, &block) detect_case(params) transformed_params = self.transform_params(params) result = super(transformed_params, current_user: current_user, **, &block) as_transformed_action_result(result) end |
#snakecase_keys(obj) ⇒ Array, Hash
Make all keys in the passed object 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.
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 |