Module | Ezamar::Morpher |
In: |
lib/ezamar/morpher.rb
|
MORPHS | = | { 'if' => '<?r %morph %expression ?>%content<?r end ?>', 'unless' => '<?r %morph %expression ?>%content<?r end ?>', 'for' => '<?r %morph %expression ?>%content<?r end ?>', 'each' => '<?r %expression.%morph do |_e| ?>%content<?r end ?>', 'times' => '<?r %expression.%morph do |_t| ?>%content<?r end ?>', } | Use this trait to define your custom morphs. |
Since the functionality is best explained by examples, here they come.
Example:
if:
<div if="@name">#@name</div>
morphs to:
<?r if @name ?> <div>#@name</div> <?r end ?>
unless:
<div unless="@name">No Name</div>
morphs to:
<?r unless @name ?> <div>No Name</div> <?r end ?>
for:
<div for="name in @names">#{name}</div>
morphs to:
<?r for name in @names ?> <div>#{name}</div> <?r end ?>
times:
<div times="3">#{_t}<div>
morphs to:
<?r 3.times do |_t| ?> <div>#{_t}</div> <?r end ?>
each:
<div each="[1,2,3]">#{_e}</div>
morphs to:
<?r [1,2,3].each do |_e| ?> <div>#{_e}</div> <?r end ?>
The latter two examples show you also one standard introduced by a limitation of the replacement-system.
When you yield a value, please name it by the first character(s) of the morphs name, with an underscore prefixed.
for each an _e, for times a _t.
This is by far not the best way to handle it and might lead to problems due to the lack of proper scoping in ruby (if you define an _e or _t before the block it will be overwritten).
So please be careful, I tried to come up with something that is both easy to write and doesn‘t look outright awful while keeping an easy to remember mnemonic.
TODO:
- Add pure Ruby implementation as a fall-back.