Class: Pascoale::Formatter

Inherits:
Object
  • Object
show all
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

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_titleObject



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

#capitalizeObject



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

Returns:

  • (Boolean)


34
35
36
# File 'lib/pascoale/formatter.rb', line 34

def capitalize?
  self.capitalize == @text
end

#downcaseObject



42
43
44
# File 'lib/pascoale/formatter.rb', line 42

def downcase
  @text.downcase.tr('ÁÉÍÓÚÂÊÔÃÕÇÜ', 'áéíóúâêôãõçü')
end

#downcase?Boolean

Returns:

  • (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_sObject



51
52
53
# File 'lib/pascoale/formatter.rb', line 51

def to_s
  @text
end

#upcaseObject



38
39
40
# File 'lib/pascoale/formatter.rb', line 38

def upcase
  @text.upcase.tr('áéíóúâêôãõçü', 'ÁÉÍÓÚÂÊÔÃÕÇÜ')
end

#upcase?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/pascoale/formatter.rb', line 26

def upcase?
  self.upcase == @text
end