Sha256: e118d46a2856fb2f6c59ad6e1a26d3cd8ac6219c1bbca82ad5209930792dbc19

Contents?: true

Size: 1.18 KB

Versions: 3

Compression:

Stored size: 1.18 KB

Contents

require 'mustache'
module HSQL
  # Given some SQL that may contain Mustache tags (e.g. {{{ variable }}} ),
  # accept a hash of data that interpolates each tag.
  # Throws an error if one of the tag names can't be found in the data.
  class Template
    attr_reader :input

    # This is used to indicate when a source file is malformed.
    class FormatError < StandardError
    end

    def initialize(input, verbose)
      @input = input
      @verbose = verbose
    end

    def render(hash)
      Mustache.raise_on_context_miss = true
      output = Mustache.render(input, hash)
      if @verbose
        warn '-- Rendered SQL:'
        warn output
      end
      output
    rescue Mustache::ContextMiss => e
      fail_with(e.message, hash.keys.sort)
    end

    private

    def fail_with(message, keys)
      # Pull the missing template tag out of the message
      tag = message.scan(/Can't find (\w+) in /).flatten.first
      message = "Missing variable {{{ #{tag} }}}. At this point in the template the available variables are:"
      message += "\n"
      message += keys.join(', ')
      fail FormatError, message
    end

    def ast
      ::Mustache::Parser.new.compile(input)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hsql-0.4.0 lib/hsql/template.rb
hsql-0.3.8 lib/hsql/template.rb
hsql-0.3.7 lib/hsql/template.rb