aboutsummaryrefslogtreecommitdiff
path: root/src/guff/models.cr
blob: 4b6a46f6b53ea7dde10f0b0ec2f72fbcc6dddbcd (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"
require "./models/*"

private macro define_model_getters(hash)
  {% for name, klass in hash %}
    def {{ name.id }} : {{ klass.id }}
      (@cache[{{ name }}] ||= {{ klass.id }}.new(self)) as {{ klass.id }}
    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,
      site:     SiteModel,
      session:  SessionModel,
      user:     UserModel,
      role:     RoleModel,
    })
  end
end