Class: CSVImportable::TypeParser::PercentTypeParser

Inherits:
CSVImportable::TypeParser show all
Defined in:
lib/csv_importable/type_parser/percent_type_parser.rb

Instance Attribute Summary

Attributes inherited from CSVImportable::TypeParser

#key, #required, #row, #value

Instance Method Summary collapse

Methods inherited from CSVImportable::TypeParser

#initialize, #parse

Constructor Details

This class inherits a constructor from CSVImportable::TypeParser

Instance Method Details

#error_messageObject



10
11
12
# File 'lib/csv_importable/type_parser/percent_type_parser.rb', line 10

def error_message
  "Invalid percent for column: #{key}. It should be a decimal between 0 and 1."
end

#outside_rangeObject



14
15
16
# File 'lib/csv_importable/type_parser/percent_type_parser.rb', line 14

def outside_range
  raise
end

#parse_percentage_signObject



18
19
20
# File 'lib/csv_importable/type_parser/percent_type_parser.rb', line 18

def parse_percentage_sign
  value.to_f / 100.0
end

#parse_valObject



3
4
5
6
7
8
# File 'lib/csv_importable/type_parser/percent_type_parser.rb', line 3

def parse_val
  val = parse_percentage_sign if value.to_s.include?("%")
  val = val.present? ? val : Float(value)
  outside_range if val < 0 || val > 1
  val
end