Sha256: 55d9d664c8c91fc4802b9a3b4aa523e4f75b2820da648bbe2272ec77ec442715

Contents?: true

Size: 967 Bytes

Versions: 9

Compression:

Stored size: 967 Bytes

Contents

# frozen_string_literal: true

class Template
  GLOBALS = %i[output error context object].freeze
  DEFAULT_TIMEOUT = 0

  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

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

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

  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

  private

  attr_reader :input, :timeout, :output, :error, :context
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
template-ruby-1.1.2 lib/template.rb
template-ruby-1.1.0 lib/template.rb
template-ruby-1.0.0 lib/template.rb
template-ruby-0.7.3 lib/template.rb
template-ruby-0.7.2 lib/template.rb
template-ruby-0.7.0 lib/template.rb
template-ruby-0.6.9 lib/template.rb
template-ruby-0.6.8 lib/template.rb
template-ruby-0.6.7 lib/template.rb