Class: String

Inherits:
Object
  • Object
show all
Includes:
CleanMethodable
Defined in:
lib/ndr_support/string/cleaning.rb,
lib/ndr_support/string/conversions.rb

Overview

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

Constant Summary collapse

INVALID_CONTROL_CHARS =
/[\x00-\x08\x0b-\x0c\x0e-\x1f]/
ROMAN_ONE_TO_FIVE_MAPPING =
{ 'I' => '1', 'II' => '2', 'III' => '3', 'IIII' => '4', 'IV' => '4', 'V' => '5' }
POSTCODE_REGEXP =
/
  ^(
    [A-Z][0-9]           |
    [A-Z][0-9][0-9]      |
    [A-Z][0-9][A-Z]      |
    [A-Z][A-Z][0-9]      |
    NPT                  |
    [A-Z][A-Z][0-9][0-9] |
    [A-Z][A-Z][0-9][A-Z]
  )
  [0-9][A-Z][A-Z]
$/x
SOUNDEX_CHARS =
'BPFVCSKGJQXZDTLMNR'
SOUNDEX_NUMS =
'111122222222334556'
SOUNDEX_CHARS_EX =
'^' + SOUNDEX_CHARS
SOUNDEX_CHARS_DEL =
'^A-Z'

Constants included from CleanMethodable

CleanMethodable::CLEAN_METHODS

Instance Method Summary collapse

Methods included from CleanMethodable

#clean

Instance Method Details

#date1Object



40
41
42
# File 'lib/ndr_support/string/conversions.rb', line 40

def date1
  Daterange.new(self).date1
end

#date2Object



44
45
46
# File 'lib/ndr_support/string/conversions.rb', line 44

def date2
  Daterange.new(self).date2
end

#nhs_numberizeObject

Show NHS numbers with spaces



74
75
76
77
# 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_datetimeObject



128
# File 'lib/ndr_support/string/conversions.rb', line 128

alias orig_to_datetime to_datetime

#postcodeize(option = :user) ⇒ Object

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ndr_support/string/cleaning.rb', line 32

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) ⇒ Object



26
27
28
29
30
31
32
33
34
# 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) ⇒ Object



36
37
38
# File 'lib/ndr_support/string/conversions.rb', line 36

def sounds_like(other)
  soundex == other.soundex
end

#squashObject

Used for comparing addresses



26
27
28
# File 'lib/ndr_support/string/cleaning.rb', line 26

def squash
  upcase.delete('^A-Z0-9')
end

#strip_xml_unsafe_charactersObject



50
51
52
# File 'lib/ndr_support/string/cleaning.rb', line 50

def strip_xml_unsafe_characters
  gsub(String::INVALID_CONTROL_CHARS, '')
end

#surname_and_initialsObject

Convert “SMITH JD” into “Smith JD”



57
58
59
60
61
# File 'lib/ndr_support/string/conversions.rb', line 57

def surname_and_initials
  a = split
  initials = a.pop
  a.collect(&:capitalize).join(' ') + ' ' + initials
end

#surnameizeObject

Like titleize but copes with Scottish and Irish names.



64
65
66
67
68
69
70
71
# 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

#thedateObject



48
49
50
# File 'lib/ndr_support/string/conversions.rb', line 48

def thedate
  Ourdate.new(self).thedate
end

#thetimeObject



52
53
54
# File 'lib/ndr_support/string/conversions.rb', line 52

def thetime
  Ourtime.new(self).thetime
end

#to_booleanObject

Try to convert the string value into boolean



143
144
145
146
147
148
# File 'lib/ndr_support/string/conversions.rb', line 143

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_datetimeObject



130
131
132
133
134
135
136
137
138
139
140
# 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 = if ActiveRecord.respond_to?(:default_timezone)
                       ActiveRecord.default_timezone
                     else
                       ActiveRecord::Base.default_timezone # Rails <= 6.1
                     end
  return to_time.to_datetime if default_timezone == :local

  orig_to_datetime
end

#truncate_hellip(n) ⇒ Object

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



80
81
82
# File 'lib/ndr_support/string/conversions.rb', line 80

def truncate_hellip(n)
  length > n ? slice(0, n - 1) + '&hellip;' : self
end

#xml_unsafe?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/ndr_support/string/cleaning.rb', line 54

def xml_unsafe?
  self =~ String::INVALID_CONTROL_CHARS
end