Sha256: 4197133a739ab8484ef1b9ed431c6349264d46f907203aaaa4566c08a91ac86e

Contents?: true

Size: 1.96 KB

Versions: 4

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

require "erb"
require "set"

module Lita
  # A simple wrapper around ERB to render text from files or strings.
  # @since 4.2.0
  class Template
    # A clean room object to use as the binding for ERB rendering.
    # @api private
    class TemplateEvaluationContext
      # Returns the evaluation context's binding.
      # @return [Binding] The binding.
      def __get_binding
        binding
      end
    end

    class << self
      # Initializes a new Template with the contents of the file at the given path.
      # @param path [String] The path to the file to use as the template content.
      # @return Template
      def from_file(path)
        new(File.read(path).chomp)
      end
    end

    # @param source [String] A string to use as the template's content.
    def initialize(source)
      @erb = ERB.new(source, trim_mode: "<>")
      self.helpers = Set.new
    end

    # Add a module of helpers methods to be added to the template evalutation context.
    # @param helper [Module] The module to extend onto the template evalutation context.
    # @return [void]
    # @since 4.5.0
    def add_helper(helper)
      helpers << helper
    end

    # Render the template with the provided variables.
    # @param variables [Hash] A collection of variables for interpolation. Each key-value pair will
    #   make the value available inside the template as an instance variable with the key as its
    #   name.
    def render(variables = {})
      erb.result(context_binding(variables))
    end

    private

    attr_accessor :helpers

    # Create an empty object to use as the ERB context and set any provided variables in it.
    def context_binding(variables)
      context = TemplateEvaluationContext.new

      helpers.each { |helper| context.extend(helper) }

      variables.each do |k, v|
        context.instance_variable_set("@#{k}", v)
      end

      context.__get_binding
    end

    # The underlying ERB object.
    attr_reader :erb
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rita-5.0.0.alpha.4 lib/lita/template.rb
rita-5.0.0.alpha.3 lib/lita/template.rb
rita-5.0.0.alpha.2 lib/lita/template.rb
rita-5.0.0.alpha.1 lib/lita/template.rb