Module: Securial::Helpers::KeyTransformer
- Extended by:
- KeyTransformer
- Included in:
- KeyTransformer
- Defined in:
- lib/securial/helpers/key_transformer.rb
Overview
Transforms hash keys between different naming conventions.
This module provides methods to convert between snake_case (Ruby convention), camelCase (JavaScript convention), and PascalCase (C# convention) for API response formatting. It supports deep transformation of nested structures.
Class Method Summary collapse
-
.deep_transform_keys(obj) {|key| ... } ⇒ Object
Recursively transforms all keys in a nested data structure.
-
.underscore(str) ⇒ String
Converts a camelCase or PascalCase string to snake_case.
Instance Method Summary collapse
-
#camelize(str, format) ⇒ String
Converts a string to camelCase or PascalCase format.
Class Method Details
.deep_transform_keys(obj) {|key| ... } ⇒ Object
Recursively transforms all keys in a nested data structure.
Applies a key transformation block to all hash keys in a deeply nested structure containing hashes, arrays, and other objects. The transformation preserves the structure while only modifying the keys.
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/securial/helpers/key_transformer.rb', line 127 def self.deep_transform_keys(obj, &block) case obj when Hash obj.transform_keys(&block).transform_values { |v| deep_transform_keys(v, &block) } when Array obj.map { |e| deep_transform_keys(e, &block) } else obj end end |
.underscore(str) ⇒ String
Converts a camelCase or PascalCase string to snake_case.
Transforms camelCase or PascalCase strings back to Ruby’s snake_case convention. Useful for converting API input keys to Ruby conventions.
81 82 83 |
# File 'lib/securial/helpers/key_transformer.rb', line 81 def self.underscore(str) str.to_s.underscore end |
Instance Method Details
#camelize(str, format) ⇒ String
Converts a string to camelCase or PascalCase format.
Transforms snake_case strings into camelCase variants based on the specified format. Useful for converting Ruby hash keys to JavaScript or other language conventions.
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/securial/helpers/key_transformer.rb', line 54 def camelize(str, format) case format when :lowerCamelCase str.to_s.camelize(:lower) when :UpperCamelCase str.to_s.camelize else str.to_s end end |