-
Notifications
You must be signed in to change notification settings - Fork 1
/
base.cr
225 lines (188 loc) · 6.5 KB
/
base.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
require "./trait"
module Factory
class Base
IS_FACTORY = ["false"]
macro after_finished_hook
end
def self.build
end
def self.get_trait(name : String, go_deep)
end
def self.get_trait(name : Symbol)
get_trait(name.to_s)
end
def self.after_initialize(obj)
end
def self.assign_attr(obj, name, value)
raise "Attribute #{name} can't be found."
end
def self.attributes
{} of String => String
end
Factory.default_methods
Factory.build_methods
macro not_a_factory
{% IS_FACTORY << "false" %}
end
macro after_initialize(&block)
def self.after_initialize({{block.args[0].id}})
super
{{block.body}}
end
end
macro initialize_with(&block)
def self.initialize_with({{block.args[0].id}}, {{block.args[1].id}})
{{block.body}}
end
end
macro describe_class(klass)
{% CLASS_NAME.push(klass.stringify) %}
end
macro argument_type(type)
{% ARGUMENT_TYPE.push(type.stringify) %}
end
# traits could be defined only for some class
macro trait(name)
{% trait_name = "#{name.id.stringify.camelcase.id}Trait" %}
{% TRAITS[name.id.stringify] = trait_name %}
macro _macro_trait_{{name.id}}
class {{trait_name.id}} < ::Factory::Trait(\{{CLASS_NAME[-1].id}})
{{yield.stringify.id}}
end
end
{% TRAIT_CLASS_DECLARATIONS << "_macro_trait_#{name.id}" %}
end
macro inherited
IS_FACTORY = ["true"]
TRAITS = {} of String => String
SEQUENCES = {} of String => String
CLASS_NAME = [] of String
ARGUMENT_TYPE = [] of String
ATTRIBUTES = {} of String => String
ASSIGNS = {} of String => String
IGNORED_METHODS = [] of String
TRAIT_CLASS_DECLARATIONS = [] of String
\{% if @type.superclass.constant("IS_FACTORY")[-1] == "true" %}
\{% for k, v in @type.superclass.constant("ASSIGNS") %}
\{% ASSIGNS[k] = v %}
\{% end %}
\{% for k, v in @type.superclass.constant("ATTRIBUTES") %}
\{% ATTRIBUTES[k] = v %}
\{% end %}
\{% end %}
macro def self.described_class
\{{CLASS_NAME.last.id}}
end
macro skip_all_constructors
skip_hash_constructor
skip_empty_constructor
end
macro skip_hash_constructor
\{% IGNORED_METHODS << "hash_constructor" %}
end
macro skip_empty_constructor
\{% IGNORED_METHODS << "empty_constructor" %}
end
macro finished
\{% for trait in TRAIT_CLASS_DECLARATIONS %}
\{{trait.id}}
\{% end %}
def self.get_trait(name : String, go_deep : Bool = true)
\{% for k, v in TRAITS %}
return \{{v.id}} if \{{k}} == name
\{% end %}
super if go_deep
end
\{% if ARGUMENT_TYPE.size == 0 && @type.superclass.constant("IS_FACTORY")[-1] == "true" && @type.superclass.constant("ARGUMENT_TYPE").size != 0 %}
\{% ARGUMENT_TYPE.push(@type.superclass.constant("ARGUMENT_TYPE").last) %}
\{% end %}
\{% if CLASS_NAME.empty? %}
\{% if @type.superclass.constant("IS_FACTORY")[-1] == "true" %}
\{% CLASS_NAME.push(@type.superclass.constant("CLASS_NAME").last) %}
\{% else %}
\{% CLASS_NAME.push(@type.stringify.gsub(/Factory$/, "").id) %}
\{% end %}
\{% end %}
\{% if IS_FACTORY[-1] == "true" %}
\{% factory_name = @type.stringify.gsub(/Factory$/, "").underscore.gsub(/::/, "_") %}
factory_builders(\{{factory_name.id}})
\{% Factory::FACTORIES[factory_name] = @type.stringify %}
\{% if ATTRIBUTES.empty? %}
\{% if !IGNORED_METHODS.includes?("empty_constructor") %}
def self.initialize_with(hash, traits)
obj = described_class.new
make_assigns(obj, traits)
obj
end
\{% end %}
\{% else %}
\{% if !IGNORED_METHODS.includes?("hash_constructor") %}
def self.initialize_with(hash, traits)
obj = described_class.new(hash)
make_assigns(obj, traits)
obj
end
\{% end %}
\{% end %}
def self.build(traits = [] of String | Symbol, **attrs)
obj = initialize_with(build_attributes(attrs, traits), traits)
after_initialize(obj)
obj
end
def self.build(attrs : Hash | NamedTuple)
obj = initialize_with(build_attributes(attrs), [] of String)
after_initialize(obj)
obj
end
def self.build(traits : Array, attrs : Hash | NamedTuple)
obj = initialize_with(build_attributes(attrs, traits), traits)
after_initialize(obj)
obj
end
def self.attributes
\{% if !ATTRIBUTES.empty? %}
\{% if ARGUMENT_TYPE.empty? %}
{
\{% for k, v in ATTRIBUTES %}
\{{k.id.stringify}} => \{% if v =~ /->/ %} \{{v.id}}.call \{% else %} @@\{{k.id}} \{% end %},
\{% end %}
}
\{% else %}
hash = {} of String => \{{ARGUMENT_TYPE.last.id}}
\{% for k, v in ATTRIBUTES %}
hash[\{{k.id.stringify}}] = \{% if v =~ /->/ %} \{{v.id}}.call \{% else %} @@\{{k.id}} \{% end %}
\{% end %}
hash
\{% end %}
\{% else %}
{} of String => String
\{% end %}
end
def self.build_attributes(opts, traits = [] of String | Symbol)
attrs = attributes
traits.each do |name|
trait = get_trait(name.to_s)
raise "Unknown trait \"#{name.to_s}\"" if trait.nil?
trait.not_nil!.add_attributes(attrs)
end
opts.each do |k, v|
attrs[k.to_s] = v
end
attrs
end
def self.make_assigns(obj, traits)
\{% for k, v in ASSIGNS %}
obj.\{{k.id}} = \{% if v =~ /->/ %} \{{v.id}}.call \{% else %} @@assign_\{{k.id}} \{% end %}
\{% end %}
traits.each do |name|
trait = get_trait(name.to_s)
raise "Unknown trait" if trait.nil?
trait.not_nil!.make_assignes(obj)
end
end
\{% end %}
after_finished_hook
end
end
end
end