class Phony::NationalSplitters::Variable

Public Class Methods

new(fallback, ndcs, options = {}) click to toggle source
Calls superclass method Phony::NationalSplitters::Fixed::new
# File lib/phony/national_splitters/variable.rb, line 6
def initialize(fallback, ndcs, options = {})
  super(fallback, options)
  @ndcs = optimize ndcs
end

Public Instance Methods

length() click to toggle source

A valid length.

# File lib/phony/national_splitters/variable.rb, line 45
def length
  (@mapped_ndc_min_length..@mapped_ndc_max_length)
end
split(national_number) click to toggle source

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

Calls superclass method Phony::NationalSplitters::Fixed#split
# 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

Private Instance Methods

optimize(ndc_ary) click to toggle source

Optimizes and restructures the given ndcs array.

# File lib/phony/national_splitters/variable.rb, line 53
def optimize(ndc_ary)
  ndcs = {}
  ndc_ary.each do |ndc|
    ndcs[ndc.length] ||= []
    ndcs[ndc.length] << ndc
  end
  keys = ndcs.keys
  @mapped_ndc_min_length = keys.min # || 1
  @mapped_ndc_max_length = keys.max
  ndcs
end