Class: Inflector

Inherits:
Object
  • Object
show all
Defined in:
lib/bright_serializer/inflector.rb

Constant Summary collapse

CAMEL_REGEX =
/(_[a-zA-Z])/.freeze
UNDERSCORE_REGEX =
/(.)([A-Z])/.freeze

Class Method Summary collapse

Class Method Details

.camel(term) ⇒ Object



8
9
10
11
12
13
# File 'lib/bright_serializer/inflector.rb', line 8

def camel(term)
  return term.capitalize! unless term.include?('_')

  term.capitalize!
  term.gsub!(CAMEL_REGEX) { Regexp.last_match(1).delete('_').upcase! }
end

.camel_lower(term) ⇒ Object



15
16
17
18
19
# File 'lib/bright_serializer/inflector.rb', line 15

def camel_lower(term)
  return term unless term.include?('_')

  term.gsub!(CAMEL_REGEX) { Regexp.last_match(1).delete('_').upcase! }
end

.dash(underscored_word) ⇒ Object



27
28
29
30
# File 'lib/bright_serializer/inflector.rb', line 27

def dash(underscored_word)
  underscored_word.tr!('_', '-')
  underscored_word
end

.deep_transform_keys_in_object(object, &block) ⇒ Object

File active_support/core_ext/hash/keys.rb, line 116



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bright_serializer/inflector.rb', line 33

def deep_transform_keys_in_object(object, &block)
  case object
  when Hash
    object.each_with_object({}) do |(key, value), result|
      result[yield(key)] = deep_transform_keys_in_object(value, &block)
    end
  when Array
    object.map { |e| deep_transform_keys_in_object(e, &block) }
  else
    object
  end
end

.deep_transform_values_in_object(object, &block) ⇒ Object

File active_support/core_ext/hash/deep_transform_values.rb, line 25



47
48
49
50
51
52
53
54
55
56
# File 'lib/bright_serializer/inflector.rb', line 47

def deep_transform_values_in_object(object, &block)
  case object
  when Hash
    object.transform_values { |value| deep_transform_values_in_object(value, &block) }
  when Array
    object.map { |e| deep_transform_values_in_object(e, &block) }
  else
    yield(object)
  end
end

.underscore(camel_cased_word) ⇒ Object



21
22
23
24
25
# File 'lib/bright_serializer/inflector.rb', line 21

def underscore(camel_cased_word)
  camel_cased_word.gsub!(UNDERSCORE_REGEX, '\1_\2')
  camel_cased_word.downcase!
  camel_cased_word
end