class String

Extends String clean with various methods of cleaning strings zand polishing them

Constants

INVALID_CONTROL_CHARS
POSTCODE_REGEXP
SOUNDEX_CHARS
SOUNDEX_CHARS_DEL
SOUNDEX_CHARS_EX
SOUNDEX_NUMS

Public Instance Methods

date1() click to toggle source
# File lib/ndr_support/string/conversions.rb, line 40
def date1
  Daterange.new(self).date1
end
date2() click to toggle source
# File lib/ndr_support/string/conversions.rb, line 44
def date2
  Daterange.new(self).date2
end
nhs_numberize() click to toggle source

Show NHS numbers with spaces

# File lib/ndr_support/string/conversions.rb, line 74
def nhs_numberize
  return self unless length == 10
  self[0..2] + ' ' + self[3..5] + ' ' + self[6..9]
end
orig_to_datetime()
Alias for: to_datetime
postcodeize(option = :user) click to toggle source

Show postcode in various formats. Parameter “option” can be :user, :compact, :db

# File lib/ndr_support/string/cleaning.rb, line 31
def postcodeize(option = :user)
  nspce = gsub(/[[:space:]]/, '').upcase
  return self unless nspce.blank? || POSTCODE_REGEXP =~ nspce # Don't change old-style or malformed postcodes

  case option
  when :compact
    nspce
  when :db
    case nspce.length
    when 5 then nspce.insert(-4, '  ')
    when 6 then nspce.insert(-4, ' ')
    else nspce
    end
  else # anything else, including :user --> friendly format
    nspce.length < 5 ? nspce : nspce.insert(-4, ' ')
  end
end
soundex(census = true) click to toggle source

desc: en.wikipedia.org/wiki/Soundex

# File lib/ndr_support/string/conversions.rb, line 26
def soundex(census = true)
  str = upcase.delete(SOUNDEX_CHARS_DEL).squeeze

  str[0..0] + str[1..-1].
    delete(SOUNDEX_CHARS_EX).
    tr(SOUNDEX_CHARS, SOUNDEX_NUMS)[0..(census ? 2 : -1)].
    squeeze[0..(census ? 2 : -1)].
    ljust(3, '0') rescue ''
end
sounds_like(other) click to toggle source
# File lib/ndr_support/string/conversions.rb, line 36
def sounds_like(other)
  soundex == other.soundex
end
squash() click to toggle source

Used for comparing addresses

# File lib/ndr_support/string/cleaning.rb, line 25
def squash
  upcase.delete('^A-Z0-9')
end
strip_xml_unsafe_characters() click to toggle source
# File lib/ndr_support/string/cleaning.rb, line 49
def strip_xml_unsafe_characters
  gsub(String::INVALID_CONTROL_CHARS, '')
end
surname_and_initials() click to toggle source

Convert “SMITH JD” into “Smith JD”

# File lib/ndr_support/string/conversions.rb, line 57
def surname_and_initials
  a = split
  initials = a.pop
  a.collect(&:capitalize).join(' ') + ' ' + initials
end
surnameize() click to toggle source

Like titleize but copes with Scottish and Irish names.

# File lib/ndr_support/string/conversions.rb, line 64
def surnameize
  s = slice(0, 2).upcase
  if s == 'MC' || s == "O'"
    s.titleize + slice(2..-1).titleize
  else
    titleize
  end
end
thedate() click to toggle source
# File lib/ndr_support/string/conversions.rb, line 48
def thedate
  Ourdate.new(self).thedate
end
thetime() click to toggle source
# File lib/ndr_support/string/conversions.rb, line 52
def thetime
  Ourtime.new(self).thetime
end
to_boolean() click to toggle source

Try to convert the string value into boolean

# File lib/ndr_support/string/conversions.rb, line 139
def to_boolean
  # SECURE: BNS 2012-10-09: But may behave oddly for multi-line input
  return true if self == true || self =~ (/^(true|t|yes|y|1)$/i)
  return false if self == false || self.nil? || self =~ (/^(false|f|no|n|0)$/i)
  fail ArgumentError, "invalid value for Boolean: \"#{self}\""
end
to_datetime() click to toggle source
# File lib/ndr_support/string/conversions.rb, line 130
def to_datetime
  # Default timezone for to_datetime conversion is GMT, not local timezone
  default_timezone = ActiveRecord.default_timezone
  return to_time.to_datetime if default_timezone == :local

  orig_to_datetime
end
Also aliased as: orig_to_datetime
truncate_hellip(n) click to toggle source

truncate a string, with a HTML &hellip; at the end

# File lib/ndr_support/string/conversions.rb, line 80
def truncate_hellip(n)
  length > n ? slice(0, n - 1) + '&hellip;' : self
end
xml_unsafe?() click to toggle source
# File lib/ndr_support/string/cleaning.rb, line 53
def xml_unsafe?
  self =~ String::INVALID_CONTROL_CHARS
end

Protected Instance Methods

split_on_separators(regexp = / |,|;/) click to toggle source
# File lib/ndr_support/string/cleaning.rb, line 59
def split_on_separators(regexp = / |,|;/)
  split(regexp)
end