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

Returns a new instance of 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)

Returns content of the serenade view, before compilation.

Returns:

  • (String)

    content of the serenade view, before compilation



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

def content
  @content
end

#nameString (readonly)

Returns name of the serenade view.

Returns:

  • (String)

    name of the serenade view



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

def name
  @name
end

Instance Method Details

#parseHash

Parse the Serenade view in #content.

Returns:

  • (Hash)

    Serenade view AST.



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.

Returns:

  • (String)

    JavaScript output: ‘Serenade.view(’view_name’, view_ast)‘



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

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