Class: Pascoale::Formatter
- Inherits:
-
Object
- Object
- Pascoale::Formatter
- Defined in:
- lib/pascoale/formatter.rb
Constant Summary collapse
- EXCEPTIONS =
%w(a o e da do de na no em as os das dos nas nos à com sem ao)
Instance Method Summary collapse
- #as_title ⇒ Object
- #capitalize ⇒ Object
- #capitalize? ⇒ Boolean
- #downcase ⇒ Object
- #downcase? ⇒ Boolean
-
#initialize(text, force_downcase: EXCEPTIONS) ⇒ Formatter
constructor
A new instance of Formatter.
- #title_word(a_word) ⇒ Object
- #to_s ⇒ Object
- #upcase ⇒ Object
- #upcase? ⇒ Boolean
Constructor Details
#initialize(text, force_downcase: EXCEPTIONS) ⇒ Formatter
Returns a new instance of Formatter.
5 6 7 8 |
# File 'lib/pascoale/formatter.rb', line 5 def initialize(text, force_downcase: EXCEPTIONS) @text = text @force_downcase = force_downcase end |
Instance Method Details
#as_title ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/pascoale/formatter.rb', line 10 def as_title def title_word(a_word) if @force_downcase.include?(a_word.downcase) a_word.downcase else a_word.capitalize end end words = @text.split(/\s+/).map { |word| Formatter.new(word) } first = words.first first = first.upcase? ? first : first.capitalize rest = words[1..-1].map { |word| word.upcase? ? word : title_word(word) } ([first] + rest).join(' ') end |
#capitalize ⇒ Object
46 47 48 49 |
# File 'lib/pascoale/formatter.rb', line 46 def capitalize Formatter.new(@text[0..0]).upcase + Formatter.new(@text[1..-1]).downcase end |
#capitalize? ⇒ Boolean
34 35 36 |
# File 'lib/pascoale/formatter.rb', line 34 def capitalize? self.capitalize == @text end |
#downcase ⇒ Object
42 43 44 |
# File 'lib/pascoale/formatter.rb', line 42 def downcase @text.downcase.tr('ÁÉÍÓÚÂÊÔÃÕÇÜ', 'áéíóúâêôãõçü') end |
#downcase? ⇒ Boolean
30 31 32 |
# File 'lib/pascoale/formatter.rb', line 30 def downcase? self.downcase == @text end |
#title_word(a_word) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/pascoale/formatter.rb', line 11 def title_word(a_word) if @force_downcase.include?(a_word.downcase) a_word.downcase else a_word.capitalize end end |
#to_s ⇒ Object
51 52 53 |
# File 'lib/pascoale/formatter.rb', line 51 def to_s @text end |
#upcase ⇒ Object
38 39 40 |
# File 'lib/pascoale/formatter.rb', line 38 def upcase @text.upcase.tr('áéíóúâêôãõçü', 'ÁÉÍÓÚÂÊÔÃÕÇÜ') end |
#upcase? ⇒ Boolean
26 27 28 |
# File 'lib/pascoale/formatter.rb', line 26 def upcase? self.upcase == @text end |