/
table.cr
53 lines (47 loc) · 1.43 KB
/
table.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
# Adds a :nodoc: to granite methods/constants if `DISABLE_GRANTE_DOCS` ENV var is true
macro disable_granite_docs?(stmt)
{% unless flag?(:granite_docs) %}
# :nodoc:
{{stmt.id}}
{% else %}
{{stmt.id}}
{% end %}
end
module Granite::Tables
module ClassMethods
def primary_name
{% begin %}
{% primary_key = @type.instance_vars.find { |ivar| (ann = ivar.annotation(Granite::Column)) && ann[:primary] } %}
{% if pk = primary_key %}
{{pk.name.stringify}}
{% end %}
{% end %}
end
def primary_type
{% begin %}
{% primary_key = @type.instance_vars.find { |ivar| (ann = ivar.annotation(Granite::Column)) && ann[:primary] } %}
{% if pk = primary_key %}
{{pk.type}}
{% end %}
{% end %}
end
def quoted_table_name : String
self.adapter.quote(table_name)
end
def quote(column_name) : String
self.adapter.quote(column_name)
end
# Returns the name of the table for `self`
# defaults to the model's name underscored + 's'.
def table_name : String
{% begin %}
{% table_ann = @type.annotation(Granite::Table) %}
{{table_ann && !table_ann[:name].nil? ? table_ann[:name] : @type.name.underscore.stringify.split("::").last}}
{% end %}
end
end
macro table(name)
@[Granite::Table(name: {{(name.is_a?(StringLiteral) ? name : name.id.stringify) || nil}})]
class ::{{@type.name.id}}; end
end
end