Class: Dato::JsonSchemaRelationships
- Inherits:
-
Object
- Object
- Dato::JsonSchemaRelationships
- Defined in:
- lib/dato/json_schema_relationships.rb
Instance Attribute Summary collapse
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Instance Method Summary collapse
-
#initialize(schema) ⇒ JsonSchemaRelationships
constructor
A new instance of JsonSchemaRelationships.
- #relationships ⇒ Object
- #type(definition) ⇒ Object
Constructor Details
#initialize(schema) ⇒ JsonSchemaRelationships
Returns a new instance of JsonSchemaRelationships.
5 6 7 |
# File 'lib/dato/json_schema_relationships.rb', line 5 def initialize(schema) @schema = schema end |
Instance Attribute Details
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
3 4 5 |
# File 'lib/dato/json_schema_relationships.rb', line 3 def schema @schema end |
Instance Method Details
#relationships ⇒ Object
9 10 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 43 44 |
# File 'lib/dato/json_schema_relationships.rb', line 9 def relationships if !schema || !schema.properties['data'] return {} end entity = if schema.properties['data'].type.first == 'array' schema.properties['data'].items else schema.properties['data'] end if !entity || !entity.properties['relationships'] || !entity.properties['relationships'] return {} end 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
46 47 48 49 50 |
# File 'lib/dato/json_schema_relationships.rb', line 46 def type(definition) if definition.properties['type'] definition.properties['type'].pattern.source.gsub(/(^\^|\$$)/, '') end end |