/
config.cr
99 lines (82 loc) · 3.36 KB
/
config.cr
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
module Marten
module Apps
# Base application config class.
#
# This class can be subclassed on a per-application basis in order to configure the considered application. Things
# like the application label can be configured this way. Moreover, app config classes are also used in order to
# retrieve various abstractions associated with applications (such as models for example).
abstract class Config
include Association
@@label = "app"
getter models
delegate label, to: self.class
def self.label(label : String | Symbol)
unless LABEL_RE.match(label.to_s)
raise Errors::InvalidAppConfig.new("A label can only contain lowercase letters and underscores")
end
if label.to_s == MainConfig::RESERVED_LABEL
raise Errors::InvalidAppConfig.new("App cannot use the 'main' reserved label")
end
@@label = label.to_s
end
# Returns the label of the application config.
def self.label : String
@@label
end
def initialize
@models = [] of DB::Model.class
end
# Returns true if the other app config corresponds to the current app config.
def ==(other : self)
super || (label == other.label && models == other.models)
end
# Returns the assets finder of the application.
#
# If the application doesn't have a dedicated assets directory, `nil` is returned.
def assets_finder
assets_dir = Path[self.class._marten_app_location].join(ASSETS_DIR)
return unless Dir.exists?(assets_dir)
Asset::Finder::FileSystem.new(assets_dir.to_s)
end
# Returns the migrations path for the application.
def migrations_path
Path[self.class._marten_app_location].join(MIGRATIONS_DIR)
end
# Associates a model to the current app config.
def register_model(model : DB::Model.class)
@models << model
end
# Allows to perform additional setup operations for a given application.
#
# Each application class can override this method in order to perform additional initializations and setup
# operations if needed. This method will be called during the Marten setup process when all the applications
# are known and initialized.
def setup
end
# Returns the templates loader of the application.
#
# If the application doesn't have a dedicated templates directory, `nil` is returned.
def templates_loader
templates_dir = Path[self.class._marten_app_location].join(TEMPLATES_DIR)
return unless Dir.exists?(templates_dir)
Template::Loader::FileSystem.new(templates_dir.to_s)
end
# Returns the translations loader of the application.
#
# If the application doesn't define translations in a dedicated directory, `nil` is returned.
def translations_loader
locales_dir = Path[self.class._marten_app_location].join(LOCALES_DIR)
return unless Dir.exists?(locales_dir)
I18n::Loader::YAML.new(locales_dir.to_s)
end
macro inherited
Marten::Apps::Registry.register_app_config({{ @type }})
end
private ASSETS_DIR = "assets"
private LABEL_RE = /^[a-z_]+$/
private LOCALES_DIR = "locales"
private MIGRATIONS_DIR = "migrations"
private TEMPLATES_DIR = "templates"
end
end
end