Sha256: 92026d7241316165950e337f31bd0996f3ed9f39e101fc17ff028da6f84d76ec

Contents?: true

Size: 1.19 KB

Versions: 21

Compression:

Stored size: 1.19 KB

Contents

class Template
  DEFAULT_TIMEOUT = 10

  def initialize(input, io: StringIO.new, timeout: DEFAULT_TIMEOUT, ruby: {})
    @input = input
    @parsed =
      Timeout.timeout(timeout) { ::Template::Parser.parse(@input).to_raw }
    @io = io
    @timeout = timeout || DEFAULT_TIMEOUT
    @ruby = ::Code::Ruby.to_code(ruby || {})
  end

  def self.render(
    input,
    context = "",
    io: StringIO.new,
    timeout: DEFAULT_TIMEOUT,
    ruby: {}
  )
    new(input, io: io, timeout: timeout, ruby: ruby).render(context)
  end

  def render(context = "")
    Timeout.timeout(timeout) do
      if context != ""
        context = ::Code.evaluate(context, timeout: timeout, io: io, ruby: ruby)
      else
        context = ::Code::Object::Dictionnary.new
      end

      if !context.is_a?(::Code::Object::Dictionnary)
        raise ::Code::Template::IncompatibleContext.new(
                "context must be a dictionnary"
              )
      end

      context = context.merge(ruby)

      ::Template::Node::Template.new(parsed).evaluate(context: context, io: io)

      io.is_a?(StringIO) ? io.string : nil
    end
  end

  private

  attr_reader :parsed, :io, :timeout, :ruby
end

require_relative "template/version"

Version data entries

21 entries across 21 versions & 3 rubygems

Version Path
template-ruby-0.5.6 lib/template.rb
language-ruby-0.5.6 lib/template.rb
code-ruby-0.5.6 lib/template.rb
template-ruby-0.5.5 lib/template.rb
language-ruby-0.5.5 lib/template.rb
code-ruby-0.5.5 lib/template.rb
template-ruby-0.5.4 lib/template.rb
language-ruby-0.5.4 lib/template.rb
code-ruby-0.5.4 lib/template.rb
template-ruby-0.5.3 lib/template.rb
language-ruby-0.5.3 lib/template.rb
code-ruby-0.5.3 lib/template.rb
template-ruby-0.5.2 lib/template.rb
language-ruby-0.5.2 lib/template.rb
code-ruby-0.5.2 lib/template.rb
language-ruby-0.5.1 lib/template.rb
template-ruby-0.5.1 lib/template.rb
code-ruby-0.5.1 lib/template.rb
template-ruby-0.5.0 lib/template.rb
language-ruby-0.5.0 lib/template.rb