-
Notifications
You must be signed in to change notification settings - Fork 0
Formatters
After run the column validators and if the cell is valid, the formatters will be applied.
You can define formatters in 2 ways:
-
As
columnoption:class SuperheroesParser < Parxer::XlsParser column(:alive, name: "Alive", format: :boolean) end
-
With the
format_withmethod: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-
Boolean
class SuperheroesParser < Parxer::XlsParser column(:alive, name: "Alive", format: :boolean) end
If you have a cell with
Xvalue, you will getYresult:- 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
- X:
-
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
Xvalue, you will getYresult:- X:
"30972198"- Y:"3.097.219-8" - X:
"3097219-8"- Y:"30972198"with 'clean: trueoption - X:
"3.097.219-8"- Y:"30972198"with 'clean: trueoption
- X:
-
String
class SuperheroesParser < Parxer::XlsParser column(:amount, name: "Amount", format: :string) end
If you have a cell with
Xvalue, you will getYresult:- X:
1234- Y:"1234" - X:
"1234"- Y:"1234"
- X:
-
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
Xvalue, you will getYresult:- X:
"1"- Y:1 - X:
"1.567"- Y:1.567 - X:
"1.567"- Y:1withinteger: trueoption - X:
"1.567"- Y:1.6withround: 1option
- X:
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