This is splendid. Rich Hickey is talking about OOP in second part of Clojure for Java Programmers.
At 35:40 he says: [With OOP] you’re set up to do the wrong thing. From the concurrency standpoint its complete catastrophe. It’s a disaster. It’s unworkable. Eventually you’ll die with locks. You either die trying make them work or to try to understand them or just from the stress.
I second that !!!
Recently I got an email which sugessted some people still don’t get what the VanadiumJS is all about. Here is my explanation:
> Vanadium won’t replace the need for server side validation but the client side instant notification is great for usability.
< Vanadium is not intended to replace the server-side validation. But it is not the client-side validation toolkit only. The real value of Vanadium is its ability to integrate both validations. Vanadium splits validation into two phases: testing and decoration. If, for an example, you do client-side validation vanadium performs testing on validated elements or the entire form and produces result of these tests in json format. Then it uses that structure in order to inject appropriate css classes to validated fields and their containers (decoration). On the other hand when your form is valid on client side you make a request to the server and you perform server-side validations there. Rather than rendering the invalid decorated html on the server-side you can pass server-side validation result as a description in very same json format vanadium uses and vanadium will decorate on the client-side the result of server-side tests. This philosophy make extremely easy to create plugins for web frameworks in order to have dynamic, transparent server side validation on the clinet-side. Currently I have RubyOnRails Vanadium plugin on my roadmap. Anther person is interested in developing one for CackePHP.
Said that, now I call all web developers: If you would like to create server-client-validation plugin for web-framework of your choice, I’m happy to help you in explanation of VanadiumJS quirks and magic, mentoring on different levels and in giving general advice and support.
Enjoy!
JSONERL is published.
It is simple tool for turning your erlang records into json and back.
Here is an example of the usage of jsonerl:
1
2
3
4
5
6
| -include("jsonerl.hrl").
-record(artist, {name, year_of_birth, city, photo, movies}).
Artist = #artist{name = <<"Luc Besson">>, year_of_birth = 1959, city = <<"Paris">>, photo = <<"http://is.gd/3pYJF">>, movies = [<<"Taken">>,<<"Bandidas">>,<<"Taxi">>]},
Json = ?record_to_json(artist, Artist),
Artist = ?json_to_record(artist, Json). |
the resulting json will be:
1
2
3
4
5
6
7
| {
name: "Luc Besson",
year_of_birth: 1959,
city: "Paris",
photo: "http://is.gd/3pYJF",
movies: ["Taken","Bandidas","Taxi"]
} |
The code is available at github.
I’m pleased to announce that the first version of demo site presenting my client-side validation toolkit Vanadium is on the go.
Vanadium is declarative, client-side validation toolkit. There is no need for coding. Just use simple markup in side class attributes (or if you are HTML purist you may prefer to attach it as external configuration file in json format).
E.g. for required field one can define it as: <input type="text" class=":required“/>
It also make use of Ajax. E.g checking for username availability can be expressed as:
<input type="text" class=":ajax;/username_checker/check.json“/>
Another handy feature is hierarchical validation regions, lots of build-in validator types and easy way of creating custom ones.
I’ll appreciate any comments on it.
You can give it try on VanadiumJS.com. The code is open source and available on github.
Justin Sheehy posted here his webmachine version of a small but fun web application created originally in mochiweb by Hughes Waroquier from BeeBole. Hughes posted it in a form of neat screencast tutorial. It’s worth watching.
Justin’s version is dated from October 22, 2008 and at that time webmachine version was < 1.0. Since that time the webmachine progressed and API has changed. Moreover, Justin's great example is not a single file but rather code in-lined with post prose. His code has the static content delivery mixed-up with sticky notes resource. It also contained a security issue, which is using list_to_atom bif with arbitrary arguments coming from API user, potentially opening door to blow our erlang VM’s memory up (running out of atoms).
Recently I was asked by my friend to introduce him Erlang webdevelopment. I though immediately about webmachine and the sweet sticky notes tutorial so I decided to create working code of that stuff. I have reused my static resource for webmachine resulting in having separation of Ajax API and static content delivery, reducing the original stickyNotes_webresource.erl to 18 lines of code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| -module(stickyNotes_webresource).
-export([init/1, process_post/2, allowed_methods/2]).
-include_lib("webmachine/include/webmachine.hrl").
init(Config) -> {{trace, "/tmp"}, Config}.
allowed_methods(ReqData, Context) ->
{['POST'], ReqData, Context}.
process_post(ReqData, Context) ->
Ps = mochiweb_util:parse_qs(wrq:req_body(ReqData)),
Struct = mochijson2:decode(proplists:get_value("json", Ps)),
Act = list_to_existing_atom(binary_to_list(struct:get_value(<<"action">>, Struct))),
Resp = wrq:merge_resp_headers([{"Content-Type", "application/json"}], ReqData),
Resp2 = wrq:append_to_response_body(mochijson2:encode(notes:Act(Struct)), Resp),
{true, Resp2, Context}. |
The whole thing can be found at github.
It easy to handle streams of bytes in Erlang. For example consider the following simple task:
given a stream consisting of 3-byte chunks we want to return a stream consisting of those 3-byte chunks whose first byte is zero.
This can be written in the following manner:
keep_0XX(Bin) -> << <<0:1,X:2>> || <<0:1,X:2>> <= Bin >>.
I like it, I hope you too.
[update]
Previously I thought it was undocumented feature of erlang. As Bryan kindly pointed out, one can find it documented here.
Thank you Bryan.
Recently I had a need to use routing definition of one RoR from the other one (don’t ask why it’s irrelevant to this post
)
As you may know Rails routing mappings are stored in global variable ActionController::Routing::Routes. So together with Craig McMillan I’ve hacked really evil piece of code
but it does the job!
So here it goes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| # evil Ruby dynamic variables
class DynamicValue
def initialize(val)
@default_value = val
end
def method_missing(name, *args, &proc)
if tv=Thread.current['DynamicValue']
tv.send( name, *args, &proc)
else
@default_value.send( name, *args, &proc)
end
end
def with_dynamic_value(value)
tmp = Thread.current['DynamicValue']
begin
Thread.current['DynamicValue'] = value
yield
ensure
Thread.current['DynamicValue'] = tmp
end
end
def to_s
"dynamic value"
end
def inspect
to_s
end
end |
One can use it for example like this:
# let's deal with fresh empty RouteSet object
ActionController::Routing::Routes.with_dynamic_value( ActionController::Routing::RouteSet.new ) do
# let's load other RoR routes to it
load File.join(Sonar::SONAR_ROOT, 'other_ror', 'config', 'routes.rb')
# now we can use the other RoR routing definition to generate email tamplate used from the other RoR
email = NewItemsMessage::create_new_message(person)
end
Only thread invoking with_dynamic_value will experience the variable reassignment. For other threads it will be transparent. Once the thread exit the block, again it will see the value in the variable as it was before the trick.
In order to enable that evil you have to invoke once and only once (placing it at the end of config/environment.rb should be fine) the following line:
ActionController::Routing.const_set("Routes", DynamicValue.new(ActionController::Routing::Routes))
Or any other variable substitution. Doing it once is important because applying more than one proxy will discard the original value of the variable.
Enjoy more LISP’y Ruby