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 9
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 44
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 16
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 > 0
    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]
    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

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 52
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