Class: CompanyNumber::Number
- Inherits:
-
Object
- Object
- CompanyNumber::Number
- Defined in:
- lib/company_number/number.rb
Instance Attribute Summary collapse
-
#company_number ⇒ Object
readonly
Returns the value of attribute company_number.
-
#country_code ⇒ Object
readonly
Returns the value of attribute country_code.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(company_number, country_code = nil) ⇒ Number
constructor
A new instance of Number.
- #to_h ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
- #valid_countries ⇒ Object
- #valid_country? ⇒ Boolean
- #valid_for_country?(country_code) ⇒ Boolean
Constructor Details
#initialize(company_number, country_code = nil) ⇒ Number
Returns a new instance of Number.
7 8 9 10 11 12 13 14 15 |
# File 'lib/company_number/number.rb', line 7 def initialize(company_number, country_code = nil) Validation.check_object_class(company_number, [String]) Validation.check_object_class(country_code, [NilClass, Symbol, String]) Validation.check_iso_code_format(country_code) @company_number = company_number @country_code = country_code&.downcase&.to_sym @metadata = CompanyNumber.dictionary[@country_code] || {} end |
Instance Attribute Details
#company_number ⇒ Object (readonly)
Returns the value of attribute company_number.
5 6 7 |
# File 'lib/company_number/number.rb', line 5 def company_number @company_number end |
#country_code ⇒ Object (readonly)
Returns the value of attribute country_code.
5 6 7 |
# File 'lib/company_number/number.rb', line 5 def country_code @country_code end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
5 6 7 |
# File 'lib/company_number/number.rb', line 5 def @metadata end |
Instance Method Details
#==(other) ⇒ Object
29 30 31 |
# File 'lib/company_number/number.rb', line 29 def ==(other) self.class == other.class && other.to_s == to_s end |
#to_h ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/company_number/number.rb', line 17 def to_h { company_number: @company_number, country_code: @country_code, metadata: @metadata } end |
#to_s ⇒ Object
25 26 27 |
# File 'lib/company_number/number.rb', line 25 def to_s "#{@company_number} #{@country_code}".strip end |
#valid? ⇒ Boolean
33 34 35 36 37 38 39 |
# File 'lib/company_number/number.rb', line 33 def valid? if CompanyNumber.strict_validation? country_code_present_and_valid_country? else no_country_code_or_valid_country? end end |
#valid_countries ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/company_number/number.rb', line 52 def valid_countries return [] if !valid_country? && @country_code CompanyNumber .dictionary .keys .select { |country_code| valid_for_country?(country_code) } end |
#valid_country? ⇒ Boolean
41 42 43 44 |
# File 'lib/company_number/number.rb', line 41 def valid_country? CompanyNumber.dictionary.keys.include?(@country_code) || (!CompanyNumber.strict_validation? && !!@country_code) end |
#valid_for_country?(country_code) ⇒ Boolean
46 47 48 49 50 |
# File 'lib/company_number/number.rb', line 46 def valid_for_country?(country_code) Validation.check_iso_code_format(country_code) regexp = CompanyNumber.dictionary.dig(country_code, :regexp) (!CompanyNumber.strict_validation? && !regexp) || valid_code?(regexp) end |