Module: JSONAPIHelpers::KeyTransform

Defined in:
lib/jsonapi_helpers/support/key_transform.rb

Class Method Summary collapse

Class Method Details

.call(object, key_transform: nil) ⇒ Object



11
12
13
14
# File 'lib/jsonapi_helpers/support/key_transform.rb', line 11

def call(object, key_transform: nil)
  key_transform ||= JSONAPIHelpers.config.key_transform
  public_send(key_transform, object)
end

.camel(value) ⇒ Object

Transforms values to UpperCamelCase or PascalCase.

@example:

"some_key" => "SomeKey",


20
21
22
23
24
25
26
27
# File 'lib/jsonapi_helpers/support/key_transform.rb', line 20

def camel(value)
  case value
  when Hash then value.deep_transform_keys! { |key| camel(key) }
  when Symbol then camel(value.to_s).to_sym
  when String then StringSupport.camel(value)
  else value
  end
end

.camel_lower(value) ⇒ Object

Transforms values to camelCase.

@example:

"some_key" => "someKey",


33
34
35
36
37
38
39
40
# File 'lib/jsonapi_helpers/support/key_transform.rb', line 33

def camel_lower(value)
  case value
  when Hash then value.deep_transform_keys! { |key| camel_lower(key) }
  when Symbol then camel_lower(value.to_s).to_sym
  when String then StringSupport.camel_lower(value)
  else value
  end
end

.dash(value) ⇒ Object

Transforms values to dashed-case. This is the default case for the JsonApi adapter.

@example:

"some_key" => "some-key",


47
48
49
50
51
52
53
54
# File 'lib/jsonapi_helpers/support/key_transform.rb', line 47

def dash(value)
  case value
  when Hash then value.deep_transform_keys! { |key| dash(key) }
  when Symbol then dash(value.to_s).to_sym
  when String then StringSupport.dash(value)
  else value
  end
end

.unaltered(value) ⇒ Object

Returns the value unaltered



71
72
73
# File 'lib/jsonapi_helpers/support/key_transform.rb', line 71

def unaltered(value)
  value
end

.underscore(value) ⇒ Object

Transforms values to underscore_case. This is the default case for deserialization in the JsonApi adapter.

@example:

"some-key" => "some_key",


61
62
63
64
65
66
67
68
# File 'lib/jsonapi_helpers/support/key_transform.rb', line 61

def underscore(value)
  case value
  when Hash then value.deep_transform_keys! { |key| underscore(key) }
  when Symbol then underscore(value.to_s).to_sym
  when String then StringSupport.underscore(value)
  else value
  end
end