class CapitalizeAttributes::SelectiveCapitalizer

Constants

ABBREVIATION_MAX_LENGTH
ABBREVIATION_VOWELS
ALWAYS_DOWNCASED_WORDS
ROMAN_NUMERALS

Public Class Methods

abbreviation?(downcased_word) click to toggle source
# File lib/capitalize_attributes/selective_capitalizer.rb, line 43
def self.abbreviation?(downcased_word)
  return false if downcased_word.length > ABBREVIATION_MAX_LENGTH

  chars = downcased_word.chars
  (chars - ABBREVIATION_VOWELS) == chars
end
always_downcased?(downcased_word) click to toggle source
# File lib/capitalize_attributes/selective_capitalizer.rb, line 54
def self.always_downcased?(downcased_word)
  ALWAYS_DOWNCASED_WORDS.include?(downcased_word)
end
capitalize(word) click to toggle source
# File lib/capitalize_attributes/selective_capitalizer.rb, line 20
def self.capitalize(word)
  # Modify the word only if it is all lowercase or all uppercase.
  # This avoids inadvertently capitalizing names intended to have mixed
  # case, like "McDonald".
  #
  # If a word is mixed case, we assume the person entering it
  # intends the case to be as entered.
  return word unless word.downcase == word || word.upcase == word

  downcased_word = word.downcase

  case
  when always_downcased?(downcased_word)
    downcased_word
  when abbreviation?(downcased_word)
    word.upcase
  when roman_numeral?(downcased_word)
    word.upcase
  else
    word.titleize
  end
end
perform(value) click to toggle source
# File lib/capitalize_attributes/selective_capitalizer.rb, line 15
def self.perform(value)
  words = value.split
  words.map { |word| capitalize(word) }.join(" ")
end
roman_numeral?(downcased_word) click to toggle source
# File lib/capitalize_attributes/selective_capitalizer.rb, line 50
def self.roman_numeral?(downcased_word)
  ROMAN_NUMERALS.include?(downcased_word)
end