Adds documentations for ResponseSchemaGenerator

This commit is contained in:
Muhammad Nawzad 2023-11-17 17:21:08 +03:00
parent e5229586af
commit 561a90953b
No known key found for this signature in database
GPG Key ID: B954B6AAE33940B2

View File

@ -1,7 +1,17 @@
module Schemable module Schemable
# The ResponseSchemaGenerator class is responsible for generating JSON schemas for responses.
# This class generates schemas based on the model definition, including attributes, relationships, and included resources.
#
# @see Schemable
class ResponseSchemaGenerator class ResponseSchemaGenerator
attr_reader :model_definition, :model, :schema_modifier, :configuration attr_reader :model_definition, :model, :schema_modifier, :configuration
# Initializes a new ResponseSchemaGenerator instance.
#
# @param model_definition [ModelDefinition] The model definition to generate the schema for.
#
# @example
# generator = ResponseSchemaGenerator.new(model_definition)
def initialize(model_definition) def initialize(model_definition)
@model_definition = model_definition @model_definition = model_definition
@model = model_definition.model @model = model_definition.model
@ -9,6 +19,19 @@ module Schemable
@configuration = Schemable.configuration @configuration = Schemable.configuration
end end
# Generates the JSON schema for a response.
# It generates a schema for the model attributes and relationships, and if the 'expand' option is true,
# it also includes the included resources in the schema.
# It also adds meta and jsonapi information to the schema.
#
# @param expand [Boolean] Whether to include the included resources in the schema.
# @param relationships_to_exclude_from_expansion [Array] The relationships to exclude from expansion in the schema.
# @param collection [Boolean] Whether the response is for a collection of resources.
#
# @example
# schema = generator.generate(expand: true, relationships_to_exclude_from_expansion: ['some_relationship'], collection: true)
#
# @return [Hash] The generated schema.
def generate(expand: false, relationships_to_exclude_from_expansion: [], collection: false) def generate(expand: false, relationships_to_exclude_from_expansion: [], collection: false)
data = { data = {
type: :object, type: :object,
@ -38,6 +61,13 @@ module Schemable
{ type: :object, properties: schema }.compact_blank { type: :object, properties: schema }.compact_blank
end end
# Generates the JSON schema for the 'meta' part of a response.
# It returns a custom meta response schema if one is defined in the configuration, otherwise it generates a default meta schema.
#
# @example
# meta_schema = generator.meta
#
# @return [Hash] The generated schema for the 'meta' part of a response.
def meta def meta
return @configuration.custom_meta_response_schema if @configuration.custom_meta_response_schema.present? return @configuration.custom_meta_response_schema if @configuration.custom_meta_response_schema.present?
@ -73,6 +103,12 @@ module Schemable
end end
end end
# Generates the JSON schema for the 'jsonapi' part of a response.
#
# @example
# jsonapi_schema = generator.jsonapi
#
# @return [Hash] The generated schema for the 'jsonapi' part of a response.
def jsonapi def jsonapi
{ {
type: :object, type: :object,