annotate LibModuleDBShare-1.0/LibModuleDBShare-1.0.lua @ 20:647cb45f5864

implemented DBG:AddDB()
author Andrew Knoll <andrewtknoll@gmail.com>
date Fri, 15 Mar 2013 03:02:01 -0400
parents ec910729e073
children f2038b9d4d9a
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@15 14 local assert = assert;
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.
@12 28 -- @param usesDualSpec True if this group should use LibDualSpec, false otherwise. (NYI)
andrewtknoll@17 29 -- @param initialProfile The name of the profile to start with. (Defaults to character-specific)
>@5 30 -- @usage
>@5 31 -- local myAddonDBGroup = LibStub("LibModuleDBShare-1.0"):NewGroup("MyAddonGroupName", true)
>@5 32 -- @return the new DB group object
andrewtknoll@11 33 function LibModuleDBShare:NewGroup(groupName, usesDualSpec, initialProfile)
andrewtknoll@17 34 assert(type(groupName) == "string", "Usage: LibModuleDBShare:NewGroup(groupName, usesDualSpec, initialProfile): 'groupName' must be a string.");
andrewtknoll@15 35 assert(type(LibModuleDBShare.groups[groupName]) == "nil", "LibModuleDBShare:NewGroup(groupName, usesDualSpec, initialProfile): 'groupName' already exists");
@12 36 local group = {}
@12 37 group.name = groupName;
andrewtknoll@17 38 group.rootOptionsTable = {
andrewtknoll@17 39 type = "group",
andrewtknoll@17 40 name = groupName,
andrewtknoll@17 41 args = {
andrewtknoll@17 42 text = {
andrewtknoll@17 43 type = "description",
andrewtknoll@17 44 name = "placeholder text.",
andrewtknoll@17 45 },
andrewtknoll@17 46 },
andrewtknoll@17 47 };
andrewtknoll@17 48 AceConfigRegistry:RegisterOptionsTable(groupName, group.rootOptionsTable);
andrewtknoll@17 49 AceConfigDialog:AddToBlizOptions(groupName);
@12 50 group.syncDBTable = {};
andrewtknoll@16 51 group.syncDB = AceDB:New(group.syncDBTable, nil, initialProfile);
andrewtknoll@17 52 group.profileOptionsTable = AceDBOptions:GetOptionsTable(group.syncDB, false);
andrewtknoll@17 53 AceConfigRegistry:RegisterOptionsTable(groupName.."Profiles", group.profileOptionsTable);
andrewtknoll@18 54 AceConfigDialog:AddToBlizOptions(groupName.."Profiles", group.profileOptionsTable.name, groupName);
andrewtknoll@18 55 group.members = {};
@12 56 for k, v in pairs(DBGroup) do
@12 57 group[k] = v;
@12 58 end
andrewtknoll@19 59 group.syncDB.RegisterCallback(group, "OnProfileChanged", "OnProfileChanged");
andrewtknoll@19 60 group.syncDB.RegisterCallback(group, "OnProfileDeleted", "OnProfileDeleted");
andrewtknoll@19 61 group.syncDB.RegisterCallback(group, "OnProfileCopied", "OnProfileCopied");
andrewtknoll@19 62 group.syncDB.RegisterCallback(group, "OnProfileReset", "OnProfileReset");
andrewtknoll@20 63 group.squelchCallbacks = false;
andrewtknoll@19 64 LibModuleDBShare.groups[groupName] = group;
@12 65 return group;
>@4 66 end
>@4 67
>@5 68 --- Retrieves an existing DB group.
>@5 69 -- @param groupName The name of the DB group to retrieve.
>@5 70 -- @usage
>@5 71 -- local myAddonDBGroup = LibStub("LibModuleDBShare-1.0"):GetGroup("MyAddonGroupName")
>@5 72 -- @return the DB group object, or nil if not found
>@5 73 function LibModuleDBShare:GetGroup(groupName)
andrewtknoll@17 74 assert(type(groupName) == "string", "Usage: LibModuleDBShare:GetGroup(groupName): 'groupName' must be a string");
@12 75 return LibModuleDBShare.groups[groupName];
>@4 76 end
>@5 77
>@5 78 --- Adds a database to the group.
andrewtknoll@20 79 -- @param db The database to add.
>@5 80 -- @usage
>@5 81 -- myAddonDBGroup:AddDB(MyAddon.db)
>@5 82 function DBGroup:AddDB(db)
andrewtknoll@20 83 local syncProfile = self.syncDB:GetCurrentProfile();
andrewtknoll@20 84
andrewtknoll@20 85 local shouldDeleteDefault = false; -- if not first DB, then default profile already handled
andrewtknoll@20 86 if type(self.profileTimestamp) == "nil" then
andrewtknoll@20 87 shouldDeleteDefault = true -- first DB added.. might not have default profile
andrewtknoll@20 88 self.profileTimestamp = 0;
andrewtknoll@20 89 end
andrewtknoll@20 90 self.squelchCallbacks = true;
andrewtknoll@20 91 for i, profile in pairs(db:GetProfiles()) do
andrewtknoll@20 92 if profile == "Default" then
andrewtknoll@20 93 shouldDeleteDefault = false;
andrewtknoll@20 94 end
andrewtknoll@20 95 self.syncDB:SetProfile(profile);
andrewtknoll@20 96 end
andrewtknoll@20 97
andrewtknoll@20 98 if db.character.logoutTimestamp > self.profileTimestamp then
andrewtknoll@20 99 self.syncDB:SetProfile(db:GetCurrentProfile());
andrewtknoll@20 100 self.profileTimestamp = db.character.logoutTimestamp;
andrewtknoll@20 101 else
andrewtknoll@20 102 self.syncDB:SetProfile(syncProfile);
andrewtknoll@20 103 end
andrewtknoll@20 104
andrewtknoll@20 105 if shouldDeleteDefault then
andrewtknoll@20 106 self.syncDB:DeleteProfile("Default");
andrewtknoll@20 107 end
andrewtknoll@20 108 self.squelchCallbacks = false;
andrewtknoll@20 109
andrewtknoll@20 110 if self.syncDB:GetCurrentProfile() ~= syncProfile then
andrewtknoll@20 111 self:OnProfileChanged("OnProfileChanged", self.syncDB, self.syncDB:GetCurrentProfile());
andrewtknoll@20 112 end
>@5 113 end
andrewtknoll@18 114
andrewtknoll@19 115 -- callback handlers (new profiles are handled by OnProfileChanged)
andrewtknoll@19 116
andrewtknoll@19 117 function DBGroup:OnProfileChanged(callback, db, profile)
andrewtknoll@19 118 print("Profile Changed");
andrewtknoll@19 119 print(self.name);
andrewtknoll@18 120 print(type(profile));
andrewtknoll@18 121 print(tostring(profile));
andrewtknoll@18 122 end
andrewtknoll@18 123
andrewtknoll@19 124 function DBGroup:OnProfileDeleted(callback, db, profile)
andrewtknoll@19 125 print("Profile Deleted");
andrewtknoll@19 126 print(self.name);
andrewtknoll@18 127 print(type(profile));
andrewtknoll@18 128 print(tostring(profile));
andrewtknoll@18 129 end
andrewtknoll@18 130
andrewtknoll@19 131 function DBGroup:OnProfileCopied(callback, db, profile)
andrewtknoll@19 132 print("Profile Copied");
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:OnProfileReset(callback, db)
andrewtknoll@19 139 print("Profile Reset");
andrewtknoll@19 140 print(self.name);
andrewtknoll@18 141 end