nitweb: add /api/inheritance/:id service
authorAlexandre Terrasa <alexandre@moz-code.org>
Wed, 8 Jun 2016 15:44:43 +0000 (11:44 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Tue, 21 Jun 2016 14:23:57 +0000 (10:23 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/nitweb.nit
src/web/model_api.nit
src/web/web_base.nit

index a1a41a3..23d17a3 100644 (file)
@@ -102,6 +102,7 @@ class APIRouter
                use("/uml/:id", new APIEntityUML(model, mainmodule))
                use("/linearization/:id", new APIEntityLinearization(model, mainmodule))
                use("/defs/:id", new APIEntityDefs(model, mainmodule))
+               use("/inheritance/:id", new APIEntityInheritance(model, mainmodule))
        end
 end
 
index 0d4c205..a9f0c75 100644 (file)
@@ -120,6 +120,22 @@ class APIEntity
        end
 end
 
+# List ancestors, parents, child and descendants of MEntity
+#
+# Example: `GET /entity/core::Array/inheritance`
+class APIEntityInheritance
+       super APIHandler
+
+       redef fun get(req, res) do
+               var mentity = mentity_from_uri(req, res)
+               if mentity == null then
+                       res.error 404
+                       return
+               end
+               res.json mentity.hierarchy_poset(view)[mentity]
+       end
+end
+
 # Linearize super definitions of a MClassDef or a MPropDef if any.
 #
 # Example: `GET /entity/core::Array/linearization`
index c84c784..0bdf0ad 100644 (file)
@@ -241,3 +241,20 @@ end
 redef class MVirtualType
        redef var web_url = mproperty.web_url is lazy
 end
+
+redef class POSetElement[E]
+       super Jsonable
+
+       # Return JSON representation of `self`.
+       fun json: JsonObject do
+               assert self isa POSetElement[MEntity]
+               var obj = new JsonObject
+               obj["greaters"] = to_mentity_refs(greaters)
+               obj["direct_greaters"] = to_mentity_refs(direct_greaters)
+               obj["direct_smallers"] = to_mentity_refs(direct_smallers)
+               obj["smallers"] = to_mentity_refs(smallers)
+               return obj
+       end
+
+       redef fun to_json do return json.to_json
+end