Class: Musterb::Musterbifier

Inherits:
Object
  • Object
show all
Defined in:
lib/musterb/musterbifier.rb

Direct Known Subclasses

TemplateHandler

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ Musterbifier

Returns a new instance of Musterbifier.



2
3
4
# File 'lib/musterb/musterbifier.rb', line 2

def initialize(template)
  @template = template
end

Instance Method Details

#block_endObject



25
26
27
# File 'lib/musterb/musterbifier.rb', line 25

def block_end
  "<% end %>"
end

#block_if(tokens) ⇒ Object



17
18
19
# File 'lib/musterb/musterbifier.rb', line 17

def block_if(tokens)
  "<% musterb.block_if #{tokens} do %>"
end

#block_unless(tokens) ⇒ Object



21
22
23
# File 'lib/musterb/musterbifier.rb', line 21

def block_unless(tokens)
  "<% musterb.block_unless #{tokens} do %>"
end

#change_tokenObject

Raises:

  • (NotImplementedError)


41
42
43
# File 'lib/musterb/musterbifier.rb', line 41

def change_token
  raise NotImplementedError, 'Not able to change the mustache delimiter just yet'
end

#commentObject



37
38
39
# File 'lib/musterb/musterbifier.rb', line 37

def comment
  ""
end

#fetch(match) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/musterb/musterbifier.rb', line 6

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

#render_partial(partial) ⇒ Object

Raises:

  • (NotImplementedError)


45
46
47
# File 'lib/musterb/musterbifier.rb', line 45

def render_partial(partial)
  raise NotImplementedError, "Override render_partial in Musterbifier to render partials"    
end

#text_with_escaping(tokens) ⇒ Object



33
34
35
# File 'lib/musterb/musterbifier.rb', line 33

def text_with_escaping(tokens)
  "<%== #{tokens} %>"
end

#text_without_escaping(tokens) ⇒ Object



29
30
31
# File 'lib/musterb/musterbifier.rb', line 29

def text_without_escaping(tokens)
  "<%= #{tokens} %>"
end

#to_erbObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/musterb/musterbifier.rb', line 49

def to_erb
  @template.gsub(/\{\{(\{?[^\}]*\}?)\}\}/) do |match|
    match = $1
    case match[0]
    when '#'
      block_if fetch match[1..-1]
    when '^'
      block_unless fetch match[1..-1]        
    when "/"
      block_end
    when '{'
      text_without_escaping fetch match[1..-2]        
    when '&'
      text_without_escaping fetch match[1..-1]
    when '!'
      comment
    when '='
      change_token
    when '>'
      render_partial match[1..-1].strip        
    else
      text_with_escaping fetch match        
    end
  end
end