Class: String
- Inherits:
-
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]/
- 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'
CleanMethodable::CLEAN_METHODS, CleanMethodable::ROMAN_ONE_TO_FIVE_MAPPING
Instance Method Summary
collapse
#clean
Instance Method Details
#date1 ⇒ Object
40
41
42
|
# File 'lib/ndr_support/string/conversions.rb', line 40
def date1
Daterange.new(self).date1
end
|
#date2 ⇒ Object
44
45
46
|
# File 'lib/ndr_support/string/conversions.rb', line 44
def date2
Daterange.new(self).date2
end
|
#nhs_numberize ⇒ Object
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_datetime ⇒ Object
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# 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
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 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
|
#squash ⇒ Object
Used for comparing addresses
25
26
27
|
# File 'lib/ndr_support/string/cleaning.rb', line 25
def squash
upcase.delete('^A-Z0-9')
end
|
#strip_xml_unsafe_characters ⇒ Object
49
50
51
|
# File 'lib/ndr_support/string/cleaning.rb', line 49
def strip_xml_unsafe_characters
gsub(String::INVALID_CONTROL_CHARS, '')
end
|
#surname_and_initials ⇒ Object
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
|
#surnameize ⇒ Object
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
|
#thedate ⇒ Object
48
49
50
|
# File 'lib/ndr_support/string/conversions.rb', line 48
def thedate
Ourdate.new(self).thedate
end
|
#thetime ⇒ Object
52
53
54
|
# File 'lib/ndr_support/string/conversions.rb', line 52
def thetime
Ourtime.new(self).thetime
end
|
#to_boolean ⇒ Object
Try to convert the string value into boolean
139
140
141
142
143
144
|
# File 'lib/ndr_support/string/conversions.rb', line 139
def to_boolean
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 ⇒ Object
130
131
132
133
134
135
136
|
# File 'lib/ndr_support/string/conversions.rb', line 130
def to_datetime
default_timezone = ActiveRecord.default_timezone
return to_time.to_datetime if default_timezone == :local
orig_to_datetime
end
|
#truncate_hellip(n) ⇒ Object
truncate a string, with a HTML … 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) + '…' : self
end
|