Class: Addressing::Subdivision

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

Constant Summary collapse

@@definitions =

Subdivision definitions.

{}
@@parents =

Parent subdivisions.

Used as a cache to speed up instantiating subdivisions with the same parent. Contains only parents instead of all instantiated subdivisions to minimize duplicating the data in $this->definitions, thus reducing memory usage.

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition = {}) ⇒ Subdivision

Returns a new instance of Subdivision.



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/addressing/subdivision.rb', line 201

def initialize(definition = {})
  # Validate the presence of required properties.
  [:country_code, :id, :code, :name].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 = {
    parent: nil,
    locale: nil,
    local_code: nil,
    local_name: nil,
    postal_code_pattern: nil,
    children: {}
  }.merge(definition)

  @id = definition[:id]
  @parent = definition[:parent]
  @country_code = definition[:country_code]
  @locale = definition[:locale]
  @code = definition[:code]
  @local_code = definition[:local_code]
  @name = definition[:name]
  @local_name = definition[:local_name]
  @postal_code_pattern = definition[:postal_code_pattern]
  @children = definition[:children]
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



199
200
201
# File 'lib/addressing/subdivision.rb', line 199

def children
  @children
end

#codeObject (readonly)

Returns the value of attribute code.



199
200
201
# File 'lib/addressing/subdivision.rb', line 199

def code
  @code
end

#country_codeObject (readonly)

Returns the value of attribute country_code.



199
200
201
# File 'lib/addressing/subdivision.rb', line 199

def country_code
  @country_code
end

#idObject (readonly)

Returns the value of attribute id.



199
200
201
# File 'lib/addressing/subdivision.rb', line 199

def id
  @id
end

#local_codeObject (readonly)

Returns the value of attribute local_code.



199
200
201
# File 'lib/addressing/subdivision.rb', line 199

def local_code
  @local_code
end

#local_nameObject (readonly)

Returns the value of attribute local_name.



199
200
201
# File 'lib/addressing/subdivision.rb', line 199

def local_name
  @local_name
end

#localeObject (readonly)

Returns the value of attribute locale.



199
200
201
# File 'lib/addressing/subdivision.rb', line 199

def locale
  @locale
end

#nameObject (readonly)

Returns the value of attribute name.



199
200
201
# File 'lib/addressing/subdivision.rb', line 199

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



199
200
201
# File 'lib/addressing/subdivision.rb', line 199

def parent
  @parent
end

#postal_code_patternObject (readonly)

Returns the value of attribute postal_code_pattern.



199
200
201
# File 'lib/addressing/subdivision.rb', line 199

def postal_code_pattern
  @postal_code_pattern
end

Class Method Details

.all(parents) ⇒ Object

Returns all subdivision instances for the provided parents.



23
24
25
26
27
28
29
30
# File 'lib/addressing/subdivision.rb', line 23

def all(parents)
  definitions = load_definitions(parents)
  return {} if definitions.empty?

  definitions["subdivisions"].each_with_object({}) do |(id, definition), subdivisions|
    subdivisions[id] = create_subdivision_from_definitions(id, definitions)
  end
end

.get(id, parents) ⇒ Object



17
18
19
20
# File 'lib/addressing/subdivision.rb', line 17

def get(id, parents)
  definitions = load_definitions(parents)
  create_subdivision_from_definitions(id, definitions)
end

.list(parents, locale = nil) ⇒ Object

Returns a list of subdivisions for the provided parents.



33
34
35
36
37
38
39
40
41
42
# File 'lib/addressing/subdivision.rb', line 33

def list(parents, locale = nil)
  definitions = load_definitions(parents)
  return {} if definitions.empty?

  use_local_name = Locale.match_candidates(locale, definitions["locale"] || "")

  definitions["subdivisions"].each_with_object({}) do |(id, definition), subdivisions|
    subdivisions[id] = use_local_name ? definition["local_name"] : definition["name"]
  end
end

Instance Method Details

#children?Boolean

Returns:

  • (Boolean)


231
232
233
# File 'lib/addressing/subdivision.rb', line 231

def children?
  @children.any?
end

#to_hObject



235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/addressing/subdivision.rb', line 235

def to_h
  {
    id: id,
    parent: parent,
    country_code: country_code,
    locale: locale,
    code: code,
    local_code: local_code,
    name: name,
    local_name: local_name,
    postal_code_pattern: postal_code_pattern,
    children: children
  }
end