Class: Dato::JsonSchemaRelationships

Inherits:
Object
  • Object
show all
Defined in:
lib/dato/json_schema_relationships.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ JsonSchemaRelationships

Returns a new instance of JsonSchemaRelationships.



7
8
9
# File 'lib/dato/json_schema_relationships.rb', line 7

def initialize(schema)
  @schema = schema
end

Instance Attribute Details

#schemaObject (readonly)

Returns the value of attribute schema.



5
6
7
# File 'lib/dato/json_schema_relationships.rb', line 5

def schema
  @schema
end

Instance Method Details

#relationshipsObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dato/json_schema_relationships.rb', line 11

def relationships
  return {} if !schema || !schema.properties["data"]

  entity = if schema.properties["data"].type.first == "array"
             schema.properties["data"].items
           else
             schema.properties["data"]
           end

  return {} if !entity || !entity.properties["relationships"] || !entity.properties["relationships"]

  relationships = entity.properties["relationships"].properties

  relationships.each_with_object({}) do |(relationship, schema), acc|
    is_collection = schema.properties["data"].type.first == "array"

    types = if is_collection
              [type(schema.properties["data"].items)]
            elsif schema.properties["data"].type.first == "object"
              [type(schema.properties["data"])]
            else
              schema.properties["data"].any_of.map do |option|
                type(option)
              end.compact
            end

    acc[relationship.to_sym] = {
      collection: is_collection,
      types: types,
    }
  end
end

#type(definition) ⇒ Object



44
45
46
# File 'lib/dato/json_schema_relationships.rb', line 44

def type(definition)
  definition.properties["type"].pattern.source.gsub(/(^\^|\$$)/, "") if definition.properties["type"]
end