-
Notifications
You must be signed in to change notification settings - Fork 1
/
factory.cr
107 lines (91 loc) · 3.21 KB
/
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
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
module Factory
FACTORIES = {} of String => String
macro default_methods
macro attr(name, value, klass = nil)
\{% if !value.is_a?(ProcLiteral) %}
\{% if klass != nil %}
@@\{{name.id}} : \{{klass}} = \{{value}}
\{% else %}
@@\{{name.id}} = \{{value}}
\{% end %}
\{% ATTRIBUTES[name.id.stringify] = "plain" %}
\{% else %}
\{% ATTRIBUTES[name.id.stringify] = value.id.stringify %}
\{% end %}
end
macro assign(name, value, klass = nil)
\{% if !value.is_a?(ProcLiteral) %}
\{% if klass != nil %}
@@assign_\{{name.id}} : \{{klass}} = \{{value}}
\{% else %}
@@assign_\{{name.id}} = \{{value}}
\{% end %}
\{% ASSIGNS[name.id.stringify] = "plain" %}
\{% else %}
\{% ASSIGNS[name.id.stringify] = value.id.stringify %}
\{% end %}
end
macro sequence(name, init = 0, &block)
\{% sclass = (name.id.stringify.camelcase + "Sequence").id %}
\{% ATTRIBUTES[name.id.stringify] = "-> { ->(#{block.args[0]} : Int32) { #{block.body.id} }.call(#{sclass.id}.next) }" %}
class \{{sclass}} < Factory::Sequence
@@start = \{{init}}
end
end
end
macro build_methods
macro factory_builders(factory_name)
module ::Factory
def self.build_\{{factory_name}}
\{{@type}}.build
end
def self.build_\{{factory_name}}(**attrs)
\{{@type}}.build(**attrs)
end
def self.build_\{{factory_name}}(attrs : Hash | NamedTuple)
\{{@type}}.build(attrs)
end
def self.build_\{{factory_name}}(traits : Array)
\{{@type}}.build(traits)
end
def self.build_\{{factory_name}}(traits : Array, **attrs)
\{{@type}}.build(traits, **attrs)
end
def self.build_\{{factory_name}}(traits : Array, attrs : Hash | NamedTuple)
\{{@type}}.build(traits, attrs)
end
def self.build_\{{factory_name}}(count : Int32)
arr = [] of \{{CLASS_NAME.last.id}}
count.times { arr << \{{@type}}.build }
arr
end
def self.build_\{{factory_name}}(count : Int32, **attrs)
arr = [] of \{{CLASS_NAME.last.id}}
count.times { arr << \{{@type}}.build(**attrs) }
arr
end
def self.build_\{{factory_name}}(count : Int32, attrs : Hash | NamedTuple)
arr = [] of \{{CLASS_NAME.last.id}}
count.times { arr << \{{@type}}.build(attrs) }
arr
end
def self.build_\{{factory_name}}(count : Int32, traits : Array)
arr = [] of \{{CLASS_NAME.last.id}}
count.times { arr << \{{@type}}.build(traits) }
arr
end
def self.build_\{{factory_name}}(count : Int32, traits : Array, **attrs)
arr = [] of \{{CLASS_NAME.last.id}}
count.times { arr << \{{@type}}.build(traits, **attrs) }
arr
end
def self.build_\{{factory_name}}(count : Int32, traits : Array, attrs : Hash | NamedTuple)
arr = [] of \{{CLASS_NAME.last.id}}
count.times { arr << \{{@type}}.build(traits, attrs) }
arr
end
end
end
end
end
require "./factory/*"