Class: Pumi::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/pumi/parser.rb

Defined Under Namespace

Classes: AddressType, AdministrativeDivision

Constant Summary collapse

DEFAULT_DATA_DIRECTORY =
File.join(File.expand_path("..", File.dirname(__dir__)), "data")
PROVINCE =
AdministrativeDivision.new(
  type: Province,
  name: :province,
  data_key: :provinces,
  id_length: 2
)
DISTRICT =
AdministrativeDivision.new(
  type: District,
  name: :district,
  data_key: :districts,
  id_length: 4,
  parent_divisions: [PROVINCE]
)
COMMUNE =
AdministrativeDivision.new(
  type: Commune,
  name: :commune,
  data_key: :communes,
  id_length: 6,
  parent_divisions: [DISTRICT, PROVINCE]
)
VILLAGE =
AdministrativeDivision.new(
  type: Village,
  name: :village,
  data_key: :villages,
  id_length: 8,
  parent_divisions: [COMMUNE, DISTRICT, PROVINCE]
)
ADMINISTRATIVE_DIVISIONS =
{
  Province => PROVINCE,
  District => DISTRICT,
  Commune => COMMUNE,
  Village => VILLAGE
}.freeze
ADDRESS_TYPES =
[
  AddressType.new(locale: :km, default_delimiter: " "),
  AddressType.new(locale: :latin, default_delimiter: ", "),
  AddressType.new(locale: :en, default_delimiter: ", ")
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, data_directory: DEFAULT_DATA_DIRECTORY) ⇒ Parser

Returns a new instance of Parser.



57
58
59
60
61
# File 'lib/pumi/parser.rb', line 57

def initialize(type, data_directory: DEFAULT_DATA_DIRECTORY)
  @type = type
  @data_directory = Pathname(data_directory)
  @administrative_division = ADMINISTRATIVE_DIVISIONS.fetch(type)
end

Instance Attribute Details

#administrative_divisionObject (readonly)

Returns the value of attribute administrative_division.



55
56
57
# File 'lib/pumi/parser.rb', line 55

def administrative_division
  @administrative_division
end

#data_directoryObject (readonly)

Returns the value of attribute data_directory.



55
56
57
# File 'lib/pumi/parser.rb', line 55

def data_directory
  @data_directory
end

#typeObject (readonly)

Returns the value of attribute type.



55
56
57
# File 'lib/pumi/parser.rb', line 55

def type
  @type
end

Instance Method Details

#loadObject



63
64
65
66
67
68
69
70
71
# File 'lib/pumi/parser.rb', line 63

def load
  raw_data.each_with_object({}) do |(id, attributes), result|
    location_data = build_location_data(id, attributes)
    add_parent_divisions(location_data)
    add_addresses(location_data)

    result[id] = type.new(location_data)
  end
end