Class: HSQL::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/hsql/template.rb

Overview

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.

Defined Under Namespace

Classes: FormatError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, verbose) ⇒ Template

Returns a new instance of Template.



13
14
15
16
# File 'lib/hsql/template.rb', line 13

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

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



7
8
9
# File 'lib/hsql/template.rb', line 7

def input
  @input
end

Instance Method Details

#render(hash) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/hsql/template.rb', line 18

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