From 561a90953bc9b9926c97350c0aa384743d313b00 Mon Sep 17 00:00:00 2001 From: Muhammad Nawzad Date: Fri, 17 Nov 2023 17:21:08 +0300 Subject: [PATCH] Adds documentations for ResponseSchemaGenerator --- lib/schemable/response_schema_generator.rb | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/lib/schemable/response_schema_generator.rb b/lib/schemable/response_schema_generator.rb index 5f6d38b..0f716ee 100644 --- a/lib/schemable/response_schema_generator.rb +++ b/lib/schemable/response_schema_generator.rb @@ -1,7 +1,17 @@ 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 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) @model_definition = model_definition @model = model_definition.model @@ -9,6 +19,19 @@ module Schemable @configuration = Schemable.configuration 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) data = { type: :object, @@ -38,6 +61,13 @@ module Schemable { type: :object, properties: schema }.compact_blank 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 return @configuration.custom_meta_response_schema if @configuration.custom_meta_response_schema.present? @@ -73,6 +103,12 @@ module Schemable 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 { type: :object,