Sha256: e1ab3dc7331dff8148fe0137aa8cf19a7acb6354640a1ea4b1f1f0dd93f1480c

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

class Musterb::Musterbifier
  def initialize(template, render_partial_template = nil)
    @template = template
    @render_partial_template = render_partial_template || method(:partials_not_implemented)
  end

  def fetch(tokens)
    tokens = tokens.strip.split(".")
    last_token = tokens.pop
    fetch_command = tokens.inject("musterb") do |str, token|
      "#{str}.chain('#{token}')"
    end
    "#{fetch_command}['#{last_token}']"
  end

  def to_erb
    @template.gsub(/\{\{(\{?[^\}]*\}?)\}\}/) do |match|
      match = $1
      case match[0]
      when '#'
        "<% musterb.block_if #{fetch match[1..-1]} do %>"
      when '^'
        "<% musterb.block_unless #{fetch match[1..-1]} do %>"
      when "/"
        "<% end %>"
      when '{'
        "<%= #{fetch match[1..-2]} %>"
      when '&'
        "<%= #{fetch match[1..-1]} %>"
      when '!'
        ""
      when '.'
        "<%== musterb.current %>"
      when '='
        raise NotImplementedError, 'Not able to change the mustache delimiter just yet'
      when '>'
        "<%= #{@render_partial_template.call(match[1..-1].strip)} %>"
      else
        "<%== #{fetch match} %>"
      end
    end
  end

  def partials_not_implemented(partial)
    "raise NotImplementedError, 'Don't know how to render partial: #{partial}'"
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
musterb-0.0.7 lib/musterb/musterbifier.rb
musterb-0.0.6 lib/musterb/musterbifier.rb