Class: ReeString::Underscore

Inherits:
Object
  • Object
show all
Includes:
Ree::FnDSL
Defined in:
lib/ree_lib/packages/ree_string/package/ree_string/functions/underscore.rb

Instance Method Summary collapse

Instance Method Details

#call(camel_cased_word, **opts) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ree_lib/packages/ree_string/package/ree_string/functions/underscore.rb', line 30

def call(camel_cased_word, **opts)
  return camel_cased_word.to_s unless /[A-Z-]|::/.match?(camel_cased_word)

  acronyms = opts[:acronyms] || []
  regex = acronyms_underscore_regex(acronyms)

  word = camel_cased_word.to_s.gsub("::", "/")
  word.gsub!(regex) { "#{$1 && '_' }#{$2.downcase}" }
  word.gsub!(/([A-Z]+)(?=[A-Z][a-z])|([a-z\d])(?=[A-Z])/) { ($1 || $2) << "_" }
  word.tr!("-", "_")
  word.downcase!
  word
end