/
remove_item.jl
41 lines (35 loc) · 1.2 KB
/
remove_item.jl
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
"""
@ _remove_fn objname ModelType IndexType [plural] [inplace] begin ... end
A helper for creating functions that follow the `remove_objname` template, such
as [`remove_metabolites`](@ref) and [`remove_reaction`](@ref).
"""
macro _remove_fn(objname, model_type, idx_type, args...)
body = last(args)
typeof(body) == Expr || throw(DomainError(body, "missing function body"))
plural = :plural in args
plural_s = plural ? "s" : ""
inplace = :inplace in args
fname = Symbol(:remove_, objname, plural_s, inplace ? "!" : "")
idx_var = Symbol(
objname,
idx_type == :Int ? "_idx" :
idx_type == :String ? "_id" :
throw(DomainError(idx_type, "unsupported index type for _remove_fn macro")),
plural_s,
)
if plural
idx_type = AbstractVector{eval(idx_type)}
end
docstring = """
$fname(model::$model_type, $idx_var::$idx_type)
Remove $objname$plural_s from the model of type `$model_type`
$(inplace ? "in-place" : "and return the modified model").
"""
Expr(
:macrocall,
Symbol("@doc"),
__source__,
docstring,
:($fname(model::$model_type, $idx_var::$idx_type) = $body),
)
end