comparison LibModuleDBShare-1.0/LibModuleDBShare-1.0.lua @ 21:f2038b9d4d9a

rewrote LMDBS:NewGroup() to require an initial DB.
author Andrew Knoll <andrewtknoll@gmail.com>
date Fri, 15 Mar 2013 17:10:57 -0400
parents 647cb45f5864
children 4f5fba631b99
comparison
equal deleted inserted replaced
20:647cb45f5864 21:f2038b9d4d9a
9 local LibModuleDBShare, oldminor = LibStub:NewLibrary(MAJOR, MINOR) 9 local LibModuleDBShare, oldminor = LibStub:NewLibrary(MAJOR, MINOR)
10 10
11 if not LibModuleDBShare then return end -- No upgrade needed 11 if not LibModuleDBShare then return end -- No upgrade needed
12 12
13 -- Lua APIs 13 -- Lua APIs
14 local assert = assert; 14 local assert, type, pairs = assert, type, pairs;
15 15
16 -- Required Libraries 16 -- Required Libraries
17 local AceDB = LibStub("AceDB-3.0"); 17 local AceDB = LibStub("AceDB-3.0");
18 local AceDBOptions = LibStub("AceDBOptions-3.0"); 18 local AceDBOptions = LibStub("AceDBOptions-3.0");
19 local AceConfigRegistry = LibStub("AceConfigRegistry-3.0"); 19 local AceConfigRegistry = LibStub("AceConfigRegistry-3.0");
23 23
24 local DBGroup = {}; 24 local DBGroup = {};
25 25
26 --- Creates a new DB group. 26 --- Creates a new DB group.
27 -- @param groupName The name of the new DB group. 27 -- @param groupName The name of the new DB group.
28 -- @param groupDescription A description of the group to be shown in the root options panel.
29 -- @param initialDB The first DB to add to the group.
28 -- @param usesDualSpec True if this group should use LibDualSpec, false otherwise. (NYI) 30 -- @param usesDualSpec True if this group should use LibDualSpec, false otherwise. (NYI)
29 -- @param initialProfile The name of the profile to start with. (Defaults to character-specific)
30 -- @usage 31 -- @usage
31 -- local myAddonDBGroup = LibStub("LibModuleDBShare-1.0"):NewGroup("MyAddonGroupName", true) 32 -- local myAddonDBGroup = LibStub("LibModuleDBShare-1.0"):NewGroup("MyAddonGroupName", true)
32 -- @return the new DB group object 33 -- @return the new DB group object
33 function LibModuleDBShare:NewGroup(groupName, usesDualSpec, initialProfile) 34 function LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec)
34 assert(type(groupName) == "string", "Usage: LibModuleDBShare:NewGroup(groupName, usesDualSpec, initialProfile): 'groupName' must be a string."); 35 -- verify parameters
35 assert(type(LibModuleDBShare.groups[groupName]) == "nil", "LibModuleDBShare:NewGroup(groupName, usesDualSpec, initialProfile): 'groupName' already exists"); 36 assert(type(groupName) == "string", "Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'groupName' must be a string.");
37 assert(type(groupDescription) == "string", "Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'groupDescription' must be a string.");
38 assert(type(LibModuleDBShare.groups[groupName]) == "nil", "LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): group '"..groupName.."' already exists");
39 assert(type(initialDB) == "table", "LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'initalDB must be a table.");
40 -- create group
36 local group = {} 41 local group = {}
37 group.name = groupName; 42 group.name = groupName;
43 group.members = {};
44 -- create root option panel for group
38 group.rootOptionsTable = { 45 group.rootOptionsTable = {
39 type = "group", 46 type = "group",
40 name = groupName, 47 name = groupName,
41 args = { 48 args = {
42 text = { 49 text = {
43 type = "description", 50 type = "description",
44 name = "placeholder text.", 51 name = groupDescription,
45 }, 52 },
46 }, 53 },
47 }; 54 };
48 AceConfigRegistry:RegisterOptionsTable(groupName, group.rootOptionsTable); 55 AceConfigRegistry:RegisterOptionsTable(groupName, group.rootOptionsTable);
49 AceConfigDialog:AddToBlizOptions(groupName); 56 AceConfigDialog:AddToBlizOptions(groupName);
57 -- create sync DB and profile options page
50 group.syncDBTable = {}; 58 group.syncDBTable = {};
51 group.syncDB = AceDB:New(group.syncDBTable, nil, initialProfile); 59 group.syncDB = AceDB:New(group.syncDBTable, nil, initialDB:GetCurrentProfile());
52 group.profileOptionsTable = AceDBOptions:GetOptionsTable(group.syncDB, false); 60 group.profileOptionsTable = AceDBOptions:GetOptionsTable(group.syncDB, false);
53 AceConfigRegistry:RegisterOptionsTable(groupName.."Profiles", group.profileOptionsTable); 61 AceConfigRegistry:RegisterOptionsTable(groupName.."Profiles", group.profileOptionsTable);
54 AceConfigDialog:AddToBlizOptions(groupName.."Profiles", group.profileOptionsTable.name, groupName); 62 AceConfigDialog:AddToBlizOptions(groupName.."Profiles", group.profileOptionsTable.name, groupName);
55 group.members = {}; 63 -- add all profiles from initialDB to syncDB
64 for i, profile in pairs(initialDB:GetProfiles()) do
65 group.syncDB:SetProfile(profile);
66 end
67 group.syncDB:SetProfile(initialDB:GetCurrentProfile());
68 group.members[initialDB] = true;
69 -- add methods and callbacks
56 for k, v in pairs(DBGroup) do 70 for k, v in pairs(DBGroup) do
57 group[k] = v; 71 group[k] = v;
58 end 72 end
59 group.syncDB.RegisterCallback(group, "OnProfileChanged", "OnProfileChanged"); 73 group.syncDB.RegisterCallback(group, "OnProfileChanged", "OnProfileChanged");
60 group.syncDB.RegisterCallback(group, "OnProfileDeleted", "OnProfileDeleted"); 74 group.syncDB.RegisterCallback(group, "OnProfileDeleted", "OnProfileDeleted");