Class: Phony::LocalSplitters::Regex
- Inherits:
-
Object
- Object
- Phony::LocalSplitters::Regex
- 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
-
#fallback ⇒ Object
readonly
Returns the value of attribute fallback.
-
#mapping ⇒ Object
readonly
Returns the value of attribute mapping.
Class Method Summary collapse
-
.instance_for(mapping) ⇒ Object
Get a splitter for the given format.
Instance Method Summary collapse
-
#initialize(mapping) ⇒ Regex
constructor
Initialize with a regex => format mapping.
- #plausible?(rest, _hints = {}) ⇒ Boolean
-
#split(number) ⇒ Object
Split a local number according to an assumed country specific format.
Constructor Details
#initialize(mapping) ⇒ Regex
Initialize with a regex => format mapping.
26 27 28 29 |
# File 'lib/phony/local_splitters/regex.rb', line 26 def initialize(mapping) @fallback = mapping.delete(:fallback) || [12] @mapping = mapping end |
Instance Attribute Details
#fallback ⇒ Object (readonly)
Returns the value of attribute fallback.
14 15 16 |
# File 'lib/phony/local_splitters/regex.rb', line 14 def fallback @fallback end |
#mapping ⇒ Object (readonly)
Returns the value of attribute mapping.
14 15 16 |
# File 'lib/phony/local_splitters/regex.rb', line 14 def mapping @mapping end |
Class Method Details
.instance_for(mapping) ⇒ Object
Get a splitter for the given format.
Note: Not cached.
20 21 22 |
# File 'lib/phony/local_splitters/regex.rb', line 20 def self.instance_for(mapping) new mapping end |
Instance Method Details
#plausible?(rest, _hints = {}) ⇒ Boolean
45 46 47 48 49 50 51 52 53 |
# 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) ⇒ Object
Split a local number according to an assumed country specific format.
Examples
-
split ‘3643533’ # => [‘364’, ‘35’, ‘33’] # (Switzerland)
36 37 38 39 40 41 42 43 |
# 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 |