Class: Addressing::AddressFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/addressing/address_format.rb

Constant Summary collapse

@@address_formats =

The instantiated address formats, keyed by country code.

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_typeObject (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_codeObject (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_valuesObject (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_typeObject (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

#formatObject (readonly)

Returns the value of attribute format.



61
62
63
# File 'lib/addressing/address_format.rb', line 61

def format
  @format
end

#local_formatObject (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

#localeObject (readonly)

Returns the value of attribute locale.



61
62
63
# File 'lib/addressing/address_format.rb', line 61

def locale
  @locale
end

#locality_typeObject (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_patternObject (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_prefixObject (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_typeObject (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_fieldsObject (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_depthObject (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_fieldsObject (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

.allObject



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_fieldsObject

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_fieldsObject

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