class Pascoale::Formatter

Constants

EXCEPTIONS

Public Class Methods

new(text, force_downcase: EXCEPTIONS) click to toggle source
# File lib/pascoale/formatter.rb, line 5
def initialize(text, force_downcase: EXCEPTIONS)
  @text = text
  @force_downcase = force_downcase
end

Public Instance Methods

as_title() click to toggle source
# 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() click to toggle source
# File lib/pascoale/formatter.rb, line 46
def capitalize
  Formatter.new(@text[0..0]).upcase +
    Formatter.new(@text[1..-1]).downcase
end
capitalize?() click to toggle source
# File lib/pascoale/formatter.rb, line 34
def capitalize?
  self.capitalize == @text
end
downcase() click to toggle source
# File lib/pascoale/formatter.rb, line 42
def downcase
  @text.downcase.tr('ÁÉÍÓÚÂÊÔÃÕÇÜ', 'áéíóúâêôãõçü')
end
downcase?() click to toggle source
# File lib/pascoale/formatter.rb, line 30
def downcase?
  self.downcase == @text
end
title_word(a_word) click to toggle source
# 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() click to toggle source
# File lib/pascoale/formatter.rb, line 51
def to_s
  @text
end
upcase() click to toggle source
# File lib/pascoale/formatter.rb, line 38
def upcase
  @text.upcase.tr('áéíóúâêôãõçü', 'ÁÉÍÓÚÂÊÔÃÕÇÜ')
end
upcase?() click to toggle source
# File lib/pascoale/formatter.rb, line 26
def upcase?
  self.upcase == @text
end