lib/musterb/musterbifier.rb in musterb-0.0.7 vs lib/musterb/musterbifier.rb in musterb-0.1.0

- old
+ new

@@ -1,47 +1,74 @@ class Musterb::Musterbifier - def initialize(template, render_partial_template = nil) + def initialize(template) @template = template - @render_partial_template = render_partial_template || method(:partials_not_implemented) end - def fetch(tokens) - tokens = tokens.strip.split(".") + def fetch(match) + match = match.strip + return "musterb.current" if match == '.' + tokens = match.split(".") last_token = tokens.pop fetch_command = tokens.inject("musterb") do |str, token| "#{str}.chain('#{token}')" end "#{fetch_command}['#{last_token}']" end + def block_if(tokens) + "<% musterb.block_if #{tokens} do %>" + end + + def block_unless(tokens) + "<% musterb.block_unless #{tokens} do %>" + end + + def block_end + "<% end %>" + end + + def text_without_escaping(tokens) + "<%= #{tokens} %>" + end + + def text_with_escaping(tokens) + "<%== #{tokens} %>" + end + + def comment + "" + end + + def change_token + raise NotImplementedError, 'Not able to change the mustache delimiter just yet' + end + + def render_partial(partial) + raise NotImplementedError, "Override render_partial in Musterbifier to render partials" + end + def to_erb @template.gsub(/\{\{(\{?[^\}]*\}?)\}\}/) do |match| match = $1 case match[0] when '#' - "<% musterb.block_if #{fetch match[1..-1]} do %>" + block_if fetch match[1..-1] when '^' - "<% musterb.block_unless #{fetch match[1..-1]} do %>" + block_unless fetch match[1..-1] when "/" - "<% end %>" + block_end when '{' - "<%= #{fetch match[1..-2]} %>" + text_without_escaping fetch match[1..-2] when '&' - "<%= #{fetch match[1..-1]} %>" + text_without_escaping fetch match[1..-1] when '!' - "" - when '.' - "<%== musterb.current %>" + comment when '=' - raise NotImplementedError, 'Not able to change the mustache delimiter just yet' + change_token when '>' - "<%= #{@render_partial_template.call(match[1..-1].strip)} %>" + render_partial match[1..-1].strip else - "<%== #{fetch match} %>" + text_with_escaping fetch match end end - end - - def partials_not_implemented(partial) - "raise NotImplementedError, 'Don't know how to render partial: #{partial}'" end end \ No newline at end of file