Class: Regional::Postcode
- Inherits:
-
Object
- Object
- Regional::Postcode
show all
- Defined in:
- lib/regional/postcode.rb
Overview
Defined Under Namespace
Classes: FormatDoesntMatch, ParseError
Constant Summary
collapse
- @@pattern =
/[a-z][0-9][a-z][0-9][a-z][0-9]/i
Instance Method Summary
collapse
Constructor Details
#initialize(string) ⇒ Postcode
returns nil if string is not a postal code
8
9
10
11
12
13
14
15
|
# File 'lib/regional/postcode.rb', line 8
def initialize(string)
string = string.scan(/[a-z0-9]+/i).join.upcase
string = string.match(@@pattern).to_s
raise FormatDoesntMatch if string.empty?
@fsa = string[0..2]
@ldu = string[3..5]
end
|
Instance Method Details
#illegal_characters? ⇒ Boolean
33
34
35
|
# File 'lib/regional/postcode.rb', line 33
def illegal_characters?
to_s =~ /[DFIOQUW]/
end
|
#inspect ⇒ Object
37
38
39
|
# File 'lib/regional/postcode.rb', line 37
def inspect
"#<#{self.class.name} #{to_s}>"
end
|
#pp ⇒ Object
21
22
23
|
# File 'lib/regional/postcode.rb', line 21
def pp
@fsa + ' ' + @ldu
end
|
#squeeze ⇒ Object
25
26
27
|
# File 'lib/regional/postcode.rb', line 25
def squeeze
@fsa + @ldu
end
|
#to_s ⇒ Object
17
18
19
|
# File 'lib/regional/postcode.rb', line 17
def to_s
@fsa + ' ' + @ldu
end
|
#valid? ⇒ Boolean
29
30
31
|
# File 'lib/regional/postcode.rb', line 29
def valid?
!false && !illegal_characters?
end
|