-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes ordering issue on map schema. refactoring.
- Loading branch information
Showing
3 changed files
with
117 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
local array_schema = {} | ||
|
||
function array_schema:new(obj) | ||
obj = obj or {} | ||
setmetatable(obj, self) | ||
self.__index = self | ||
return obj | ||
end | ||
|
||
function array_schema:trigger_all() | ||
if type(self) ~= "table" or self['on_add'] == nil then return end | ||
for key, value in ipairs(self) do | ||
if key ~= 'on_add' and key ~= 'on_remove' and key ~= 'on_change' then | ||
self['on_add'](value, key) | ||
end | ||
end | ||
end | ||
|
||
function array_schema:clone() | ||
local cloned = array_schema:new(table.clone(self)) | ||
cloned['on_add'] = self['on_add'] | ||
cloned['on_remove'] = self['on_remove'] | ||
cloned['on_change'] = self['on_change'] | ||
return cloned | ||
end | ||
|
||
return array_schema |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
local map_schema = {} | ||
map_schema.__index = map_schema | ||
|
||
function map_schema:new(obj) | ||
obj = { __keys = {} } | ||
setmetatable(obj, map_schema) | ||
return obj | ||
end | ||
|
||
function map_schema:set(key, value) | ||
if value == nil then | ||
-- delete! | ||
for i, k in pairs(self.__keys) do | ||
if k == key then | ||
table.remove(self.__keys, i) | ||
break | ||
end | ||
end | ||
|
||
self[key] = nil | ||
else | ||
-- insert! | ||
if not self[key] then | ||
table.insert(self.__keys, key) | ||
end | ||
self[key] = value | ||
end | ||
end | ||
|
||
function map_schema:length() | ||
return #self.__keys | ||
end | ||
|
||
function map_schema:keys() | ||
return self.__keys | ||
end | ||
|
||
function map_schema:values() | ||
local values = {} | ||
for _, key in ipairs(self.__keys) do | ||
table.insert(values, self[key]) | ||
end | ||
return values | ||
end | ||
|
||
function map_schema:each(cb) | ||
for _, key in ipairs(self.__keys) do | ||
cb(self[key], key) | ||
end | ||
end | ||
|
||
function map_schema:trigger_all() | ||
if type(self) ~= "table" or self['on_add'] == nil then return end | ||
for key, value in pairs(self) do | ||
if key ~= 'on_add' and key ~= 'on_remove' and key ~= 'on_change' and key ~= '__keys' then | ||
self['on_add'](value, key) | ||
end | ||
end | ||
end | ||
|
||
function map_schema:clone() | ||
local cloned = map_schema:new() | ||
|
||
for _, key in ipairs(self.__keys) do | ||
cloned:set(key, self[key]) | ||
end | ||
|
||
cloned['on_add'] = self['on_add'] | ||
cloned['on_remove'] = self['on_remove'] | ||
cloned['on_change'] = self['on_change'] | ||
return cloned | ||
end | ||
|
||
return map_schema |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters