Adds ResponseSchemaGenerator Class

This commit is contained in:
Muhammad Nawzad 2023-11-12 11:01:51 +03:00
parent 15fb52eef1
commit 2d7dd77f6e
No known key found for this signature in database
GPG Key ID: B954B6AAE33940B2
2 changed files with 93 additions and 0 deletions

View File

@ -0,0 +1,81 @@
module Schemable
class ResponseSchemaGenerator
attr_accessor :model_definition, :model
def initialize(model_definition)
@model_definition = model_definition
@model = model_definition.model
@schema_modifier = SchemaModifier.new
end
def generate(expand: false, relationships_to_exclude_from_expansion: [], collection: false)
data = {
type: :object,
properties: {
type: { type: :string, default: @model_definition.model_name },
id: { type: :string },
attributes: AttributeSchemaGenerator.new(@model_definition).generate
}.merge(
if @model_definition.relationships.blank? || @model_definition.relationships == { belongs_to: {}, has_many: {} }
{}
else
{ relationships: RelationshipSchemaGenerator.new(@model_definition).generate(expand:, relationships_to_exclude_from_expansion:) }
end
)
}
schema = collection ? { data: { type: :array, items: data } } : { data: }
if expand
included_schema = IncludedSchemaGenerator.new(@model_definition).generate(expand:, relationships_to_exclude_from_expansion:)
@schema_modifier.add_properties(schema, included_schema, '.')
end
@schema_modifier.add_properties(schema, meta, '.') if collection
@schema_modifier.add_properties(schema, jsonapi, '.')
{ type: :object, properties: schema }
end
def meta
{
type: :object,
properties: {
page: {
type: :object,
properties: {
totalPages: {
type: :integer,
default: 1
},
count: {
type: :integer,
default: 1
},
rowsPerPage: {
type: :integer,
default: 1
},
currentPage: {
type: :integer,
default: 1
}
}
}
}
}
end
def jsonapi
{
type: :object,
properties: {
version: {
type: :string,
default: '1.0'
}
}
}
end
end
end

View File

@ -0,0 +1,12 @@
module Schemable
class ResponseSchemaGenerator
attr_accessor model: Class
attr_accessor model_definition: Definition
attr_accessor schema_modifier: SchemaModifier
def initialize: (Definition) -> void
def meta: -> Hash[Symbol, any]
def jsonapi: -> Hash[Symbol, any]
def generate: (expand: bool, relationships_to_exclude_from_expansion: Array[Symbol], collection: bool) -> Hash[Symbol, any]
end
end