Class: Serenade::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/serenade/renderer.rb

Overview

Serenade::Renderer compiles Serenade views into a Serenade AST.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, content) ⇒ Renderer


19
20
21
22
# File 'lib/serenade/renderer.rb', line 19

def initialize(name, content)
  @name = name
  @content = content
end

Instance Attribute Details

#contentString (readonly)


17
18
19
# File 'lib/serenade/renderer.rb', line 17

def content
  @content
end

#nameString (readonly)


14
15
16
# File 'lib/serenade/renderer.rb', line 14

def name
  @name
end

Instance Method Details

#parseHash

Parse the Serenade view in #content.


27
28
29
30
31
# File 'lib/serenade/renderer.rb', line 27

def parse
  context = ExecJS.compile(File.read(SERENADEJS_PATH))
  code = "Serenade.view(#{MultiJson.dump(content)}).parse()"
  context.eval(code)
end

#renderString

Parse the Serenade view in #content and return a JavaScript string that will register the view with the designated #name in Serenade.

This is the compiled output of a Serenade view template in Sprockets.


39
40
41
# File 'lib/serenade/renderer.rb', line 39

def render
  "Serenade.view(#{MultiJson.dump(name)}, #{MultiJson.dump(parse)});"
end