Class: Braintrust::Vendor::Mustache::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/braintrust/vendor/mustache/generator.rb

Overview

The Generator is in charge of taking an array of Mustache tokens, usually assembled by the Parser, and generating an interpolatable Ruby string. This string is considered the “compiled” template because at that point we’re relying on Ruby to do the parsing and run our code.

For example, let’s take this template:

Hi {{thing}}!

If we run this through the Parser we’ll get these tokens:

[:multi,
  [:static, "Hi "],
  [:mustache, :etag, "thing"],
  [:static, "!\n"]]

Now let’s hand that to the Generator:

>> puts Braintrust::Vendor::Mustache::Generator.new.compile(tokens) “Hi #)!n”

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Generator

Options can be used to manipulate the resulting ruby code string behavior.



34
35
36
37
# File 'lib/braintrust/vendor/mustache/generator.rb', line 34

def initialize(options = {})
  @options = options
  @option_static_lambdas = options[:static_lambdas] == true
end

Instance Method Details

#compile(exp) ⇒ Object

Given an array of tokens, returns an interpolatable Ruby string.



40
41
42
# File 'lib/braintrust/vendor/mustache/generator.rb', line 40

def compile(exp)
  "\"#{compile!(exp)}\""
end