There are no reviewed versions of this page, so it may not have been checked for adherence to standards.

Documentation for this module may be created at Module:Infobox road/name/defs/doc

local p = {}

local format = string.format

Name = {formatArgs = {"route"}}

function Name:new(obj)
    obj = obj or {}
    setmetatable(obj, self)
    self.__index = self
    return obj
end

function Name:name(args)
    self:preprocess(args)
    return self:exception(args) or self:default(args)
end

function Name:preprocess(args)
    local preprocessors = self.preprocessors or {}
    for i,preprocessor in ipairs(preprocessors) do
        preprocessor(args)
    end
end

function Name:exception(args)
    local exceptions = self.exceptions or {}
    local route = args.route
    return exceptions[route]
end

function Name:default(args)
    local formatArgs = self.formatArgs
    local formatArguments = {}
    for i,v in ipairs(formatArgs) do
        formatArguments[i] = args[v]
    end
    local formatStr = self.formatStr
    return formatStr and format(formatStr, unpack(formatArguments)) or false
end

Type = Name:new()

function Type:name(args)
    self:preprocess(args)
    return self:fromState(args) or self:exception(args) or self:default(args)
end

function Type:fromState(args)
    local stateName = args.state or args.province
    local state = self[stateName]
    return state and state:name(args) or nil
end

Country = {}

function Country:new(obj)
    obj = obj or {}
    setmetatable(obj, self)
    self.__index = self
    return obj
end

function Country:typeOverride(args)
    return
end

function Country:type(args)
    local type = args.type
    return self:typeOverride(args) or self[type]
end

function Country:name(args)
    local type = self:type(args)
    return type and type:name(args) or error("Invalid type", 0)
end

p.Name = Name
p.Type = Type
p.Country = Country
return p