People are actually reading this?
Hmm, I never drew any popularity to any of my prior silly weblog attempts until now.It must be the tutorial that I put up. :D
Thanks for all the comments thus far.
Thanks for all the comments thus far.
Experimentation and thoughts on programming, specifically with Erlang and the application domains that it covers.
new_post(Vals) ->
Entry = entries:set_fields_from_strs(entries:new(), Vals),
new_or_edit(Entry, Vals).
new_or_edit(Entry, Vals) ->
EntryV = entries:set_fields_from_strs(Entry, Vals),
Errors = helpers:validate(entries_validate, entries, EntryV),
case Errors of
ok ->
entries:save(EntryV),
{ok, EntryV};
_ ->
{Errors, EntryV}
end.
%% Edit functions.
edit(A, Id) ->
case yaws_arg:method(A) of
'GET' ->
{data, {"", edit_get(Id)}};
'POST' ->
Vals = yaws_api:parse_post(A),
{Errors, Entry} = edit_post(Vals, Id),
case Errors of
ok ->
{ewr, entries, index};
_ ->
{data, {Errors, Entry}}
end
end.
edit_get(Id) ->
Entry = entries:find_id(Id),
Entry.
edit_post(Vals, Id) ->
Entry = entries:find_id(Id),
new_or_edit(Entry, Vals).
form_process(A,
GetAction,
ModelAction,
PostSuccessAction,
PostFailureAction) ->
case yaws_arg:method(A) of
'GET' ->
GetAction();
'POST' ->
Vals = yaws_api:parse_post(A),
{Errors, Record} = ModelAction(Vals),
case Errors of
ok ->
PostSuccessAction();
_ ->
PostFailureAction(Errors, Record)
end
end.
new(A) ->
GetAction = fun() ->
{data, {[], new_get()}} end,
ModelAction = fun(Vals) ->
new_post(Vals) end,
PostSuccessAction = fun() ->
{ewr, entries, index} end,
PostFailureAction = fun(Errors, Entry) ->
{data, {Errors, Entry}} end,
form_process(A, GetAction, ModelAction, PostSuccessAction, PostFailureAction).
edit(A, Id) ->
GetAction = fun() ->
{data, {[], edit_get(Id)}} end,
ModelAction = fun(Vals) ->
edit_post(Vals, Id) end,
PostSuccessAction = fun() ->
{ewr, entries, index} end,
PostFailureAction = fun(Errors, Entry) ->
{data, {Errors, Entry}} end,
form_process(A, GetAction, ModelAction, PostSuccessAction, PostFailureAction).
new(Data) ->
entry_form:display_form(Data, "../new", "").
edit(Data) ->
entry_form:display_form(Data, "../edit",
integer_to_list(entries:id(element(2,Data)))).
<%@ display_form(Items, GoTo, Id) %>
and
< form action="<% GoTo %>/<% Id %>" method="POST" >
view(A, Id) ->
Entry = get_entry(Id),
{data, Entry}.
view(Data) ->
entry_view:view(Data).
<%@ view(Data) %>
< b><% entries:title(Data) %>< /b>
< br />< br />
<% entries:body(Data) %>
< br />
< i>by: <% entries:author(Data) %>< /i>
< br />< br />
<% erlyweb_html:a(["../edit", integer_to_list(entries:id(Data))], "Edit Entry") %>
<% erlyweb_html:a(["view", integer_to_list(entries:id(Entry))], "view") %>
erlyweb:create_app("blog", "/path/to/apps").
create table entries (
id integer auto_increment primary key,
title varchar(100),
body text,
author varchar(100)
);
erlyweb:create_component("entries", "/path/to/apps/blog").
-module(start).
-export([boot/0, boot/1]).
boot() ->
boot(true).
boot(false) ->
compile();
boot(true) ->
mysql_start(),
compile().
mysql_start() ->
erlydb:start(mysql, [{hostname, "localhost"},
{username, "username"},
{password, "password"},
{database, "blog"}]).
compile() ->
erlyweb:compile("/path/to/app/blog",
[{erlydb_driver, mysql}]).
< server junk >
port = 8000
listen = 127.0.0.1
docroot = /path/to/blog/www
appmods = <"/blog", erlyweb>
appname = blog
< /server >
yaws -i
index(A) ->
Entries = entries:find(),
{data, Entries}
-export([index/1]).
index(Data) ->
entries_show:show_entries(Data).
-export([index/1]).
<%@ show_entries(Entries) %>
<% [entry(E) || E <- Entries] %>
-module(helpers).
-export([value/1]).
value(Val) ->
case Val of
undefined ->
"";
_ ->
Val
end.
start:boot().
-module(helpers).
-export([value/1, validate/3]).
value(Val) ->
case Val of
undefined ->
"";
_ ->
Val
end.
validate(ValidatorModule, Model, Item) ->
Fields = Model:use_fields(),
Results = [ValidatorModule:Field(Model:Field(Item)) || Field <- Fields],
Errors = [Error || Error <- Results,element(1, Error) == error],
case Errors of [] ->
ok;
_ ->
Errors
end.
new(A) ->
case yaws_arg:method(A) of
'GET' ->
{data, {[], new_get()}};
'POST' ->
Vals = yaws_api:parse_post(A),
{Errors, Entry} = new_post(Vals),
case Errors of
ok ->
{ewr, index};
_ ->
{data, {Errors, Entry}}
end
end.
new_get() ->
Entry = entries:new(),
Entry.
new_post(Vals) ->
Entry = entries:set_fields_from_strs(entries:new(), Vals),
Errors = helpers:validate(entries_validate, entries, Entry),
case Errors of
ok ->
entries:save(Entry),
{ok, Entry};
_ ->
{Errors, Entry}
end.
-export([index/1, new/1, new_get/0, new_post/1]).
'GET' ->
{data, {[], new_get()}};
...
new_get() ->
Entry = entries:new(),
Entry.
'POST' ->
Vals = yaws_api:parse_post(A),
{Errors, Entry} = new_post(Vals),
case Errors of
ok ->
{ewr, index};
_ ->
{data, {Errors, Entry}}
end
{data, {Errors, Entry}}
new_post(Vals) ->
Entry = entries:set_fields_from_strs(entries:new(), Vals),
Errors = helpers:validate(entries_validate, entries, Entry),
case Errors of
ok ->
entries:save(Entry),
{ok, Entry};
_ ->
{Errors, Entry}
end.
-module(entries_validate).
-export([title/1, body/1, author/1]).
title(undefined) ->
{error, title, "The title field is blank."};
title(_) ->
ok.
body(undefined) ->
{error, body, "The body field is blank."};
body(_) ->
ok.
author(undefined) ->
{error, author, "The author field is blank."};
author(_) ->
ok.
-export([use_fields/0]).
use_fields() ->
[title, body, author].
new(Data) ->
entries_show:display_form(Data).
-export([index/1, new/1]).
<%@ display_form(Items) %>
<%? {Errors, Data} = Items %>
<% helpers_html:show_errors(Errors) %>
< form action="new" method="post">
< table>
< tbody>< tr>< td valign="top">Title:< /td>
< td>
<% erlyweb_html:input("title", text_field, undefined, helpers:value(entries:title(Data))) %>
< /td>
< /tr>
< tr>< td valign="top">Body:< /td>
< td>
<% erlyweb_html:input("body", text_area, undefined, helpers:value(entries:body(Data))) %>
< /td>
< /tr>
< tr>Author:< /td>
< td><% erlyweb_html:input("author", text_field, undefined, helpers:value(entries:author(Data))) %>< /td>
< /tr>
< tr>< input value="Submit" type="submit">< /td>< /tr>
< /tbody>< /table>
< /form>
<%@ show_errors(Errors) %>
< ul>
<% [error(E) || E <- Errors] %>
< /ul>
<%@ error(Error) %>
< li><% element(3, Error) %>< /li>
start:boot().