class Countries::PhoneNumbers::DetectorFactory

Attributes

config[RW]
detectors[RW]

Public Class Methods

new( config=Countries::PhoneNumbers::DATA_FILE ) click to toggle source
# File lib/countries/phone_numbers/detector_factory.rb, line 5
def initialize( config=Countries::PhoneNumbers::DATA_FILE )
  @config    = config.is_a?(Hash) ? config : {}
  @detectors = {}
  load_config_file( config ) if config.is_a?(String)
end

Public Instance Methods

add_config( cc ) click to toggle source
# File lib/countries/phone_numbers/detector_factory.rb, line 17
def add_config( cc )
  key = cc['applies_to'].to_s
  config[key] = cc
  detectors.delete(key)
end
detector_for( prefix ) click to toggle source
# File lib/countries/phone_numbers/detector_factory.rb, line 27
def detector_for( prefix )
  prefix = prefix.to_s
  if detector_for? prefix
    detectors[prefix] = build_detector( config[prefix] ) unless detectors.include? prefix
  end
  return detectors[prefix]
end
detector_for?( prefix ) click to toggle source
# File lib/countries/phone_numbers/detector_factory.rb, line 23
def detector_for?( prefix )
  config.include? prefix.to_s
end
load_config_file( filename ) click to toggle source
# File lib/countries/phone_numbers/detector_factory.rb, line 11
def load_config_file( filename )
  File.open( filename ) do |file|
    YAML.load_documents( file ) { |doc| add_config( doc ) }
  end
end

Protected Instance Methods

build_detector( config ) click to toggle source
# File lib/countries/phone_numbers/detector_factory.rb, line 37
def build_detector( config )
  # Build a new config tool based on the given strategy
  return case
    when config.include?('start_with')
      Countries::PhoneNumbers::StartWithDetector.new config
    when config.include?('one_of')
      Countries::PhoneNumbers::OneOfDetector.new config
    else
      Countries::PhoneNumbers::Detector.new config
    end
end