aboutsummaryrefslogtreecommitdiff
path: root/src/guff/models.cr
blob: 631be8d4779f359880bb72de860398cad6886118 (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
require "./database"
require "./database-updater"

private macro define_model_getters(hash)
  {% for name, klass in hash %}
    def {{ name.id }}
      @cache[{{ name }}] ||= {{ klass.id }}.new(self)
    end
  {% end %}
end

module Guff
  class Models
    getter :config
    getter :db

    def initialize(@config : Config)
      # build model cache
      @cache = {} of Symbol => Model

      # path to db
      path = "%s/site.db" % [config["data"]]

      # update database (if necessary)
      DatabaseUpdater.run(path, @config)

      # open db
      @db = Database.new(path)
    end

    define_model_getters({
      post: PostModel,
      tag:  TagModel,
    })
  end
end