View Single Post
07/08/14, 12:15 PM   #3
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Some more suggestions:

Lua Code:
  1. local MAJOR, MINOR = "LibConstantMapper-1.0", 1
  2.  
  3. local lib, oldminor = LibStub:NewLibrary(MAJOR, MINOR)
  4. if not lib then return end
  5.  
  6. local mappingsTable = {
  7.    ["ACTION_TYPE"] = { exclude = "DEPRECATED" },
  8.    ["ATTRIBUTE"] ={ exclude = "ATTRIBUTE_.+_" },
  9.    ["CHAT_CATEGORY"] = { exclude = "^CHAT_CATEGORY_HEADER" },
  10.    ["CURRENCY_CHANGE_REASON"] = {},
  11.    ["DAMAGE_TYPE" = {},
  12.    ["ITEM_QUALITY"] = {},
  13.    ["ITEM_TRAIT_TYPE"] = {},
  14.    ["ITEMSTYLE"] = { exclude = "DEPRECATED" },
  15.    ["ITEMTYPE"] = {},
  16.    ["LINK_TYPE"] = { pattern = "_LINK_TYPE$" }
  17.    ["STAT"] = { exclude = {"^STAT_BONUS_OPTION_", "^STAT_SOFT_CAP_OPTION", "^STAT_VALUE_COLOR"} }
  18. }
  19.  
  20. --do not overwrite existing data
  21. lib.data = lib.data or {}
  22.  
  23. --internal functions
  24. function lib:AddData(mapping, pattern, exclude)
  25.    for name, value in zo_insecurePairs(_G) do
  26.       if type(mapping) == "table" then
  27.          for mappingName, mappingTable in pairs(mapping) do
  28.             lib:SaveDataEntry(name, value, mappingName, mappingTable.pattern, mappingTable.exclude)
  29.          end
  30.       elseif type(mapping) == "string" then
  31.          lib:SaveDataEntry(name, value, mapping, pattern, exclude)
  32.       end
  33.    end
  34. end
  35.  
  36. function lib:SaveDataEntry(name, value, mapping, pattern, exclude)
  37.    if (name):find(pattern or "^"..mapping.."_") == nil then return end
  38.  
  39.    local skip = false
  40.    if exclude then
  41.       if type(exclude) == "table" then
  42.          for i,v in ipairs(exclude) do
  43.             if (name):find(v) then
  44.                skip = true
  45.                break
  46.             end
  47.          end
  48.       else
  49.          skip = (name):find(exclude) ~= nil
  50.       end
  51.    end
  52.    
  53.    if not skip then
  54.       lib.data[mapping] = data[mapping] or {}
  55.       lib.data[mapping][name] = value
  56.    end
  57. end
  58.  
  59. if oldminor then
  60.    --library update, add only new/changed items?
  61.    local newItems = {}
  62.    for name, value in pairs(mappingsTable) do
  63.       if value ~= lib.mappings[name] then
  64.          newItems[name] = value
  65.          lib.mappings = value
  66.       end
  67.    end
  68.    lib:AddData(newItems)
  69. else
  70.    lib.mappings = mappingsTable
  71.    lib:AddData(mappingsTable)
  72. end
  73.  
  74.  
  75. --external functions
  76. function lib:AddMapping(mapping, pattern, exclude)
  77.    --add or replace enxisting entry
  78.    if type(mapping) == "table" then
  79.       for name, value in pairs(mapping) do
  80.          lib.mappings[name] = value
  81.       end
  82.    else  
  83.       lib.mappings[mapping] = { ["pattern"] = pattern, ["exclude"] = exclude }
  84.    end
  85.  
  86.    lib:AddData(mapping, pattern, exclude)  
  87. end
  88.  
  89. function lib:GetConstants(mapping)
  90.    return lib.data[mapping]
  91. end

1) Mapping can be part of the name and pattern is defined only in special cases
2) Do not create a new global table ConstantMapperMappings, just make it accessible using the library reference
3) As merlight said, it would be nice to have direct [value] => "key" mapping, so I changed it that way
  Reply With Quote