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.



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

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.



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

def on_fail_take
  @on_fail_take
end

#regexObject (readonly)

Returns the value of attribute regex.



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

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.



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

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

Instance Method Details

#lengthObject

A valid length.



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

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)



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

def split(national_number)
  # Improve matching.
  #
  return [@zero, national_number.slice!(0..::Regexp.last_match(1).size - 1), national_number] if national_number =~ regex

  # Not found.
  #
  super(national_number)
end