module CSVPP::Conversions
Constants
- ARRAY_TYPE_RGX
Public Instance Methods
clean_decimal(str)
click to toggle source
# File lib/csvpp/conversions.rb, line 142 def clean_decimal(str) return str unless str.is_a?(String) val = str.strip .gsub(/['`\s]?/, '') # remove thousand separators .sub(/[\sa-zA-Z]*$/, '') # remove trailing words like "mg" .sub(/^-0*(.+)$/, '-\1') # remove 0 after negative sign: -003 => -3 if val =~ /^0+$/ # remove leading zeros '0' else val.gsub( /^0*/, '') end end
convert(obj, to:, missings: [], **options)
click to toggle source
@param obj [Object] object to parse @param to [String] a type, e.g. “int” @param missings [Array] list of values that are treated as missings,
e.g. ['NA', '-', -999]
@param options [Hash] options passed on to parsing methods for specific types @return parsed value, read from `obj`, interpreted as type given by `to`
# File lib/csvpp/conversions.rb, line 15 def convert(obj, to:, missings: [], **options) return nil if missing?(obj, missings) if to.start_with?('array') to, rest = to.split('<') rest = rest.tr('>', '') match = rest.match(ARRAY_TYPE_RGX) options = options.merge( type: match[:array_type], delimiter: match[:array_delimiter] ) end send("parse_#{to}", obj, **options) end
missing?(obj, missings)
click to toggle source
# File lib/csvpp/conversions.rb, line 138 def missing?(obj, missings) missings.map(&:to_s).include?(obj.to_s) end
parse_array(str, type:, delimiter:, **options)
click to toggle source
# File lib/csvpp/conversions.rb, line 31 def parse_array(str, type:, delimiter:, **options) str.split(delimiter).map { |entry| send("parse_#{type}", entry) } end
parse_boolean(str, true_values: [], false_values: [], **options)
click to toggle source
@param true_values [Array]: list of values that are interpreted as `true` @param false_values [Array]: list of values that are interpreted as `false` @return true or false, or
nil if `str` doesn't match any value interpreted as `true` or `false`
# File lib/csvpp/conversions.rb, line 114 def parse_boolean(str, true_values: [], false_values: [], **options) cleaned = str.to_s.strip.downcase trues = if true_values.empty? ['1', 't', 'true'] else true_values.map(&:to_s).map(&:downcase) end return true if trues.include? cleaned falses = if false_values.empty? ['0', 'f', 'false'] else false_values.map(&:to_s).map(&:downcase) end return false if falses.include? cleaned nil end
parse_chop(str, delimiter: ':', **options)
click to toggle source
# File lib/csvpp/conversions.rb, line 35 def parse_chop(str, delimiter: ':', **options) code, laterality, date = str.split(delimiter) code = parse_string(code) laterality = parse_string(laterality) if laterality laterality = nil if laterality&.empty? date = parse_date(date) if date { code: code, laterality: laterality, date: date } end
parse_date(str, **options)
click to toggle source
# File lib/csvpp/conversions.rb, line 106 def parse_date(str, **options) Date.parse(str.to_s) end
parse_decimal(str, **options)
click to toggle source
# File lib/csvpp/conversions.rb, line 94 def parse_decimal(str, **options) return nil if str.to_s.empty? cleaned = clean_decimal(str).to_s if cleaned.empty? nil else BigDecimal(cleaned) end end
parse_float(str, **options)
click to toggle source
# File lib/csvpp/conversions.rb, line 89 def parse_float(str, **options) return nil if str.to_s.empty? Float(clean_decimal(str)) rescue nil end
parse_int(str, **options)
click to toggle source
# File lib/csvpp/conversions.rb, line 73 def parse_int(str, **options) return nil if str.to_s.empty? cleaned = if str.is_a?(String) val = str.strip .gsub(/['`\s]?/, '') # remove thousand separators .sub(/\.\d*/, '') # remove decimal point and everything thereafter .sub(/[\sa-zA-Z]*$/, '') # remove trailing words like "mg" .sub(/^-0*(.+)$/, '-\1') # remove 0 after negative sign: -003 => -3 val =~ /^0+$/ ? '0' : val.gsub( /^0*/, '') # remove leading zeros else str end Integer(cleaned) rescue nil end
parse_medi(str, delimiter: ':', **options)
click to toggle source
See page 3 in documentation/Technisches_Begleitblatt_2017_d.pdf more more info on the medi data type.
# File lib/csvpp/conversions.rb, line 51 def parse_medi(str, delimiter: ':', **options) atc_code, annex, application, dose, unit = str.split(delimiter) atc_code = parse_string(atc_code) annex = parse_string(annex) if annex annex = nil if annex&.empty? application = parse_string(application) dose = parse_decimal(dose) unit = parse_string(unit) { atc_code: atc_code, annex: annex, application: application, dose: dose, unit: unit } end
parse_string(str, **options)
click to toggle source
# File lib/csvpp/conversions.rb, line 69 def parse_string(str, **options) str.to_s.strip end