Class: Addressing::AddressFormat
- Inherits:
-
Object
- Object
- Addressing::AddressFormat
- Defined in:
- lib/addressing/address_format.rb
Constant Summary collapse
- @@address_formats =
The instantiated address formats, keyed by country code.
{}
Instance Attribute Summary collapse
-
#administrative_area_type ⇒ Object
readonly
Returns the value of attribute administrative_area_type.
-
#country_code ⇒ Object
readonly
Returns the value of attribute country_code.
-
#default_values ⇒ Object
readonly
Returns the value of attribute default_values.
-
#dependent_locality_type ⇒ Object
readonly
Returns the value of attribute dependent_locality_type.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#local_format ⇒ Object
readonly
Returns the value of attribute local_format.
-
#locale ⇒ Object
readonly
Returns the value of attribute locale.
-
#locality_type ⇒ Object
readonly
Returns the value of attribute locality_type.
-
#postal_code_pattern ⇒ Object
readonly
Returns the value of attribute postal_code_pattern.
-
#postal_code_prefix ⇒ Object
readonly
Returns the value of attribute postal_code_prefix.
-
#postal_code_type ⇒ Object
readonly
Returns the value of attribute postal_code_type.
-
#required_fields ⇒ Object
readonly
Returns the value of attribute required_fields.
-
#subdivision_depth ⇒ Object
readonly
Returns the value of attribute subdivision_depth.
-
#uppercase_fields ⇒ Object
readonly
Returns the value of attribute uppercase_fields.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(definition = {}) ⇒ AddressFormat
constructor
A new instance of AddressFormat.
-
#used_fields ⇒ Object
Gets the list of used fields.
-
#used_subdivision_fields ⇒ Object
Gets the list of used subdivision fields.
Constructor Details
#initialize(definition = {}) ⇒ AddressFormat
Returns a new instance of AddressFormat.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/addressing/address_format.rb', line 63 def initialize(definition = {}) # Validate the presence of required properties. [:country_code, :format].each do |required_property| if definition[required_property].nil? raise ArgumentError, "Missing required property #{required_property}." end end # Add defaults for properties that are allowed to be empty. definition = { locale: nil, local_format: nil, required_fields: [], uppercase_fields: [], default_values: {}, postal_code_pattern: nil, postal_code_prefix: nil, subdivision_depth: 0 }.merge(definition) AddressField.assert_all_exist(definition[:required_fields]) AddressField.assert_all_exist(definition[:uppercase_fields]) AddressField.assert_all_exist(definition[:default_values].keys) @country_code = definition[:country_code] @locale = definition[:locale] @format = definition[:format] @local_format = definition[:local_format] @required_fields = definition[:required_fields] @uppercase_fields = definition[:uppercase_fields] @default_values = definition[:default_values] @subdivision_depth = definition[:subdivision_depth] if used_fields.include?(AddressField::ADMINISTRATIVE_AREA) if definition[:administrative_area_type] AdministrativeAreaType.assert_exists(definition[:administrative_area_type]) @administrative_area_type = definition[:administrative_area_type] end end if used_fields.include?(AddressField::LOCALITY) if definition[:locality_type] LocalityType.assert_exists(definition[:locality_type]) @locality_type = definition[:locality_type] end end if used_fields.include?(AddressField::DEPENDENT_LOCALITY) if definition[:dependent_locality_type] DependentLocalityType.assert_exists(definition[:dependent_locality_type]) @dependent_locality_type = definition[:dependent_locality_type] end end if used_fields.include?(AddressField::POSTAL_CODE) if definition[:postal_code_type] PostalCodeType.assert_exists(definition[:postal_code_type]) @postal_code_type = definition[:postal_code_type] end @postal_code_pattern = definition[:postal_code_pattern] @postal_code_prefix = definition[:postal_code_prefix] end end |
Instance Attribute Details
#administrative_area_type ⇒ Object (readonly)
Returns the value of attribute administrative_area_type.
61 62 63 |
# File 'lib/addressing/address_format.rb', line 61 def administrative_area_type @administrative_area_type end |
#country_code ⇒ Object (readonly)
Returns the value of attribute country_code.
61 62 63 |
# File 'lib/addressing/address_format.rb', line 61 def country_code @country_code end |
#default_values ⇒ Object (readonly)
Returns the value of attribute default_values.
61 62 63 |
# File 'lib/addressing/address_format.rb', line 61 def default_values @default_values end |
#dependent_locality_type ⇒ Object (readonly)
Returns the value of attribute dependent_locality_type.
61 62 63 |
# File 'lib/addressing/address_format.rb', line 61 def dependent_locality_type @dependent_locality_type end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
61 62 63 |
# File 'lib/addressing/address_format.rb', line 61 def format @format end |
#local_format ⇒ Object (readonly)
Returns the value of attribute local_format.
61 62 63 |
# File 'lib/addressing/address_format.rb', line 61 def local_format @local_format end |
#locale ⇒ Object (readonly)
Returns the value of attribute locale.
61 62 63 |
# File 'lib/addressing/address_format.rb', line 61 def locale @locale end |
#locality_type ⇒ Object (readonly)
Returns the value of attribute locality_type.
61 62 63 |
# File 'lib/addressing/address_format.rb', line 61 def locality_type @locality_type end |
#postal_code_pattern ⇒ Object (readonly)
Returns the value of attribute postal_code_pattern.
61 62 63 |
# File 'lib/addressing/address_format.rb', line 61 def postal_code_pattern @postal_code_pattern end |
#postal_code_prefix ⇒ Object (readonly)
Returns the value of attribute postal_code_prefix.
61 62 63 |
# File 'lib/addressing/address_format.rb', line 61 def postal_code_prefix @postal_code_prefix end |
#postal_code_type ⇒ Object (readonly)
Returns the value of attribute postal_code_type.
61 62 63 |
# File 'lib/addressing/address_format.rb', line 61 def postal_code_type @postal_code_type end |
#required_fields ⇒ Object (readonly)
Returns the value of attribute required_fields.
61 62 63 |
# File 'lib/addressing/address_format.rb', line 61 def required_fields @required_fields end |
#subdivision_depth ⇒ Object (readonly)
Returns the value of attribute subdivision_depth.
61 62 63 |
# File 'lib/addressing/address_format.rb', line 61 def subdivision_depth @subdivision_depth end |
#uppercase_fields ⇒ Object (readonly)
Returns the value of attribute uppercase_fields.
61 62 63 |
# File 'lib/addressing/address_format.rb', line 61 def uppercase_fields @uppercase_fields end |
Class Method Details
.all ⇒ Object
20 21 22 23 24 25 |
# File 'lib/addressing/address_format.rb', line 20 def all definitions.map do |country_code, definition| definition = process_definition(definition) [country_code, new(definition)] end.to_h end |
.get(country_code) ⇒ Object
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/addressing/address_format.rb', line 9 def get(country_code) country_code = country_code.upcase unless @@address_formats.key?(country_code) definition = process_definition(definitions[country_code] || {country_code: country_code}) @@address_formats[country_code] = new(definition) end @@address_formats[country_code] end |
Instance Method Details
#used_fields ⇒ Object
Gets the list of used fields.
129 130 131 132 133 |
# File 'lib/addressing/address_format.rb', line 129 def used_fields @used_fields ||= AddressField.all.filter_map do |key, value| value if @format.include?("%" + value) end end |
#used_subdivision_fields ⇒ Object
Gets the list of used subdivision fields.
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/addressing/address_format.rb', line 136 def used_subdivision_fields fields = [ AddressField::ADMINISTRATIVE_AREA, AddressField::LOCALITY, AddressField::DEPENDENT_LOCALITY ] # Remove fields not used by the format. fields & used_fields end |