Module: OxAiWorkers::CamelizeCompat

Defined in:
lib/oxaiworkers/present_compat.rb

Instance Method Summary collapse

Instance Method Details

#camelize(first_letter = :upper) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/oxaiworkers/present_compat.rb', line 11

def camelize(first_letter = :upper)
  string = dup
  string = if first_letter == :upper
             string.sub(/^[a-z\d]*/, &:capitalize)
           else
             string.sub(/^(?:(?=\b|[A-Z_])|\w)/, &:downcase)
           end
  string.gsub(%r{(?:_|(/))([a-z\d]*)}) do
    "#{::Regexp.last_match(1)}#{::Regexp.last_match(2).capitalize}"
  end.gsub('/', '::')
end

#underscoreObject



23
24
25
26
27
28
29
30
31
# File 'lib/oxaiworkers/present_compat.rb', line 23

def underscore
  word = dup
  word.gsub!(/::/, '/')
  word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  word.tr!('-', '_')
  word.downcase!
  word
end