openstax/openstax_api — pull request #6 by jpslav (2014-09-29 19:01:33+00:00)
Title: can handle oneOf now in JSON schemas

Roar/representable let you dynamically specify the decorator for a property (depending on the specific type of the property being representer).

Representable v2 uses `Uber::Callable` object to do this (v1 used lambdas). Here's a `property` definition using these callables for the `:class` and ':decorator` fields:

property :details,
class: Api::V1::DetailedTaskRepresenter::SubModelFinder.new,
decorator: Api::V1::DetailedTaskRepresenter::SubRepresenterFinder.new,
parse_strategy: :sync,
schema_info: {
required: true
}

This PR changes this gem to enable the JSON schema generator to know how to handle these dynamic types of decorators (it generates a `"oneOf"` array in the schema where each entry points off to another decorator definition. (see http://json-schema.org/example2.html)

The callable decorator has one entry point, a `def call(*args)` method. Normally this method takes a model Class and returns the appropriate representer. This PR also requires that the `call` method be on the lookout for when `args = [:all_sub_representers]`. A call with this argument has to return an array of all possible representers that this callable can return (this lets us generate the `"oneOf"` list appropriately.

For reference, here's the `SubRepresenterFinder` referenced above:

class SubRepresenterFinder
include Uber::Callable

def call(*args)
if args[0] == :all_sub_representers
[Api::V1::ReadingRepresenter]
else
case args[0].class
when Reading
Api::V1::ReadingRepresenter
end
end
end
end