How can I add a function to a Model module in ChicagoBoss?

26 views
Skip to first unread message

lucafinz...@gmail.com

unread,
Jul 13, 2015, 6:03:32 AM7/13/15
to chica...@googlegroups.com
Hi all,
I am quite new to ChicagoBoss and I am struggling to just add a simple function to an existing model module.
My module is image.erl.

I have a function called create_processed_file():

create_processed_file(FileType, Size)  ->

lager:info("-=> Stepping into create_processed_file <=-"),
OriginalFilename = THIS:get_filename(FileType, Size),
GammaValue = THIS:gamma(),
NewFilename = THIS:apply_gamma_factor(OriginalFilename, GammaValue),
NewFilename.

I added THIS:apply_gamma_factor/2.

The function is defined at the end of the file:
apply_gamma_factor(Filename, Gamma) ->
lager:info("-=> GAMMA PROCESSING : ~p", [Gamma]),
ProcessedFilenamePart = "_gamma_" ++ io_lib:format("~4.2f", [Gamma]),
NewFilename = "/tmp/" ++ filename:rootname(filename:basename(Filename)) ++ ProcessedFilenamePart ++ filename:extension(Filename),
case (Gamma == 1.0) of
true ->
% if gamma == 1.0 let's just copy the original image with the new file name.
lager:info("[apply_gamma_factor] - gamma = 1 - copying ~s to ~s . ", [Filename, NewFilename]),
{ok, _} = file:copy(Filename, NewFilename);
false ->
% if gamma /= 1.0 then we need to create the processed image.
case filelib:is_regular(NewFilename) of
false -> %% send to graphicsmagick for processing
GammaCmd = io_lib:format("gm convert -gamma ~f ~s ~s ",
[Gamma,
Filename,
NewFilename]),
os:cmd(GammaCmd);
true ->
undefined
end
end,
NewFilename.


I cleaned, built and restarted my CB application and every time I get the same error:
13:08:34.929 [info] -=> Stepping into create_processed_file <=-
13:08:34.946 [error] gen_server gsd_report_server terminated with reason: no function clause matching image:apply_gamma_factor("./data/exams/5/c/6/5/a3a9-7762-450e-9dca-f738934cd773/image-5c65a3a9-7762-450e-9dca-f738934cd77...", 2.0, {image,"image-5c65a3a9-7762-450e-9dca-f738934cd773",<<"visible">>,undefined,{{2015,3,26},{14,5,52.0}},...}) line 196
13:08:34.947 [error] CRASH REPORT Process gsd_report_server with 0 neighbours exited with reason: no function clause matching image:apply_gamma_factor("./data/exams/5/c/6/5/a3a9-7762-450e-9dca-f738934cd773/image-5c65a3a9-7762-450e-9dca-f738934cd77...", 2.0, {image,"image-5c65a3a9-7762-450e-9dca-f738934cd773",<<"visible">>,undefined,{{2015,3,26},{14,5,52.0}},...}) line 196 in gen_server:terminate/7 line 804

So it enters the create_processed_file function but somehow cannot 'see' the apply_gamma_factor/2 fuc, instead it looks for a function with more parameters.
Could you please help me in solving this problem?
Thanks in advance

emacstheviking

unread,
Jul 13, 2015, 6:07:45 AM7/13/15
to chica...@googlegroups.com
First glance.... apply_gamma_factor/2 .... at a brief look the failing call seems to be supplying more than two arguments... check your code and make sure you are calling it how it expects to be called perhaps!?

Sean.



--
You received this message because you are subscribed to the Google Groups "ChicagoBoss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chicagoboss...@googlegroups.com.
Visit this group at http://groups.google.com/group/chicagoboss.
To view this discussion on the web visit https://groups.google.com/d/msgid/chicagoboss/6d8cb159-f10d-4c1e-a379-147a40e8b210%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Graeme Defty

unread,
Jul 13, 2015, 11:04:23 PM7/13/15
to chica...@googlegroups.com

It's a while since I got tangled in this stuff, but is it possible that it is related to the fact that models are passed to their methods as the first (hidden) parameter?

Try removing the "THIS:" and I think you may find that it works.
NewFilename = apply_gamma_factor(OriginalFilename, GammaValue),


If you really want the "THIS:" where you have it to explicitly provide the module for the call , try passing it also as the first parameter to the call.

NewFilename = THIS:apply_gamma_factor(THIS, OriginalFilename, GammaValue),

g







Luca Finzi Contini

unread,
Jul 14, 2015, 7:42:42 AM7/14/15
to chica...@googlegroups.com
Thank you Graeme! I will try this one, looks very promising. I was just thinking something similar, i.e. that CB does some compile-time trickery and translates the THIS:function(...) to something like function(..., <all THIS attributes>).
Thank you!

emacstheviking

unread,
Jul 14, 2015, 8:32:20 AM7/14/15
to chica...@googlegroups.com
I believe the "trickery" is pmod_transform, which despite being not taken into the language as a standard feature is widely used by a lot of projects. It's very useful, I used "erlando" for a lot of Erlang stuff and it's great.

Checking therse links out might give you some more information...






Luca Finzi Contini

unread,
Jul 14, 2015, 8:41:13 AM7/14/15
to chica...@googlegroups.com
@emacstheviking, yes I did not mean to be offensive, I am just a newbie in CB development and it struck me as counterintuitive to have 'sort-of' object-oriented-like features like THIS:function() in a natively non-OOP language.
Thank you for the links, I will sure read them as soon as possible!
Luca.

--
You received this message because you are subscribed to a topic in the Google Groups "ChicagoBoss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/chicagoboss/XJHXwGO46BU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to chicagoboss...@googlegroups.com.

emacstheviking

unread,
Jul 14, 2015, 10:26:47 AM7/14/15
to chica...@googlegroups.com
You were not offensive at all, relax!

:)


Evgeny M

unread,
Jul 30, 2015, 3:59:44 AM7/30/15
to ChicagoBoss, lucafinz...@gmail.com

Static methods can be kind of imitated with this snippet, though all the pmod variables would still be bound to id and undefined inside the static_method

Rec = boss_record_lib:dummy_record(Model);
Rec:some_static_method().

Maybe it is possible to create a transform to add a native support of static methods for models

вторник, 14 июля 2015 г., 14:42:42 UTC+3 пользователь Luca Finzi Contini написал:
Reply all
Reply to author
Forward
0 new messages