Mercurial > wow > reaction
comparison lib/LibDataBroker-1.1.lua @ 104:4bc50350f405
- Added LDB support
- Updated readme
- Pulled in LibStub and CallbackHandler instead of external
author | Flick <flickerstreak@gmail.com> |
---|---|
date | Fri, 07 Nov 2008 23:55:33 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
103:890e4c4ab143 | 104:4bc50350f405 |
---|---|
1 | |
2 assert(LibStub, "LibDataBroker-1.1 requires LibStub") | |
3 assert(LibStub:GetLibrary("CallbackHandler-1.0", true), "LibDataBroker-1.1 requires CallbackHandler-1.0") | |
4 | |
5 local lib, oldminor = LibStub:NewLibrary("LibDataBroker-1.1", 4) | |
6 if not lib then return end | |
7 oldminor = oldminor or 0 | |
8 | |
9 | |
10 lib.callbacks = lib.callbacks or LibStub:GetLibrary("CallbackHandler-1.0"):New(lib) | |
11 lib.attributestorage, lib.namestorage, lib.proxystorage = lib.attributestorage or {}, lib.namestorage or {}, lib.proxystorage or {} | |
12 local attributestorage, namestorage, callbacks = lib.attributestorage, lib.namestorage, lib.callbacks | |
13 | |
14 if oldminor < 2 then | |
15 lib.domt = { | |
16 __metatable = "access denied", | |
17 __index = function(self, key) return attributestorage[self] and attributestorage[self][key] end, | |
18 } | |
19 end | |
20 | |
21 if oldminor < 3 then | |
22 lib.domt.__newindex = function(self, key, value) | |
23 if not attributestorage[self] then attributestorage[self] = {} end | |
24 if attributestorage[self][key] == value then return end | |
25 attributestorage[self][key] = value | |
26 local name = namestorage[self] | |
27 if not name then return end | |
28 callbacks:Fire("LibDataBroker_AttributeChanged", name, key, value, self) | |
29 callbacks:Fire("LibDataBroker_AttributeChanged_"..name, name, key, value, self) | |
30 callbacks:Fire("LibDataBroker_AttributeChanged_"..name.."_"..key, name, key, value, self) | |
31 callbacks:Fire("LibDataBroker_AttributeChanged__"..key, name, key, value, self) | |
32 end | |
33 end | |
34 | |
35 if oldminor < 2 then | |
36 function lib:NewDataObject(name, dataobj) | |
37 if self.proxystorage[name] then return end | |
38 | |
39 if dataobj then | |
40 assert(type(dataobj) == "table", "Invalid dataobj, must be nil or a table") | |
41 self.attributestorage[dataobj] = {} | |
42 for i,v in pairs(dataobj) do | |
43 self.attributestorage[dataobj][i] = v | |
44 dataobj[i] = nil | |
45 end | |
46 end | |
47 dataobj = setmetatable(dataobj or {}, self.domt) | |
48 self.proxystorage[name], self.namestorage[dataobj] = dataobj, name | |
49 self.callbacks:Fire("LibDataBroker_DataObjectCreated", name, dataobj) | |
50 return dataobj | |
51 end | |
52 end | |
53 | |
54 if oldminor < 1 then | |
55 function lib:DataObjectIterator() | |
56 return pairs(self.proxystorage) | |
57 end | |
58 | |
59 function lib:GetDataObjectByName(dataobjectname) | |
60 return self.proxystorage[dataobjectname] | |
61 end | |
62 | |
63 function lib:GetNameByDataObject(dataobject) | |
64 return self.namestorage[dataobject] | |
65 end | |
66 end | |
67 | |
68 if oldminor < 4 then | |
69 local next = pairs(attributestorage) | |
70 function lib:pairs(dataobject_or_name) | |
71 local t = type(dataobject_or_name) | |
72 assert(t == "string" or t == "table", "Usage: ldb:pairs('dataobjectname') or ldb:pairs(dataobject)") | |
73 | |
74 local dataobj = self.proxystorage[dataobject_or_name] or dataobject_or_name | |
75 assert(attributestorage[dataobj], "Data object not found") | |
76 | |
77 return next, attributestorage[dataobj], nil | |
78 end | |
79 | |
80 local ipairs_iter = ipairs(attributestorage) | |
81 function lib:ipairs(dataobject_or_name) | |
82 local t = type(dataobject_or_name) | |
83 assert(t == "string" or t == "table", "Usage: ldb:ipairs('dataobjectname') or ldb:ipairs(dataobject)") | |
84 | |
85 local dataobj = self.proxystorage[dataobject_or_name] or dataobject_or_name | |
86 assert(attributestorage[dataobj], "Data object not found") | |
87 | |
88 return ipairs_iter, attributestorage[dataobj], 0 | |
89 end | |
90 end |