Class: Phony::LocalSplitters::Regex

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

Overview

Local splitter class to split the last part of a number, i.e. minus cc or ndc.

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mapping) ⇒ Regex

Initialize with a regex => format mapping.


27
28
29
30
# File 'lib/phony/local_splitters/regex.rb', line 27

def initialize mapping
  @fallback = mapping.delete(:fallback) || [12]
  @mapping  = mapping
end

Instance Attribute Details

#fallbackObject (readonly)

Returns the value of attribute fallback


15
16
17
# File 'lib/phony/local_splitters/regex.rb', line 15

def fallback
  @fallback
end

#mappingObject (readonly)

Returns the value of attribute mapping


15
16
17
# File 'lib/phony/local_splitters/regex.rb', line 15

def mapping
  @mapping
end

Class Method Details

.instance_for(mapping) ⇒ Object

Get a splitter for the given format.

Note: Not cached.


21
22
23
# File 'lib/phony/local_splitters/regex.rb', line 21

def self.instance_for mapping
  new mapping
end

Instance Method Details

#plausible?(rest, hints = {}) ⇒ Boolean


45
46
47
48
49
50
51
52
# File 'lib/phony/local_splitters/regex.rb', line 45

def plausible? rest, hints = {}
  number = rest.inject('', :+)
  mapping.each do |regex, format|
    next unless number =~ regex
    return plausible_with? number, format
  end
  plausible_with? number, fallback
end

#split(number) ⇒ Object

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

Examples

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


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

def split number
  mapping.each do |regex, format|
    next unless number =~ regex
    return split_with(number, format)
  end
  split_with number, fallback
end