1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Examples for Model entities
19 # This module introduce flags in all mentities so they can be tagged as example.
20 # Flagged classes will be treated differently by the documentation tools.
22 # See `MEntity::is_example` and `MEntity::examples`.
27 # An example from a MEntity flagged with `is example`.
32 mentity
.mexample
= self
35 # MEntity containing this example
38 # MEntities this example is for
40 # For each entity we provide a weight so examples can be ranked.
41 var example_for
= new ArrayMap[MEntity, Int]
43 redef fun to_s
do return mentity
.full_name
48 # Is `self` existing for an example purpose?
50 # All mentities annotated with `is example` or located inside a mentity that
51 # is an example are considered as examples.
52 fun is_example
: Bool do return mexample
!= null
54 # Return this entity as a MExample
55 var mexample
: nullable MExample = null
57 # Examples found for `self`
58 var examples
= new Array[MExample]
64 for mgroup
in mgroups
do
65 for example
in mgroup
.examples
do
66 if not res
.has
(example
) then res
.add example
74 redef var is_example
is lazy
do
75 var parent
= self.parent
76 if parent
!= null and parent
.is_example
then return true
77 return name
== "examples"
82 for mmodule
in mmodules
do
83 for example
in mmodule
.examples
do
84 if not res
.has
(example
) then res
.add example
92 redef var is_example
is lazy
do return intro
.is_example
96 for mclassdef
in mclassdefs
do
97 for example
in mclassdef
.examples
do
98 if not res
.has
(example
) then res
.add example
105 redef class MProperty
106 redef var is_example
is lazy
do return intro
.is_example
108 redef fun examples
do
110 for mpropdef
in mpropdefs
do
111 for example
in mpropdef
.examples
do
112 if not res
.has
(example
) then res
.add example