Class: Template

Inherits:
Object
  • Object
show all
Defined in:
lib/template.rb,
lib/template/node.rb,
lib/template/parser.rb,
lib/template/node/part.rb,
lib/template/node/template.rb,
lib/template/node/code_part.rb,
lib/template/node/text_part.rb,
lib/template/parser/template.rb

Defined Under Namespace

Classes: Node, Parser

Constant Summary collapse

GLOBALS =
%i[output error context object].freeze
DEFAULT_TIMEOUT =
0
Version =
Gem::Version.new(File.read(File.expand_path("../../VERSION", __dir__)))

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, output: StringIO.new, error: StringIO.new, timeout: DEFAULT_TIMEOUT) ⇒ Template

Returns a new instance of Template.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/template.rb', line 7

def initialize(
  input,
  output: StringIO.new,
  error: StringIO.new,
  timeout: DEFAULT_TIMEOUT
)
  @input = input
  @output = output
  @error = error
  @timeout = timeout || DEFAULT_TIMEOUT
  @context = ::Code::Object::Context.new
end

Class Method Details

.evaluate(input, output: StringIO.new, error: StringIO.new, timeout: DEFAULT_TIMEOUT) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/template.rb', line 24

def self.evaluate(
  input,
  output: StringIO.new,
  error: StringIO.new,
  timeout: DEFAULT_TIMEOUT
)
  new(input, output:, error:, timeout:).evaluate
end

.parse(input, timeout: DEFAULT_TIMEOUT) ⇒ Object



20
21
22
# File 'lib/template.rb', line 20

def self.parse(input, timeout: DEFAULT_TIMEOUT)
  Timeout.timeout(timeout) { Parser.parse(input).to_raw }
end

Instance Method Details

#evaluateObject



33
34
35
36
37
38
39
40
# File 'lib/template.rb', line 33

def evaluate
  Timeout.timeout(timeout) do
    parsed = Template.parse(input)
    Node::Template.new(parsed).evaluate(context:, output:, error:)

    output.is_a?(StringIO) ? output.string : ""
  end
end