aboutsummaryrefslogtreecommitdiff
path: root/src/guff/template-database.cr
blob: 7ff9690917473974ed152c66b3d4216dfe53269b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
module Guff
  class TemplateDatabase
    def initialize(@db : Database, @templates : TemplateCache)
    end

    def one(
      key       : Symbol, 
      args      : Array(String) | Hash(String, String) | Nil,
      tmpl_args : Hash(String, String)?
    )
      @db.one(template(key, tmpl_args), args)
    end

    def row(
      key       : Symbol, 
      args      : Array(String) | Hash(String, String) | Nil,
      tmpl_args : Hash(String, String)?,
    )
      @db.row(template(key, tmpl_args), args)
    end

    def all(
      key       : Symbol, 
      args      : Array(String) | Hash(String, String) | Nil,
      tmpl_args : Hash(String, String)?,
      &block    : Proc(Hash(String, ::SQLite3::Value), Nil) \
    )
      @db.all(template(key, tmpl_args), args, &block)
    end

    def query(
      key       : Symbol, 
      args      : Array(String) | Hash(String, String) | Nil,
      tmpl_args : Hash(String, String)?
    )
      @db.query(template(key, tmpl_args), args)
    end

    def template(
      key   : Symbol,
      args  : Hash(String, String)?
    )
      @templates[key].run(args)
    end
  end
end