Class: Phony::NationalSplitters::Regex

Inherits:
Fixed
  • Object
show all
Defined in:
lib/phony/national_splitters/regex.rb

Overview

National splitter class to split the ndc-local part of a number.

Countries can create new instances according to their needs.

Note: Countries should use instance_for

to avoid getting new local splitter instances.

Instance Attribute Summary collapse

Attributes inherited from Fixed

#special_splitter

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DSL

#>>, #country_for, #reserved

Constructor Details

#initialize(regex, on_fail_take = nil, options = {}) ⇒ Regex

Returns a new instance of Regex


24
25
26
27
28
# File 'lib/phony/national_splitters/regex.rb', line 24

def initialize regex, on_fail_take = nil, options = {}
  super on_fail_take, options

  @regex = regex
end

Instance Attribute Details

#on_fail_takeObject (readonly)

Returns the value of attribute on_fail_take


14
15
16
# File 'lib/phony/national_splitters/regex.rb', line 14

def on_fail_take
  @on_fail_take
end

#regexObject (readonly)

Returns the value of attribute regex


14
15
16
# File 'lib/phony/national_splitters/regex.rb', line 14

def regex
  @regex
end

Class Method Details

.instance_for(regex, on_fail_take = nil, options = {}) ⇒ Object

Get a splitter for the given format.

Note: Not cached.


20
21
22
# File 'lib/phony/national_splitters/regex.rb', line 20

def self.instance_for regex, on_fail_take = nil, options = {}
  new regex, on_fail_take, options
end

Instance Method Details

#lengthObject

A valid length.


47
48
49
# File 'lib/phony/national_splitters/regex.rb', line 47

def length
  # raise "#{self.class.name} has no length that can be automatically extracted."
end

#split(national_number) ⇒ Object

Split a local number according to an assumed country specific format.

Examples

  • split '3643533' # => ['364', '35', '33'] # (Switzerland)


35
36
37
38
39
40
41
42
43
# File 'lib/phony/national_splitters/regex.rb', line 35

def split national_number
  # Improve matching.
  #
  return [@zero, national_number.slice!(0..$1.size-1), national_number] if national_number =~ regex
  
  # Not found.
  #
  super national_number
end