class String

Extend string with helpers specificic to this gem.

Constants

MM_Acronyms
MM_Upper_In_Camel

Public Instance Methods

mm_camelize() click to toggle source

Convert snake case to camel case while honoring acronyms.

# File lib/core_ext/string_extension.rb, line 31
def mm_camelize
  first = split('_')[0]
  first = first.upcase if MM_Upper_In_Camel.include?(first.upcase)
  remaining = split('_').drop(1)
  camelized = remaining.map { |p| mm_capitalize_with_acronyms(p) }
  camelized.insert(0, first)
  camelized.join
end
mm_pascalize() click to toggle source

Convert snake case to Pascal case while honoring acronyms.

# File lib/core_ext/string_extension.rb, line 41
def mm_pascalize
  split('_').map { |p| mm_capitalize_with_acronyms(p) }.join
end
mm_pluralize() click to toggle source

Simplistic pluralization for M&M types

# File lib/core_ext/string_extension.rb, line 46
def mm_pluralize
  if self =~ /y$/
    "#{chomp('y')}ies"
  else
    "#{self}s"
  end
end
mm_underscore() click to toggle source

Underscore functionality to match conversion from M&M values.

# File lib/core_ext/string_extension.rb, line 7
def mm_underscore
  return self if self =~ /^[a-z_]+$/

  # handle fields that are IDs
  underscored = gsub(/IDs/, '_ids')

  # break on lower to upper transition
  underscored.gsub!(/([a-z])([A-Z]+)/) do
    "#{Regexp.last_match(1)}_#{Regexp.last_match(2)}"
  end

  # break on acronym to caps word transition
  underscored.gsub!(/([A-Z])([A-Z][a-z]+)/) do
    "#{Regexp.last_match(1)}_#{Regexp.last_match(2)}"
  end

  # special case separation
  underscored.gsub!(/DNSPTR/i, 'dns_ptr')
  underscored.gsub!(/VLANID/i, 'vlan_id')

  underscored.downcase
end

Private Instance Methods

mm_capitalize_with_acronyms(word) click to toggle source
# File lib/core_ext/string_extension.rb, line 56
def mm_capitalize_with_acronyms(word)
  return 'IDs' if word == 'ids'
  MM_Acronyms.include?(word.upcase) ? word.upcase : word.capitalize
end