Class: Regional::Postcode

Inherits:
Object
  • Object
show all
Defined in:
lib/regional/postcode.rb

Overview

Canadian postal code

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

Raises:



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

Returns:

  • (Boolean)


33
34
35
# File 'lib/regional/postcode.rb', line 33

def illegal_characters?
  to_s =~ /[DFIOQUW]/
end

#inspectObject



37
38
39
# File 'lib/regional/postcode.rb', line 37

def inspect
  "#<#{self.class.name} #{to_s}>"
end

#ppObject



21
22
23
# File 'lib/regional/postcode.rb', line 21

def pp
  @fsa + ' ' + @ldu
end

#squeezeObject



25
26
27
# File 'lib/regional/postcode.rb', line 25

def squeeze
  @fsa + @ldu
end

#to_sObject



17
18
19
# File 'lib/regional/postcode.rb', line 17

def to_s
  @fsa + ' ' + @ldu
end

#valid?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/regional/postcode.rb', line 29

def valid?
  !false && !illegal_characters?
end