Module: CaseTransform

Defined in:
lib/case_transform.rb,
lib/case_transform/version.rb

Constant Summary collapse

VERSION =
'0.1'.freeze

Class Method Summary collapse

Class Method Details

.camel(value) ⇒ Object

Transforms values to UpperCamelCase or PascalCase.

@example:

"some_key" => "SomeKey",


14
15
16
17
18
19
20
21
22
# File 'lib/case_transform.rb', line 14

def camel(value)
  case value
  when Array then value.map { |item| camel(item) }
  when Hash then value.deep_transform_keys! { |key| camel(key) }
  when Symbol then camel(value.to_s).to_sym
  when String then value.underscore.camelize
  else value
  end
end

.camel_lower(value) ⇒ Object

Transforms values to camelCase.

@example:

"some_key" => "someKey",


28
29
30
31
32
33
34
35
36
# File 'lib/case_transform.rb', line 28

def camel_lower(value)
  case value
  when Array then value.map { |item| camel_lower(item) }
  when Hash then value.deep_transform_keys! { |key| camel_lower(key) }
  when Symbol then camel_lower(value.to_s).to_sym
  when String then value.underscore.camelize(:lower)
  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",


43
44
45
46
47
48
49
50
51
# File 'lib/case_transform.rb', line 43

def dash(value)
  case value
  when Array then value.map { |item| dash(item) }
  when Hash then value.deep_transform_keys! { |key| dash(key) }
  when Symbol then dash(value.to_s).to_sym
  when String then value.underscore.dasherize
  else value
  end
end

.unaltered(value) ⇒ Object

Returns the value unaltered



69
70
71
# File 'lib/case_transform.rb', line 69

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",


58
59
60
61
62
63
64
65
66
# File 'lib/case_transform.rb', line 58

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