| Path: | lib/galena.rb |
| Last Update: | Wed Sep 13 00:18:53 CEST 2006 |
Copyright © 2006 Brad Phelan
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Galena a bright shiny obsidian black oxide of lead which I fell in love with whilst working at Pasminco Mines as a student many years back. Within the expanse of black Galena crysta there are to be found many tiny ruby crystals.
More precisely, in this context, Galena is text with bit of Ruby embedded along similar lines to ERB. However it is different to ERB in that it uses metaprogramming techniques to create templated text methods on any class you are creating. Ruby methods are dynamically added to the class. Eval and binding are never used. In theorey this should make these templates much faster than ERB, though benchmarks need to be done.
A simple example is
class Foo
tdef :bar, "food" <<-END
eat %=food=%
END
end
foo = Foo.new
puts foo.bar "curry"
eat curry
See galena_test.rb for a full example
! /usr/bin/ruby
require 'galena'
require 'enumerator'
include Galena
class MyText
def initialize
@drink = "koolaid"
@food = "dogfood"
end
# Creates a method called page which
# accepts an array and prints it in
# an html table with four columns.
tdef :page, "data", <<-END
<html>
<head>
</head>
<body>
^= tableize(data, 4) do |item|
foo
^= window("Wow") do
This is my item %=item=%
boo
^= end
^= end
</body>
</html>
END
# Generates an html table with a varying
# number of columns
tdef :tableize, "data, columns", <<-END
<table>
^< data.each_slice(columns) do |slice|
<tr>
^< slice.each do |s|
<td>
%=yield s=%
</td>
^> end
</tr>
^> end
</table>
END
# Generates an html div with a title and some
# contents defined by the yielded block
tdef :window, "title", <<-END
<div>
<div style="width:100; color:blue;%">
%=title=%
</div>
%=yield=%
</div>
END
end
h = MyText.new
puts h.page(1..10)