Skip to content

Formatters

Leandro Segovia edited this page Feb 23, 2018 · 4 revisions

After run the column validators and if the cell is valid, the formatters will be applied.

You can define formatters in 2 ways:

  1. As column option:

    class SuperheroesParser < Parxer::XlsParser
      column(:alive, name: "Alive", format: :boolean)
    end
  2. With the format_with method:

    class SuperheroesParser < Parxer::XlsParser
      column(:name, name: "Name") do
        format_with do
          value.upcase
        end
      end
    end

According to the previous examples, if you have a xls file with "1" as "Alive" value and "Leandro" as "Name" value, you will get

parser = SuperheroesParser.new
result = parser.run("/some_path/superhero.xls"); #=> #<Enumerator: ...>
row = result.next

row.name #=> "LEANDRO"
row.alive #=> true

Built in formatters:

  • Boolean

    class SuperheroesParser < Parxer::XlsParser
      column(:alive, name: "Alive", format: :boolean)
    end

    If you have a cell with X value, you will get Y result:

    • X: true - Y: true
    • X: "true" - Y: true
    • X: "1" - Y: true
    • X. "t" - Y: true
    • X: false - Y: false
    • X: "false" - Y: false
    • X: "0" - Y: false
    • X. "f" - Y: false
  • Chilean RUT

    class SuperheroesParser < Parxer::XlsParser
      column(:rut, name: "RUT", format: :rut)
    
      # OR
    
      column(:rut, name: "RUT") do
        format_with(:rut, options)
      end
    end

    If you have a cell with X value, you will get Y result:

    • X: "30972198" - Y: "3.097.219-8"
    • X: "3097219-8" - Y: "30972198" with ' clean: true option
    • X: "3.097.219-8" - Y: "30972198" with ' clean: true option
  • String

    class SuperheroesParser < Parxer::XlsParser
      column(:amount, name: "Amount", format: :string)
    end

    If you have a cell with X value, you will get Y result:

    • X: 1234 - Y: "1234"
    • X: "1234" - Y: "1234"
  • Number

    class SuperheroesParser < Parxer::XlsParser
      column(:amount, name: "Amount", format: :number)
    
      # OR
    
      column(:amount, name: "Amount") do
        format_with(:number, options)
      end
    end

    If you have a cell with X value, you will get Y result:

    • X: "1" - Y: 1
    • X: "1.567" - Y: 1.567
    • X: "1.567" - Y: 1 with integer: true option
    • X: "1.567" - Y: 1.6 with round: 1 option

Custom formatters

You can define 2 kind of custom formatters:

  • Inline Formatters:

    This kind of formatters take a block with formatting logic. For example:

    class SuperheroesParser < Parxer::XlsParser
      column(:name, name: "Name")
        format_with do
          value.to_s.upcase
        end
      end
    end
  • Reusable Formatters:

    If you want to reuse a formatter, you can create one like this:

    module Parxer
      module Formatter
        class StringMagic < Base
          def format_value
            value.to_s.send(magic_type)
          end
    
          private
    
          def magic_type
            config[:magic_type] || :upcase
          end
        end
      end
    end

    And then you can use it like this:

    class SuperheroesParser < Parxer::XlsParser
      column(:name, name: "Name", format: :string_magic)
    
      column(:publisher, name: "Publisher") do
        format_with(:string_magic, type: :downcase)
      end
    end
Clone this wiki locally