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.



6
7
8
9
# File 'lib/phony/national_splitters/variable.rb', line 6

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

Instance Method Details

#lengthObject

A valid length.



45
46
47
# File 'lib/phony/national_splitters/variable.rb', line 45

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.



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

def split(national_number)
  fallback_number = national_number.dup

  # Extract a starting point.
  #
  # This if can possibly be removed.
  #
  presumed_code = if @mapped_ndc_min_length.positive?
                    presumed_code = 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]
    unless ndcs_of_size_i && !ndcs_of_size_i.include?(presumed_code)
      return [@zero, presumed_code,
              national_number]
    end

    presumed_code << national_number.slice!(0..0)
  end

  # Not found.
  #
  super(fallback_number)
end