|
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 | +""" |
2 | 54 | macro creator(exp)
|
3 | 55 | return :creator, exp
|
4 | 56 | end
|
5 | 57 |
|
| 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 | +""" |
6 | 96 | macro extractor(exp)
|
7 | 97 | return :extractor, exp
|
8 | 98 | end
|
9 | 99 |
|
| 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 | +""" |
10 | 111 | macro updater(exp)
|
11 | 112 | return :updater, exp
|
12 | 113 | end
|
0 commit comments