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 20 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 26 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.sum('') mapping.each do |regex, format| next unless number&.match?(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 36 def split(number) mapping.each do |regex, format| next unless number&.match?(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.sum number.length == length end
split_with(number, format)
click to toggle source
# File lib/phony/local_splitters/regex.rb, line 57 def split_with(number, format) format.each_with_object([]) do |size, result| result << number.slice!(0..size - 1) return result if number.empty? end << number end