[erlang-questions] parameterized modules alternative in chicago boss like use case

Pablo Platt pablo.platt@REDACTED
Mon Jul 4 08:05:55 CEST 2011

I read about the pros and cons of pmods and wanted to find an alternative.
In mochiweb, pmods doesn't look essential.
You can replace all the Req:something type of calls with mochiweb_request:something(Req, Args)

Chicago Boss uses pmods in the following way:
>Greetings = boss_db:find(greeting, []).
[{greeting, "greeting-1", "Boss says Hello!"}] 
>Greeting = hd(Greetings).
{greeting, "greeting-1", "Boss says Hello!"}
"Boss says Hello!"

The powerful part is being able to use the model in an erl_dtl template:
{% for greeting in greetings %}
<li>{{ greeting.text }}</li>
{% endfor %}

Using pmods you can pass different types of models with common fields and use them in the same template.
For example, models of several product types that have name and price fields.
You can also load data dynamically in the template. A many-to-many field(function) in the pmod can load data from the db and pass an 'instance' of another pmod.

Alternative to pmods in that case could be records, proplist/dict, gen_server.

- records

To use a record, we need to tell the template about the record type.
{% for p#product in products %}
That's fine but we can only use the same template for several model types
if the records of all product types have the same structure:
{product_type1, {name, price, some_field}}
{product_type2, {name, price, another_field}}
We can't 'extend' a record in erlang so we'll have to manually make write the structure of all related records which will be hard in a large application.

- proplist/dict
This will probably work but I'm not sure about performance.
For small models' we probably want to use proplists and for large models a dict.
I'm not sure what small and large are in this case.
We can't dynamically load data in the template unless we define the model type in the template.
{% for product#some_type in group_product.children %}

- gen_server
Might work but creating 100 gen_servers just to show 100 products feels like big overhead.

Any ideas?


More information about the erlang-questions mailing list