Skip to content

Commit 98a6556

Browse files
committed
Add documentation for the macros
1 parent c763025 commit 98a6556

File tree

4 files changed

+123
-3
lines changed

4 files changed

+123
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ d = html(body = b)
4141
```
4242
```html
4343
julia> pprint(d)
44-
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
44+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
4545
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
4646
<html>
4747
<body>

docs/src/customConstructors.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@
22

33
## Adding Custom code to the structures
44

5+
AcuteML introduces three `@creator`, `@extractor`, and `@updater` macros to be used inside `@aml` definition. The location of the macros specifies their location in the function. For example, putting `@creator` at the begining, adds the code to begining of creator function.
6+
7+
8+
- Put `@creator` inside `@aml` to add a custom code to the creator function (DOM creation when the struct is instanciated).
9+
10+
This macro only affects creation (not extraction/updating), but can be used in combination with other macros.
11+
12+
- Put `@extractor` inside `@aml` to add a custom code to the extractor function (DOM parsing when a html/xml text is used for instanciation of a struct).
13+
14+
This macro only affects creation (not creation/updating), but can be used in combination with other macros.
15+
16+
Be careful that setting struct fields using `@extractor` only changes the struct field and not the xml code.
17+
18+
- Put `@updater` inside `@aml` to add a custom code to the updater function (DOM updating after instanciation of a struct).
19+
20+
This macro only affects updating (not creation/extraction), but can be used in combination with other macros.
21+
22+
In the following example `IQ` and `average` are calculated automatically. Also, in the following example `log` is filled automatically (which doesn't have an associated xml element).
23+
524
```julia
625
using AcuteML
726
# Definition

src/@aml/@aml_parse/custom_code.jl

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,113 @@
1-
export creator, extractor, updater
1+
export @creator, @extractor, @updater
2+
3+
"""
4+
@creator
5+
6+
Put `@creator` inside `@aml` to add a custom code to the creator function (DOM creation when the struct is instanciated).
7+
8+
The location of the macro specifies its location in the function. For example, putting it at the begining, adds the code to begining of creator function.
9+
10+
This macro only affects creation (not extraction/updating), but can be used in combination with other macros.
11+
12+
In the following example `IQ` and `average` are calculated automatically.
13+
14+
# Example
15+
```julia
16+
@aml mutable struct Student "student"
17+
18+
# add custom code to the begining of creator function
19+
# the following automatically fills IQ based on the name
20+
@creator begin
21+
if occursin("smart", name)
22+
name = replace(name, "-smart" => "") # remove smart from name
23+
IQ = "smart"
24+
elseif occursin("genius", name)
25+
name = replace(name, "-genius" => "") # remove smart from name
26+
IQ = "genius"
27+
else
28+
error("Give a smart student.")
29+
end
30+
end
31+
32+
name::String, "~"
33+
GPA::Float64, "~"
34+
IQ::UN{String} = nothing, att"~" # default to nothing, but filled automatically by first @cretor macro
35+
end
36+
37+
@aml mutable struct MathClass "math-class"
38+
@creator begin
39+
GPAsum = 0.0
40+
for student in students
41+
GPAsum = GPAsum + student.GPA
42+
end
43+
average = GPAsum / length(students) # fill average field
44+
end
45+
46+
students::Vector{Student}, "student"
47+
average::UN{Float64} = nothing, "average" # calculated automatically
48+
end
49+
50+
smarts = [Student(name = "Jack-smart", GPA = 2.0), Student(name = "Sara-genius", GPA = 5.0)]
51+
mathclass = MathClass(students = smarts)
52+
```
53+
"""
254
macro creator(exp)
355
return :creator, exp
456
end
557

58+
"""
59+
@extractor
60+
61+
Put `@extractor` inside `@aml` to add a custom code to the extractor function (DOM parsing when a html/xml text is used for instanciation of a struct).
62+
63+
The location of the macro specifies its location in the function. For example, putting it at the begining, adds the code to begining of extractor function.
64+
65+
This macro only affects creation (not creation/updating), but can be used in combination with other macros.
66+
67+
Be careful that setting struct fields using `@extractor` only changes the struct field and not the xml code.
68+
69+
In the following example `log` is filled automatically (which doesn't have an associated xml element).
70+
71+
# Example
72+
```julia
73+
@aml mutable struct Student "student"
74+
75+
name::String, "~"
76+
GPA::Float64, "~"
77+
78+
log::UN{String} = nothing, "~"
79+
80+
# add custom code to the end of extractor function
81+
@extractor begin
82+
if GPA > 4.0
83+
log = "A genius with a GPA of $GPA is found" # setting fields using @extractor only changes the field and not the xml code
84+
end
85+
end
86+
end
87+
88+
@aml mutable struct MathClass "math-class"
89+
students::Vector{Student}, "student"
90+
end
91+
92+
smarts = [Student(name = "Jack-smart", GPA = 2.0), Student(name = "Sara-genius", GPA = 5.0)]
93+
mathclass = MathClass(students = smarts)
94+
```
95+
"""
696
macro extractor(exp)
797
return :extractor, exp
898
end
999

100+
"""
101+
@updater
102+
103+
Put `@updater` inside `@aml` to add a custom code to the updater function (DOM updating after instanciation of a struct).
104+
105+
The location of the macro specifies its location in the function. For example, putting it at the begining, adds the code to begining of updater function.
106+
107+
This macro only affects updating (not creation/extraction), but can be used in combination with other macros.
108+
109+
See [`@creator`](@ref) and [`@extractor`](@ref) for some examples.
110+
"""
10111
macro updater(exp)
11112
return :updater, exp
12113
end

test/customcode.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
name::String, "~"
1919
GPA::Float64, "~"
2020
IQ::UN{String} = nothing, att"~" # default to nothing, but filled automatically by first @cretor macro
21-
# add custom code to the end of extractor function
2221

2322
log::UN{String} = nothing, "~"
2423

24+
# add custom code to the end of extractor function
2525
@extractor begin
2626
if GPA > 4.0
2727
log = "A genius with a GPA of $GPA is found" # setting fields using @extractor only changes the field and not the xml code

0 commit comments

Comments
 (0)