Class: Countries::PhoneNumbers::DetectorFactory
- Inherits:
-
Object
- Object
- Countries::PhoneNumbers::DetectorFactory
- Defined in:
- lib/countries/phone_numbers/detector_factory.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#detectors ⇒ Object
Returns the value of attribute detectors.
Instance Method Summary collapse
- #add_config(cc) ⇒ Object
- #detector_for(prefix) ⇒ Object
- #detector_for?(prefix) ⇒ Boolean
-
#initialize(config = Countries::PhoneNumbers::DATA_FILE) ⇒ DetectorFactory
constructor
A new instance of DetectorFactory.
- #load_config_file(filename) ⇒ Object
Constructor Details
#initialize(config = Countries::PhoneNumbers::DATA_FILE) ⇒ DetectorFactory
Returns a new instance of DetectorFactory.
5 6 7 8 9 |
# 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 |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
3 4 5 |
# File 'lib/countries/phone_numbers/detector_factory.rb', line 3 def config @config end |
#detectors ⇒ Object
Returns the value of attribute detectors.
3 4 5 |
# File 'lib/countries/phone_numbers/detector_factory.rb', line 3 def detectors @detectors end |
Instance Method Details
#add_config(cc) ⇒ Object
17 18 19 20 21 |
# 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) ⇒ Object
27 28 29 30 31 32 33 |
# 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) ⇒ Boolean
23 24 25 |
# File 'lib/countries/phone_numbers/detector_factory.rb', line 23 def detector_for?( prefix ) config.include? prefix.to_s end |
#load_config_file(filename) ⇒ Object
11 12 13 14 15 |
# 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 |