Module: DeepHashTransformer::ElementOperation

Defined in:
lib/deep_hash_transformer/element_operation.rb

Class Method Summary collapse

Class Method Details

.camel_case(val) ⇒ Object



6
7
8
9
# File 'lib/deep_hash_transformer/element_operation.rb', line 6

def camel_case(val)
  pascal_case(val)
    .sub(/^[A-Z]/, &:downcase)
end

.dasherize(val) ⇒ Object



11
12
13
14
# File 'lib/deep_hash_transformer/element_operation.rb', line 11

def dasherize(val)
  stringify(val)
    .tr("_", "-")
end

.identity(val) ⇒ Object



16
17
18
# File 'lib/deep_hash_transformer/element_operation.rb', line 16

def identity(val)
  val
end

.pascal_case(val) ⇒ Object



20
21
22
23
24
25
# File 'lib/deep_hash_transformer/element_operation.rb', line 20

def pascal_case(val)
  stringify(val)
    .split(/(?=[A-Z])|[-_]/)
    .map(&:capitalize)
    .join
end

.snake_case(val) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/deep_hash_transformer/element_operation.rb', line 40

def snake_case(val)
  stringify(val)
    .gsub(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .tr("-", "_")
    .downcase
end

.stringify(val) ⇒ Object



27
28
29
# File 'lib/deep_hash_transformer/element_operation.rb', line 27

def stringify(val)
  val.to_s
end

.symbolize(val) ⇒ Object



31
32
33
# File 'lib/deep_hash_transformer/element_operation.rb', line 31

def symbolize(val)
  val.to_sym
end

.underscore(val) ⇒ Object



35
36
37
38
# File 'lib/deep_hash_transformer/element_operation.rb', line 35

def underscore(val)
  stringify(val)
    .tr("-", "_")
end