aboutsummaryrefslogtreecommitdiff
path: root/src/guff/models.cr
blob: 24cc9f824a549ee63adb3f9982efb8c8278e7fbf (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
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,
    })

    def default_site_id
      # TODO
      0
    end
  end
end