Class: TelephoneNumber::Country
- Inherits:
-
Object
- Object
- TelephoneNumber::Country
- Defined in:
- lib/telephone_number/country.rb
Constant Summary collapse
- MOBILE_TOKEN_COUNTRIES =
{ AR: '9' }.freeze
Instance Attribute Summary collapse
-
#country_code ⇒ Object
readonly
Returns the value of attribute country_code.
-
#country_id ⇒ Object
readonly
Returns the value of attribute country_id.
-
#formats ⇒ Object
readonly
Returns the value of attribute formats.
-
#general_validation ⇒ Object
readonly
Returns the value of attribute general_validation.
-
#international_prefix ⇒ Object
readonly
Returns the value of attribute international_prefix.
-
#main_country_for_code ⇒ Object
readonly
Returns the value of attribute main_country_for_code.
-
#mobile_token ⇒ Object
readonly
Returns the value of attribute mobile_token.
-
#national_prefix ⇒ Object
readonly
Returns the value of attribute national_prefix.
-
#national_prefix_for_parsing ⇒ Object
readonly
Returns the value of attribute national_prefix_for_parsing.
-
#national_prefix_transform_rule ⇒ Object
readonly
Returns the value of attribute national_prefix_transform_rule.
-
#validations ⇒ Object
readonly
Returns the value of attribute validations.
Class Method Summary collapse
- .all_countries ⇒ Object
- .deep_merge(base_hash, other_hash) ⇒ Object
- .find(country_id) ⇒ Object
- .load_data ⇒ Object
- .phone_data ⇒ Object
Instance Method Summary collapse
- #detect_format(number) ⇒ Object
- #full_general_pattern ⇒ Object
-
#initialize(data_hash) ⇒ Country
constructor
A new instance of Country.
- #parent_country ⇒ Object
Constructor Details
#initialize(data_hash) ⇒ Country
Returns a new instance of Country.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/telephone_number/country.rb', line 9 def initialize(data_hash) validations = data_hash.fetch(:validations, {}).dup @country_code = data_hash[:country_code] @country_id = data_hash[:id] @general_validation = NumberValidation.new(:general_desc, validations[:general_desc]) if validations[:general_desc] @international_prefix = Regexp.new(data_hash[:international_prefix]) if data_hash[:international_prefix] @main_country_for_code = data_hash[:main_country_for_code] == 'true' @mobile_token = MOBILE_TOKEN_COUNTRIES[@country_id.to_sym] @national_prefix = data_hash[:national_prefix] @national_prefix_for_parsing = Regexp.new(data_hash[:national_prefix_for_parsing]) if data_hash[:national_prefix_for_parsing] @national_prefix_transform_rule = data_hash[:national_prefix_transform_rule] @validations = validations.delete_if { |name, _| name == :general_desc || name == :area_code_optional } .map { |name, data| NumberValidation.new(name, data) } @formats = data_hash.fetch(:formats, []).map { |format| NumberFormat.new(format, data_hash[:national_prefix_formatting_rule]) } end |
Instance Attribute Details
#country_code ⇒ Object (readonly)
Returns the value of attribute country_code.
3 4 5 |
# File 'lib/telephone_number/country.rb', line 3 def country_code @country_code end |
#country_id ⇒ Object (readonly)
Returns the value of attribute country_id.
3 4 5 |
# File 'lib/telephone_number/country.rb', line 3 def country_id @country_id end |
#formats ⇒ Object (readonly)
Returns the value of attribute formats.
3 4 5 |
# File 'lib/telephone_number/country.rb', line 3 def formats @formats end |
#general_validation ⇒ Object (readonly)
Returns the value of attribute general_validation.
3 4 5 |
# File 'lib/telephone_number/country.rb', line 3 def general_validation @general_validation end |
#international_prefix ⇒ Object (readonly)
Returns the value of attribute international_prefix.
3 4 5 |
# File 'lib/telephone_number/country.rb', line 3 def international_prefix @international_prefix end |
#main_country_for_code ⇒ Object (readonly)
Returns the value of attribute main_country_for_code.
3 4 5 |
# File 'lib/telephone_number/country.rb', line 3 def main_country_for_code @main_country_for_code end |
#mobile_token ⇒ Object (readonly)
Returns the value of attribute mobile_token.
3 4 5 |
# File 'lib/telephone_number/country.rb', line 3 def mobile_token @mobile_token end |
#national_prefix ⇒ Object (readonly)
Returns the value of attribute national_prefix.
3 4 5 |
# File 'lib/telephone_number/country.rb', line 3 def national_prefix @national_prefix end |
#national_prefix_for_parsing ⇒ Object (readonly)
Returns the value of attribute national_prefix_for_parsing.
3 4 5 |
# File 'lib/telephone_number/country.rb', line 3 def national_prefix_for_parsing @national_prefix_for_parsing end |
#national_prefix_transform_rule ⇒ Object (readonly)
Returns the value of attribute national_prefix_transform_rule.
3 4 5 |
# File 'lib/telephone_number/country.rb', line 3 def national_prefix_transform_rule @national_prefix_transform_rule end |
#validations ⇒ Object (readonly)
Returns the value of attribute validations.
3 4 5 |
# File 'lib/telephone_number/country.rb', line 3 def validations @validations end |
Class Method Details
.all_countries ⇒ Object
75 76 77 |
# File 'lib/telephone_number/country.rb', line 75 def self.all_countries @all_countries ||= phone_data.values.map {|data| new(data)} end |
.deep_merge(base_hash, other_hash) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/telephone_number/country.rb', line 64 def self.deep_merge(base_hash, other_hash) other_hash.each do |key, value| base_hash[key] = if base_hash[key].is_a?(Hash) && value.is_a?(Hash) deep_merge(base_hash[key], value) else value end end base_hash end |
.find(country_id) ⇒ Object
49 50 51 52 |
# File 'lib/telephone_number/country.rb', line 49 def self.find(country_id) data = phone_data[country_id.to_s.upcase.to_sym] new(data) if data end |
.load_data ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/telephone_number/country.rb', line 54 def self.load_data data_path = "#{File.dirname(__FILE__)}/../../data/telephone_number_data_file.dat" main_data = Marshal.load(File.binread(data_path)) if TelephoneNumber.override_file override_data = Marshal.load(File.binread(TelephoneNumber.override_file)) main_data = deep_merge(main_data, override_data) end main_data end |
.phone_data ⇒ Object
45 46 47 |
# File 'lib/telephone_number/country.rb', line 45 def self.phone_data @phone_data ||= load_data end |
Instance Method Details
#detect_format(number) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/telephone_number/country.rb', line 25 def detect_format(number) native_format = formats.detect do |format| Regexp.new("^(#{format.leading_digits})").match?(number) && Regexp.new("^(#{format.pattern})$").match?(number) end return native_format if native_format || main_country_for_code parent_country.detect_format(number) if parent_country end |
#full_general_pattern ⇒ Object
41 42 43 |
# File 'lib/telephone_number/country.rb', line 41 def full_general_pattern %r{^(#{country_code})?(#{national_prefix})?(?<national_num>#{general_validation.pattern})$} end |
#parent_country ⇒ Object
34 35 36 37 38 39 |
# File 'lib/telephone_number/country.rb', line 34 def parent_country return if main_country_for_code Country.all_countries.detect do |country| country.country_code == self.country_code && country.main_country_for_code end end |