Class: Phony::NationalSplitters::Variable

Inherits:
Fixed
  • Object
show all
Defined in:
lib/phony/national_splitters/variable.rb

Instance Attribute Summary

Attributes inherited from Fixed

#special_splitter

Instance Method Summary collapse

Methods inherited from Fixed

instance_for

Methods inherited from DSL

#>>, #country_for, #reserved

Constructor Details

#initialize(fallback, ndcs, options = {}) ⇒ Variable

Returns a new instance of Variable


9
10
11
12
# File 'lib/phony/national_splitters/variable.rb', line 9

def initialize fallback, ndcs, options = {}
  super fallback, options
  @ndcs = optimize ndcs
end

Instance Method Details

#lengthObject

A valid length.


42
43
44
# File 'lib/phony/national_splitters/variable.rb', line 42

def length
  (@mapped_ndc_min_length..@mapped_ndc_max_length)
end

#split(national_number) ⇒ Object

Takes a national number and splits it into ndc and rest.


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/phony/national_splitters/variable.rb', line 16

def split national_number
  fallback_number = national_number.dup
  
  # Extract a starting point.
  #
  presumed_code = if @mapped_ndc_min_length > 0
    national_number.slice!(0..@mapped_ndc_min_length-1)
  else
    ''
  end
  
  # Try for all possible mapped.
  #
  @mapped_ndc_min_length.upto @mapped_ndc_max_length do |i|
    ndcs_of_size_i = @ndcs[i]
    return [@zero, presumed_code, national_number] unless ndcs_of_size_i && !ndcs_of_size_i.include?(presumed_code)
    presumed_code << national_number.slice!(0..0)
  end
  
  # Not found.
  #
  super fallback_number
end