/
polymorphic_factory.cr
36 lines (32 loc) · 1.02 KB
/
polymorphic_factory.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
require "./base"
module Clear::Model::Factory
class PolymorphicFactory(T)
include Base
property type_field : String = ""
property self_class : String = ""
def initialize(@type_field, @self_class)
end
def build(h : Hash(String, ::Clear::SQL::Any),
cache : Clear::Model::QueryCache? = nil,
persisted : Bool = false,
fetch_columns : Bool = false) : Clear::Model
v = h[@type_field]
case v
when String
if v == T.name
{% if T.abstract? %}
raise "Cannot instantiate #{@type_field} because it is abstract class"
{% else %}
T.new(v, h, cache, persisted, fetch_columns).as(Clear::Model)
{% end %}
else
Clear::Model::Factory.build(v, h, cache, persisted, fetch_columns).as(Clear::Model)
end
when Nil
raise Clear::ErrorMessages.polymorphic_nil(@type_field)
else
raise Clear::ErrorMessages.polymorphic_nil(@type_field)
end
end
end
end