comparison LibModuleDBShare-1.0/LibModuleDBShare-1.0.lua @ 22:4f5fba631b99

Corrected some assertions. Corrected initial timestamp handling in LMDBS:NewGroup(). Rewrote DBG:AddDB() to remove first DB handling.
author Andrew Knoll <andrewtknoll@gmail.com>
date Fri, 15 Mar 2013 20:42:26 -0400
parents f2038b9d4d9a
children 91ae8cfc63f2
comparison
equal deleted inserted replaced
21:f2038b9d4d9a 22:4f5fba631b99
33 -- @return the new DB group object 33 -- @return the new DB group object
34 function LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec) 34 function LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec)
35 -- verify parameters 35 -- verify parameters
36 assert(type(groupName) == "string", "Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'groupName' must be a string."); 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."); 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"); 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."); 39 assert(type(initialDB) == "table", "LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'initalDB must be a table.");
40 -- create group 40 -- create group
41 local group = {} 41 local group = {}
42 group.name = groupName; 42 group.name = groupName;
43 group.members = {}; 43 group.members = {};
64 for i, profile in pairs(initialDB:GetProfiles()) do 64 for i, profile in pairs(initialDB:GetProfiles()) do
65 group.syncDB:SetProfile(profile); 65 group.syncDB:SetProfile(profile);
66 end 66 end
67 group.syncDB:SetProfile(initialDB:GetCurrentProfile()); 67 group.syncDB:SetProfile(initialDB:GetCurrentProfile());
68 group.members[initialDB] = true; 68 group.members[initialDB] = true;
69 if type(initialDB.character.logoutTimestamp) == "number" then
70 group.profileTimestamp = initialDB.character.logoutTimestamp;
71 else
72 group.profileTimestamp = 0;
73 end
69 -- add methods and callbacks 74 -- add methods and callbacks
70 for k, v in pairs(DBGroup) do 75 for k, v in pairs(DBGroup) do
71 group[k] = v; 76 group[k] = v;
72 end 77 end
73 group.syncDB.RegisterCallback(group, "OnProfileChanged", "OnProfileChanged"); 78 group.syncDB.RegisterCallback(group, "OnProfileChanged", "OnProfileChanged");
83 -- @param groupName The name of the DB group to retrieve. 88 -- @param groupName The name of the DB group to retrieve.
84 -- @usage 89 -- @usage
85 -- local myAddonDBGroup = LibStub("LibModuleDBShare-1.0"):GetGroup("MyAddonGroupName") 90 -- local myAddonDBGroup = LibStub("LibModuleDBShare-1.0"):GetGroup("MyAddonGroupName")
86 -- @return the DB group object, or nil if not found 91 -- @return the DB group object, or nil if not found
87 function LibModuleDBShare:GetGroup(groupName) 92 function LibModuleDBShare:GetGroup(groupName)
88 assert(type(groupName) == "string", "Usage: LibModuleDBShare:GetGroup(groupName): 'groupName' must be a string"); 93 assert(type(groupName) == "string", "Usage: LibModuleDBShare:GetGroup(groupName): 'groupName' must be a string.");
89 return LibModuleDBShare.groups[groupName]; 94 return LibModuleDBShare.groups[groupName];
90 end 95 end
91 96
92 --- Adds a database to the group. 97 --- Adds a database to the group.
93 -- @param db The database to add. 98 -- @param newDB The database to add.
94 -- @usage 99 -- @usage
95 -- myAddonDBGroup:AddDB(MyAddon.db) 100 -- myAddonDBGroup:AddDB(MyAddon.db)
96 function DBGroup:AddDB(db) 101 function DBGroup:AddDB(newDB)
102 -- verify parameters
103 assert(type(newDB) == "table", "Usage: DBGroup:AddDB(newDB): 'newDB' must be a table.");
104 assert(type(self.members[newDB]) == "nil", "DBGroup:AddDB(newDB): 'newDB' is already a member of DBGroup.");
105 -- record current profile
97 local syncProfile = self.syncDB:GetCurrentProfile(); 106 local syncProfile = self.syncDB:GetCurrentProfile();
98 107 -- add new profiles to syncDB
99 local shouldDeleteDefault = false; -- if not first DB, then default profile already handled
100 if type(self.profileTimestamp) == "nil" then
101 shouldDeleteDefault = true -- first DB added.. might not have default profile
102 self.profileTimestamp = 0;
103 end
104 self.squelchCallbacks = true; 108 self.squelchCallbacks = true;
105 for i, profile in pairs(db:GetProfiles()) do 109 for i, profile in pairs(newDB:GetProfiles()) do
106 if profile == "Default" then
107 shouldDeleteDefault = false;
108 end
109 self.syncDB:SetProfile(profile); 110 self.syncDB:SetProfile(profile);
110 end 111 end
111 112 -- set current profile based on timestamps
112 if db.character.logoutTimestamp > self.profileTimestamp then 113 if type(newDB.character.logoutTimestamp) == "number" and newDB.character.logoutTimestamp > self.profileTimestamp then
113 self.syncDB:SetProfile(db:GetCurrentProfile()); 114 self.squelchCallbacks = false;
114 self.profileTimestamp = db.character.logoutTimestamp; 115 self.syncDB:SetProfile(newDB:GetCurrentProfile());
116 self.profileTimestamp = newDB.character.logoutTimestamp;
115 else 117 else
116 self.syncDB:SetProfile(syncProfile); 118 self.syncDB:SetProfile(syncProfile);
119 newDB:SetProfile(syncProfile);
120 self.squelchCallbacks = false;
117 end 121 end
118 122 -- add to members list
119 if shouldDeleteDefault then 123 self.members[newDB] = true;
120 self.syncDB:DeleteProfile("Default");
121 end
122 self.squelchCallbacks = false;
123
124 if self.syncDB:GetCurrentProfile() ~= syncProfile then
125 self:OnProfileChanged("OnProfileChanged", self.syncDB, self.syncDB:GetCurrentProfile());
126 end
127 end 124 end
128 125
129 -- callback handlers (new profiles are handled by OnProfileChanged) 126 -- callback handlers (new profiles are handled by OnProfileChanged)
130 127
131 function DBGroup:OnProfileChanged(callback, db, profile) 128 function DBGroup:OnProfileChanged(callback, db, profile)