module CleanMethodable

Adds the ‘clean’ method to String, which can be used to clean strings in various ways depending on the contents

Constants

CLEAN_METHODS
ROMAN_ONE_TO_FIVE_MAPPING

Public Instance Methods

clean(what) click to toggle source
# File lib/ndr_support/string/clean_methodable.rb, line 25
def clean(what)
  cleaning_method = CLEAN_METHODS[what]
  return send(cleaning_method) if cleaning_method

  gsub(' ?', ' ')
end

Private Instance Methods

clean_code() click to toggle source
# File lib/ndr_support/string/clean_methodable.rb, line 88
def clean_code
  split_on_separators.map do |code|
    code.blank? ? next : code.delete('.')
  end.compact.join(' ')
end
clean_code_icd() click to toggle source
# File lib/ndr_support/string/clean_methodable.rb, line 94
def clean_code_icd
  warn '[DEPRECATION] clean(:code_icd) is deprecated - consider using clean(:icd) instead.'
  # regexp = /[A-Z][0-9]{2}(\.(X|[0-9]{1,2})|[0-9]?)( *(D|A)( |,|;|$))/
  codes = upcase.split_on_separators.delete_if { |x| x.squash.blank? }
  cleaned_codes = []
  codes.each do |code|
    if %w[A D].include?(code)
      cleaned_codes[-1] += code
    else
      cleaned_codes << code
    end
  end
  cleaned_codes.join(' ')
end
clean_code_opcs() click to toggle source
# File lib/ndr_support/string/clean_methodable.rb, line 136
def clean_code_opcs
  split_on_separators.map do |code|
    db_code = code.squash
    next unless 4 == db_code.length || db_code =~ /CZ00[12]/

    db_code
  end.compact.join(' ')
end
clean_ethniccategory() click to toggle source
# File lib/ndr_support/string/clean_methodable.rb, line 78
def clean_ethniccategory
  replace_ethniccategory = {
    '0' => '0', '1' => 'M', '2' => 'N',
    '3' => 'H', '4' => 'J', '5' => 'K',
    '6' => 'R', '7' => '8', '&' => 'X',
    ' ' => 'X', '99' => 'X'
  }
  replace_ethniccategory[self] || upcase
end
clean_gender() click to toggle source
# File lib/ndr_support/string/clean_methodable.rb, line 46
def clean_gender
  return '1' if self =~ /\AM(ale)?/i
  return '2' if self =~ /\AF(emale)?/i

  self
end
clean_hospitalnumber() click to toggle source
# File lib/ndr_support/string/clean_methodable.rb, line 114
def clean_hospitalnumber
  self[-1..] =~ /\d/ ? self : self[0..-2]
end
clean_icd() click to toggle source
# File lib/ndr_support/string/clean_methodable.rb, line 109
def clean_icd
  codes = upcase.squish.split_on_separators.reject(&:blank?)
  codes.map { |code| code.gsub(/(?<=\d)(\.?X?)/, '') }.join(' ')
end
clean_log10() click to toggle source
# File lib/ndr_support/string/clean_methodable.rb, line 145
def clean_log10
  f_value = Float(self, exception: false)
  return self if f_value.nil? || f_value.negative?

  f_value.zero? ? '0.0' : Math.log10(f_value).to_s
end
clean_lpi() click to toggle source
# File lib/ndr_support/string/clean_methodable.rb, line 42
def clean_lpi
  upcase.delete('^0-9A-Z')
end
clean_name() click to toggle source
# File lib/ndr_support/string/clean_methodable.rb, line 68
def clean_name
  substitutions = {
    '.'      => '',
    /,|;/    => ' ',
    /\s{2,}/ => ' ',
    '`'      => '\''
  }
  substitutions.inject(upcase) { |a, e| a.gsub(*e) }.strip
end
clean_nhsnumber() click to toggle source
# File lib/ndr_support/string/clean_methodable.rb, line 34
def clean_nhsnumber
  delete('^0-9')[0..9]
end
clean_postcode() click to toggle source
# File lib/ndr_support/string/clean_methodable.rb, line 38
def clean_postcode
  postcodeize(:db)
end
clean_roman5() click to toggle source
# File lib/ndr_support/string/clean_methodable.rb, line 122
def clean_roman5
  # This deromanises roman numerals between 1 and 5
  gsub(/[IV]+/i) { |match| ROMAN_ONE_TO_FIVE_MAPPING[match.upcase] }
end
clean_sex() click to toggle source
# File lib/ndr_support/string/clean_methodable.rb, line 53
def clean_sex
  # SECURE: BNS 2012-10-09: But may behave oddly for multi-line input
  return '1' if self =~ /^M|1/i
  return '2' if self =~ /^F|2/i

  '0'
end
clean_sex_c() click to toggle source
# File lib/ndr_support/string/clean_methodable.rb, line 61
def clean_sex_c
  return 'M' if self =~ /^M|1/i
  return 'F' if self =~ /^F|2/i

  ''
end
clean_tnmcategory() click to toggle source
# File lib/ndr_support/string/clean_methodable.rb, line 127
def clean_tnmcategory
  sub!(/\A[tnm]/i, '')
  if self =~ /\Ax\z/i
    upcase
  else
    downcase
  end
end
clean_xmlsafe() click to toggle source
# File lib/ndr_support/string/clean_methodable.rb, line 118
def clean_xmlsafe
  strip_xml_unsafe_characters
end