annotate LibModuleDBShare-1.0/LibModuleDBShare-1.0.lua @ 18:dacd01bf164f

Added profile callbacks. Piggybacked on AceDBOptions' localization for "profiles"
author Andrew Knoll <andrewtknoll@gmail.com>
date Thu, 14 Mar 2013 01:16:01 -0400
parents 4ed8d6827ef7
children ec910729e073
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 = {};
andrewtknoll@18 56 group.syncDB:RegisterCallback(group, "OnNewProfile", "OnNewProfile");
andrewtknoll@18 57 group.syncDB:RegisterCallback(group, "OnProfileChanged", "OnProfileChanged");
andrewtknoll@18 58 group.syncDB:RegisterCallback(group, "OnProfileDeleted", "OnProfileDeleted");
andrewtknoll@18 59 group.syncDB:RegisterCallback(group, "OnProfileCopied", "OnProfileCopied");
andrewtknoll@18 60 group.syncDB:RegisterCallback(group, "OnProfileReset", "OnProfileReset");
@12 61 for k, v in pairs(DBGroup) do
@12 62 group[k] = v;
@12 63 end
@12 64 return group;
>@4 65 end
>@4 66
>@5 67 --- Retrieves an existing DB group.
>@5 68 -- @param groupName The name of the DB group to retrieve.
>@5 69 -- @usage
>@5 70 -- local myAddonDBGroup = LibStub("LibModuleDBShare-1.0"):GetGroup("MyAddonGroupName")
>@5 71 -- @return the DB group object, or nil if not found
>@5 72 function LibModuleDBShare:GetGroup(groupName)
andrewtknoll@17 73 assert(type(groupName) == "string", "Usage: LibModuleDBShare:GetGroup(groupName): 'groupName' must be a string");
@12 74 return LibModuleDBShare.groups[groupName];
>@4 75 end
>@5 76
>@5 77 --- Adds a database to the group.
>@5 78 -- @param db The name of the new DB group.
>@5 79 -- @usage
>@5 80 -- myAddonDBGroup:AddDB(MyAddon.db)
>@5 81 function DBGroup:AddDB(db)
>@5 82
>@5 83 end
andrewtknoll@18 84
andrewtknoll@18 85 function DBGroup:OnNewProfile(db, profile)
andrewtknoll@18 86 print("New Profile");
andrewtknoll@18 87 print(type(profile));
andrewtknoll@18 88 print(tostring(profile));
andrewtknoll@18 89 end
andrewtknoll@18 90
andrewtknoll@18 91 function DBGroup:OnProfileChanged(db, profile)
andrewtknoll@18 92 print("Profile Changed");
andrewtknoll@18 93 print(type(profile));
andrewtknoll@18 94 print(tostring(profile));
andrewtknoll@18 95 end
andrewtknoll@18 96
andrewtknoll@18 97 function DBGroup:OnProfileDeleted(db, profile)
andrewtknoll@18 98 print("Profile Deleted");
andrewtknoll@18 99 print(type(profile));
andrewtknoll@18 100 print(tostring(profile));
andrewtknoll@18 101 end
andrewtknoll@18 102
andrewtknoll@18 103 function DBGroup:OnProfileCopied(db, profile)
andrewtknoll@18 104 print("Profile Copied");
andrewtknoll@18 105 print(type(profile));
andrewtknoll@18 106 print(tostring(profile));
andrewtknoll@18 107 end
andrewtknoll@18 108
andrewtknoll@18 109 function DBGroup:OnProfileReset(db)
andrewtknoll@18 110 print("Profile Reset");
andrewtknoll@18 111 end