Module: CaseTransform

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

Constant Summary collapse

VERSION =
'0.2'.freeze

Class Method Summary collapse

Class Method Details

.camel(value) ⇒ Object

Transforms values to UpperCamelCase or PascalCase.

@example:

"some_key" => "SomeKey",


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

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 camel_cache[value] ||= value.underscore.camelize
  else value
  end
end

.camel_cacheObject



9
10
11
# File 'lib/case_transform.rb', line 9

def camel_cache
  @camel_cache ||= {}
end

.camel_lower(value) ⇒ Object

Transforms values to camelCase.

@example:

"some_key" => "someKey",


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

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 camel_lower_cache[value] ||= value.underscore.camelize(:lower)
  else value
  end
end

.camel_lower_cacheObject



13
14
15
# File 'lib/case_transform.rb', line 13

def camel_lower_cache
  @camel_lower_cache ||= {}
end

.dash(value) ⇒ Object

Transforms values to dashed-case. This is the default case for 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 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 dash_cache[value] ||= value.underscore.dasherize
  else value
  end
end

.dash_cacheObject



17
18
19
# File 'lib/case_transform.rb', line 17

def dash_cache
  @dash_cache ||= {}
end

.unaltered(value) ⇒ Object

Returns the value unaltered



84
85
86
# File 'lib/case_transform.rb', line 84

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


73
74
75
76
77
78
79
80
81
# File 'lib/case_transform.rb', line 73

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 underscore_cache[value] ||= value.underscore
  else value
  end
end

.underscore_cacheObject



21
22
23
# File 'lib/case_transform.rb', line 21

def underscore_cache
  @underscore_cache ||= {}
end