annotate 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
rev   line source
>@5 1 --- **LibModuleDBShare-1.0**\\
>@5 2 -- A description will eventually be here.
>@5 3 --
>@5 4 -- @usage
>@5 5 -- Also coming soon.
>@5 6 -- @class file
>@5 7 -- @name LibModuleDBShare-1.0.lua
>@3 8 local MAJOR, MINOR = "LibModuleDBShare-1.0", 1
>@3 9 local LibModuleDBShare, oldminor = LibStub:NewLibrary(MAJOR, MINOR)
>@3 10
>@4 11 if not LibModuleDBShare then return end -- No upgrade needed
>@4 12
andrewtknoll@15 13 -- Lua APIs
andrewtknoll@21 14 local assert, type, pairs = assert, type, pairs;
andrewtknoll@15 15
andrewtknoll@17 16 -- Required Libraries
@12 17 local AceDB = LibStub("AceDB-3.0");
andrewtknoll@17 18 local AceDBOptions = LibStub("AceDBOptions-3.0");
andrewtknoll@17 19 local AceConfigRegistry = LibStub("AceConfigRegistry-3.0");
andrewtknoll@17 20 local AceConfigDialog = LibStub("AceConfigDialog-3.0");
@12 21
>@4 22 LibModuleDBShare.groups = LibModuleDBShare.groups or {};
>@4 23
>@5 24 local DBGroup = {};
>@5 25
>@5 26 --- Creates a new DB group.
>@5 27 -- @param groupName The name of the new DB group.
andrewtknoll@21 28 -- @param groupDescription A description of the group to be shown in the root options panel.
andrewtknoll@21 29 -- @param initialDB The first DB to add to the group.
@12 30 -- @param usesDualSpec True if this group should use LibDualSpec, false otherwise. (NYI)
>@5 31 -- @usage
>@5 32 -- local myAddonDBGroup = LibStub("LibModuleDBShare-1.0"):NewGroup("MyAddonGroupName", true)
>@5 33 -- @return the new DB group object
andrewtknoll@21 34 function LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec)
andrewtknoll@21 35 -- verify parameters
andrewtknoll@21 36 assert(type(groupName) == "string", "Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'groupName' must be a string.");
andrewtknoll@21 37 assert(type(groupDescription) == "string", "Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'groupDescription' must be a string.");
andrewtknoll@21 38 assert(type(LibModuleDBShare.groups[groupName]) == "nil", "LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): group '"..groupName.."' already exists");
andrewtknoll@21 39 assert(type(initialDB) == "table", "LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'initalDB must be a table.");
andrewtknoll@21 40 -- create group
@12 41 local group = {}
@12 42 group.name = groupName;
andrewtknoll@21 43 group.members = {};
andrewtknoll@21 44 -- create root option panel for group
andrewtknoll@17 45 group.rootOptionsTable = {
andrewtknoll@17 46 type = "group",
andrewtknoll@17 47 name = groupName,
andrewtknoll@17 48 args = {
andrewtknoll@17 49 text = {
andrewtknoll@17 50 type = "description",
andrewtknoll@21 51 name = groupDescription,
andrewtknoll@17 52 },
andrewtknoll@17 53 },
andrewtknoll@17 54 };
andrewtknoll@17 55 AceConfigRegistry:RegisterOptionsTable(groupName, group.rootOptionsTable);
andrewtknoll@17 56 AceConfigDialog:AddToBlizOptions(groupName);
andrewtknoll@21 57 -- create sync DB and profile options page
@12 58 group.syncDBTable = {};
andrewtknoll@21 59 group.syncDB = AceDB:New(group.syncDBTable, nil, initialDB:GetCurrentProfile());
andrewtknoll@17 60 group.profileOptionsTable = AceDBOptions:GetOptionsTable(group.syncDB, false);
andrewtknoll@17 61 AceConfigRegistry:RegisterOptionsTable(groupName.."Profiles", group.profileOptionsTable);
andrewtknoll@18 62 AceConfigDialog:AddToBlizOptions(groupName.."Profiles", group.profileOptionsTable.name, groupName);
andrewtknoll@21 63 -- add all profiles from initialDB to syncDB
andrewtknoll@21 64 for i, profile in pairs(initialDB:GetProfiles()) do
andrewtknoll@21 65 group.syncDB:SetProfile(profile);
andrewtknoll@21 66 end
andrewtknoll@21 67 group.syncDB:SetProfile(initialDB:GetCurrentProfile());
andrewtknoll@21 68 group.members[initialDB] = true;
andrewtknoll@21 69 -- add methods and callbacks
@12 70 for k, v in pairs(DBGroup) do
@12 71 group[k] = v;
@12 72 end
andrewtknoll@19 73 group.syncDB.RegisterCallback(group, "OnProfileChanged", "OnProfileChanged");
andrewtknoll@19 74 group.syncDB.RegisterCallback(group, "OnProfileDeleted", "OnProfileDeleted");
andrewtknoll@19 75 group.syncDB.RegisterCallback(group, "OnProfileCopied", "OnProfileCopied");
andrewtknoll@19 76 group.syncDB.RegisterCallback(group, "OnProfileReset", "OnProfileReset");
andrewtknoll@20 77 group.squelchCallbacks = false;
andrewtknoll@19 78 LibModuleDBShare.groups[groupName] = group;
@12 79 return group;
>@4 80 end
>@4 81
>@5 82 --- Retrieves an existing DB group.
>@5 83 -- @param groupName The name of the DB group to retrieve.
>@5 84 -- @usage
>@5 85 -- local myAddonDBGroup = LibStub("LibModuleDBShare-1.0"):GetGroup("MyAddonGroupName")
>@5 86 -- @return the DB group object, or nil if not found
>@5 87 function LibModuleDBShare:GetGroup(groupName)
andrewtknoll@17 88 assert(type(groupName) == "string", "Usage: LibModuleDBShare:GetGroup(groupName): 'groupName' must be a string");
@12 89 return LibModuleDBShare.groups[groupName];
>@4 90 end
>@5 91
>@5 92 --- Adds a database to the group.
andrewtknoll@20 93 -- @param db The database to add.
>@5 94 -- @usage
>@5 95 -- myAddonDBGroup:AddDB(MyAddon.db)
>@5 96 function DBGroup:AddDB(db)
andrewtknoll@20 97 local syncProfile = self.syncDB:GetCurrentProfile();
andrewtknoll@20 98
andrewtknoll@20 99 local shouldDeleteDefault = false; -- if not first DB, then default profile already handled
andrewtknoll@20 100 if type(self.profileTimestamp) == "nil" then
andrewtknoll@20 101 shouldDeleteDefault = true -- first DB added.. might not have default profile
andrewtknoll@20 102 self.profileTimestamp = 0;
andrewtknoll@20 103 end
andrewtknoll@20 104 self.squelchCallbacks = true;
andrewtknoll@20 105 for i, profile in pairs(db:GetProfiles()) do
andrewtknoll@20 106 if profile == "Default" then
andrewtknoll@20 107 shouldDeleteDefault = false;
andrewtknoll@20 108 end
andrewtknoll@20 109 self.syncDB:SetProfile(profile);
andrewtknoll@20 110 end
andrewtknoll@20 111
andrewtknoll@20 112 if db.character.logoutTimestamp > self.profileTimestamp then
andrewtknoll@20 113 self.syncDB:SetProfile(db:GetCurrentProfile());
andrewtknoll@20 114 self.profileTimestamp = db.character.logoutTimestamp;
andrewtknoll@20 115 else
andrewtknoll@20 116 self.syncDB:SetProfile(syncProfile);
andrewtknoll@20 117 end
andrewtknoll@20 118
andrewtknoll@20 119 if shouldDeleteDefault then
andrewtknoll@20 120 self.syncDB:DeleteProfile("Default");
andrewtknoll@20 121 end
andrewtknoll@20 122 self.squelchCallbacks = false;
andrewtknoll@20 123
andrewtknoll@20 124 if self.syncDB:GetCurrentProfile() ~= syncProfile then
andrewtknoll@20 125 self:OnProfileChanged("OnProfileChanged", self.syncDB, self.syncDB:GetCurrentProfile());
andrewtknoll@20 126 end
>@5 127 end
andrewtknoll@18 128
andrewtknoll@19 129 -- callback handlers (new profiles are handled by OnProfileChanged)
andrewtknoll@19 130
andrewtknoll@19 131 function DBGroup:OnProfileChanged(callback, db, profile)
andrewtknoll@19 132 print("Profile Changed");
andrewtknoll@19 133 print(self.name);
andrewtknoll@18 134 print(type(profile));
andrewtknoll@18 135 print(tostring(profile));
andrewtknoll@18 136 end
andrewtknoll@18 137
andrewtknoll@19 138 function DBGroup:OnProfileDeleted(callback, db, profile)
andrewtknoll@19 139 print("Profile Deleted");
andrewtknoll@19 140 print(self.name);
andrewtknoll@18 141 print(type(profile));
andrewtknoll@18 142 print(tostring(profile));
andrewtknoll@18 143 end
andrewtknoll@18 144
andrewtknoll@19 145 function DBGroup:OnProfileCopied(callback, db, profile)
andrewtknoll@19 146 print("Profile Copied");
andrewtknoll@19 147 print(self.name);
andrewtknoll@18 148 print(type(profile));
andrewtknoll@18 149 print(tostring(profile));
andrewtknoll@18 150 end
andrewtknoll@18 151
andrewtknoll@19 152 function DBGroup:OnProfileReset(callback, db)
andrewtknoll@19 153 print("Profile Reset");
andrewtknoll@19 154 print(self.name);
andrewtknoll@18 155 end