Rails frontend developer toolset
Tequila
Features
Installation
./script/plugin git://github.com/inem/tequila.git
After that drop some Tequila code into an apropriate template (should have .jazz extension) - and you are there!
Sandbox
Here are some prooflinks, where you can see, how JSON output differs for different jazz templates.
Don't forget to turn on you firebug/safari console
Basic syntax
Have you ever used to_json method in your Rails app? If so, you should be familiar with basic Tequila syntax. You still have your :only, :except and :methods keywords. Instead of writing
:include => { :association1 => {...}}
you should just remember that every variable definition must be started '-' and every association with '+'. So example above can be written much shortly:
+association1
Advanced
Labels
Label can be defined via expression (=> label) which can be added everywhere where it makes sense. It means that you can add label to such elements as :code, +association, etc.. and can not add label to :except declaration and :gluening object. Some functionality of these features are covered by standart Rails to_json method and some aren't. For instance, if you bind alias 'animals' to assoication 'pets' it will automatically leads to bound 'animal' label to each Pet model below such declaration (see example ).
Call methods with params
It is pretty simple. You just can pass any amount of params in any method in :methods definition. You should not create any presenters for model methods if you want to call them with params. Want to use instance method as param? No problem! With a bit of Tequila magic you can simple do so:
...
:methods
.calculate(1,2,3, total_count)
Code blocks
Code block are extremely useful if you need to add evaluable expression in the generated json but you don't want to store it as model (or presenter) method. For example you have attribute label, but for some reason you have to return "[# {label}]" just in one place of the code. The following feature allows you to keep your models clean:
...
:code => plabel
'[' + label + ']'
end
As result, in the generated data structure you will have additional field 'plabel' with desired value.
Gluing
It is another nice feature which you always want to have (may be instinctively ;). Sometimes you have 'belongs_to' association where you need just one attribute. And 'gluing' allows you to.. glue attributes from child associations. Compare:
...
:tag
:only
.label
+tagger
:only
.name
end
# Out: {'tag' => {'label' => 'Happy Christmas!', 'tagger' => {'name' => 'Mr Lawrence"}}
Generated json fragment looks like too comprehensive. Let us rewrite this fragment using gluing feature:
...
:tag
:only
.label
<tagger
:only
.name => tagger_name
end
# Out: {'tag' => {'label' => 'Happy Christmas!', 'tagger_name' => 'Mr Lawrence"}}
Of course it can be handled via additional model methods, but latter is mere artifical solution. We are going to DRY, aren't we?
Issues
Strict order of definitions required! All blocks are optional.
- :only or :except
- :methods
- :code blocks
- +asscociations
- <gluening
Development
Benchmarks
At least for these tests it looks like to_json is ~2.4x faster..
But for some reason you use Ruby instead of C, right? Despite of the fact that Tequila is not too fast today we are happy to have such instrument and are going to develop it further. And we have good plans about it...
Plans
- Grammatic review
- implement HashMapper fucntionality
- Arbitrary order of defenitions
- More tests for edge cases
- More syntax sugar
- Speedup