Sha256: 940e337355e7cbe3478869dca825b146ec59f9c2f58d57ee941a870ee70b9655

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

module Pascoale
  class Formatter
    EXCEPTIONS = %(a o e da do de na no em as os das dos nas nos à com sem)

    def initialize(text)
      @text = text
    end

    def as_title
      def title_word(a_word)
        if EXCEPTIONS.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

    def upcase?
      self.upcase == @text
    end

    def downcase?
      self.downcase == @text
    end

    def capitalize?
      self.capitalize == @text
    end

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

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

    def capitalize
      Formatter.new(@text[0..0]).upcase +
        Formatter.new(@text[1..-1]).downcase
    end

    def to_s
      @text
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pascoale-0.2.0 lib/pascoale/formatter.rb