Class: CapitalizeAttributes::SelectiveCapitalizer
- Inherits:
-
Object
- Object
- CapitalizeAttributes::SelectiveCapitalizer
- Defined in:
- lib/capitalize_attributes/selective_capitalizer.rb
Constant Summary collapse
- ABBREVIATION_MAX_LENGTH =
4
- ABBREVIATION_VOWELS =
%w(a e i o u y).freeze
- ROMAN_NUMERALS =
%w(i ii iii iv v vi vii viii ix x).to_set.freeze
- ALWAYS_DOWNCASED_WORDS =
[ "de", "of", "on", "the", ].to_set.freeze
Class Method Summary collapse
- .abbreviation?(downcased_word) ⇒ Boolean
- .always_downcased?(downcased_word) ⇒ Boolean
- .capitalize(word) ⇒ Object
- .perform(value) ⇒ Object
- .roman_numeral?(downcased_word) ⇒ Boolean
Class Method Details
.abbreviation?(downcased_word) ⇒ Boolean
43 44 45 46 47 48 |
# 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) ⇒ Boolean
54 55 56 |
# File 'lib/capitalize_attributes/selective_capitalizer.rb', line 54 def self.always_downcased?(downcased_word) ALWAYS_DOWNCASED_WORDS.include?(downcased_word) end |
.capitalize(word) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# 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) ⇒ Object
15 16 17 18 |
# 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) ⇒ Boolean
50 51 52 |
# File 'lib/capitalize_attributes/selective_capitalizer.rb', line 50 def self.roman_numeral?(downcased_word) ROMAN_NUMERALS.include?(downcased_word) end |