Skip to content

Commit

Permalink
Adds page:tostring() function
Browse files Browse the repository at this point in the history
Removes logic from inspect.lp and puts on the page object, creating an useful function that can be used for tracing errors. Page.tostring will pass variables recursively to readable form. For example, breaking lines for table rows and indenting on multi-levels tables.
  • Loading branch information
Etiene committed Oct 7, 2015
1 parent 0c848a7 commit 77c8f84
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 42 deletions.
23 changes: 22 additions & 1 deletion src/sailor.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--------------------------------------------------------------------------------
-- sailor.lua, v0.4.10: core functionalities of the framework
-- sailor.lua, v0.4.11: core functionalities of the framework
-- This file is a part of Sailor project
-- Copyright (c) 2014 Etiene Dalcol <[email protected]>
-- License: MIT
Expand Down Expand Up @@ -98,6 +98,7 @@ function sailor.init(r)
redirect = Page.redirect,
include = Page.include,
inspect = Page.inspect,
tostring = Page.tostring,
write = function(_,...) r:write(...) end,
print = function(_,...) r:puts(...) end,
GET = GET,
Expand Down Expand Up @@ -240,6 +241,26 @@ function Page:inspect(value,message)
end
end

function Page:tostring (val, indent, sep, ln, inspect)
indent = indent or 0
inspect = inspect or ''
sep = sep or '&nbsp;'
ln = ln or "<br/>"

if type(val) ~= "table" then
inspect = inspect .. tostring(val)
else
for k, v in pairs(val) do
if(k ~= "__newindex") then
local formatting = ln..string.rep(sep, indent) .. k .. ": "
inspect = inspect.. formatting
inspect = self:tostring(v, indent+8, sep, ln, inspect)
end
end
end
return inspect
end

-- Auxiliary function to open the autogen page for models and CRUDs
-- page: our page object
local function autogen(page)
Expand Down
21 changes: 1 addition & 20 deletions src/sailor/blank-app/views/error/inspect.lp
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,11 @@
}
</style>

<?lua
local function tstring (val, indent, inspect)
indent = indent or 0
inspect = inspect or ''
if type(val) ~= "table" then
inspect = inspect .. tostring(val)
else
for k, v in pairs(val) do
if(k ~= "__newindex") then
local formatting = "<br/>"..string.rep("&nbsp;", indent) .. k .. ": "
inspect = inspect.. formatting
inspect = tstring(v, indent+8, inspect)
end
end
end
return inspect
end
?>

<table id="sailor_error_inspect">
<?lua for k,v in pairs(page.trace) do ?>
<tr>
<td>
<%= (string.gsub(tstring(v),"<br/>","",1)) %>
<%= (string.gsub(page:tostring(v),"<br/>","",1)) %>
</td>
</tr>
<?lua end ?>
Expand Down
22 changes: 1 addition & 21 deletions test/dev-app/views/error/inspect.lp
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,11 @@
}
</style>

<?lua
local function tstring (val, indent, inspect)
indent = indent or 0
inspect = inspect or ''

if type(val) ~= "table" then
inspect = inspect .. tostring(val)
else
for k, v in pairs(val) do
if(k ~= "__newindex") then
local formatting = "<br/>"..string.rep("&nbsp;", indent) .. k .. ": "
inspect = inspect.. formatting
inspect = tstring(v, indent+8, inspect)
end
end
end
return inspect
end
?>

<table id="sailor_error_inspect">
<?lua for k,v in pairs(page.trace) do ?>
<tr>
<td>
<%= (string.gsub(tstring(v),"<br/>","",1)) %>
<%= (string.gsub(page:tostring(v),"<br/>","",1)) %>
</td>
</tr>
<?lua end ?>
Expand Down

0 comments on commit 77c8f84

Please sign in to comment.