Self embedding in resource definition

8 views
Skip to first unread message

andrea.s...@gmail.com

unread,
May 24, 2016, 12:20:08 PM5/24/16
to praxis-support
Hi, is there a way to model a resource which is a tree?

I.E:

module V1
module MediaTypes
class BusinessUnit < Praxis::MediaType
attributes do
attribute :id, Integer,
attribute :name, String,
attribute :substructures, Praxis::Collection.of(BusinessUnit),

...
end
end
end
end

This version generates an infinite recursion on doc preview, since each substructure includes more substructures itself...

Josep Blanquer

unread,
May 24, 2016, 1:29:52 PM5/24/16
to Salicetti Andrea, praxis-support
Well, exactly like that is how you do it. But you just have to make sure that:
1- The view attributes you render the substructures make it non-recursive (i.e. maybe use a view for BusinessUnit that does not include substructures). I think that's not what you want since you want to print a true data tree.
2- Make sure the generated examples for a BusinessUnit are not recursive (i.e., some will have no substructures)

Since you can use the "example" option for an attribute, I would suggest doing that.
You can make it simple or more sophisticated, depending on what you need to be shown in the doc browser. By that I am saying you could do something as simple as:

```
   attribute :substructures, Praxis::Collection.of(BusinessUnit),
       example: -> { ( rand(100) > 80 ) ? (1..rand(2)+1).map{BusinessUnit.example} : [] }
```
this is untested (just wrote it up without checking) but you could do something like that were 80% of the time you'd get an empty list for substructures, and the other 20% you'd get one or two ...)

Anyway, just an initial suggestion to make you think how you could prepare the example data for your docs.
This can obviously be taken way far by reading things from a "db" etc...there's only so much we can do with automatic example generation...these things are application related so the app needs to do some extra work.

Cheers,

Josep M.




--
You received this message because you are subscribed to the Google Groups "praxis-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to praxis-suppor...@googlegroups.com.
To post to this group, send email to praxis-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/praxis-support/0a53e3de-85ba-4735-87ee-171975476735%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages