class Phony::LocalSplitters::Regex
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.
Attributes
fallback[R]
mapping[R]
Public Class Methods
instance_for(mapping)
click to toggle source
Get a splitter for the given format.
Note: Not cached.
# File lib/phony/local_splitters/regex.rb, line 21 def self.instance_for mapping new mapping end
new(mapping)
click to toggle source
Initialize with a regex => format mapping.
# File lib/phony/local_splitters/regex.rb, line 27 def initialize mapping @fallback = mapping.delete(:fallback) || [12] @mapping = mapping end
Public Instance Methods
plausible?(rest, hints = {})
click to toggle source
# 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)
click to toggle source
Split a local number according to an assumed country specific format.
Examples
-
split ‘3643533’ # => [‘364’, ‘35’, ‘33’] # (Switzerland)
# 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
Private Instance Methods
plausible_with?(number, format)
click to toggle source
# File lib/phony/local_splitters/regex.rb, line 64 def plausible_with? number, format length = format.inject 0, :+ number.length == length end
split_with(number, format)
click to toggle source
# File lib/phony/local_splitters/regex.rb, line 56 def split_with number, format format.inject([]) do |result, size| result << number.slice!(0..size-1) return result if number.empty? result end << number end