Updates Definition Class

This commit is contained in:
Muhammad Nawzad 2023-11-09 16:55:49 +03:00
parent dd7598ba56
commit 01d459bf5c
No known key found for this signature in database
GPG Key ID: B954B6AAE33940B2
2 changed files with 251 additions and 204 deletions

View File

@ -6,29 +6,12 @@ module Schemable
@configuration = configuration @configuration = configuration
end end
# Returns the resource serializer to be used for serialization. This method must be implemented in the definition class.
#
# @abstract This method must be implemented in the definition class.
#
# @raise [NotImplementedError] If the method is not implemented in the definition class.
#
# @return [Class] The resource serializer class.
#
# @example
# V1::UserSerializer
#
def serializer def serializer
raise NotImplementedError, 'You must implement the serializer method in the definition class in order to use the infer_serializer_from_jsonapi_serializable configuration option.' if configuration.infer_attributes_from_jsonapi_serializable raise NotImplementedError, 'You must implement the serializer method in the definition class in order to use the infer_serializer_from_jsonapi_serializable configuration option.' if configuration.infer_attributes_from_jsonapi_serializable
nil nil
end end
# Returns the attributes defined in the serializer (Auto generated from the serializer).
#
# @return [Array<Symbol>, nil] The attributes defined in the serializer or nil if there are none.
#
# @example
# [:id, :name, :email, :created_at, :updated_at]
def attributes def attributes
return (serializer&.attribute_blocks&.transform_keys { |key| key.to_s.underscore.to_sym }&.keys || nil) if configuration.infer_attributes_from_jsonapi_serializable return (serializer&.attribute_blocks&.transform_keys { |key| key.to_s.underscore.to_sym }&.keys || nil) if configuration.infer_attributes_from_jsonapi_serializable
@ -37,260 +20,78 @@ module Schemable
model.attribute_names model.attribute_names
end end
# Returns the relationships defined in the serializer.
#
# @return [Hash] The relationships defined in the serializer.
#
# @note Note that the format of the relationships is as follows:
# { belongs_to: { relationship_name: relationship_definition }, has_many: { relationship_name: relationship_definition }
#
# @example
# {
# belongs_to: {
# district: Swagger::Definitions::District,
# user: Swagger::Definitions::User
# },
# has_many: {
# applicants: Swagger::Definitions::Applicant,
# }
# }
def relationships def relationships
{ belongs_to: {}, has_many: {} } { belongs_to: {}, has_many: {} }
end end
# Returns a hash of all the arrays defined for the model. The schema for each array is defined in the definition class manually.
# This method must be implemented in the definition class if there are any arrays.
#
# @return [Hash] The arrays of the model and their schemas.
#
# @example
# {
# metadata: {
# type: :array,
# items: {
# type: :object, nullable: true,
# properties: { name: { type: :string, nullable: true } }
# }
# }
# }
def array_types def array_types
{} {}
end end
# Returns the attributes that are optional in the create request body. This means that they are not required to be present in the create request body thus they are taken out of the required array.
#
# @return [Array<Symbol>] The attributes that are optional in the create request body.
#
# @example
# [:name, :email]
def optional_create_request_attributes def optional_create_request_attributes
%i[] %i[]
end end
# Returns the attributes that are optional in the update request body. This means that they are not required to be present in the update request body thus they are taken out of the required array.
#
# @return [Array<Symbol>] The attributes that are optional in the update request body.
#
# @example
# [:name, :email]
def optional_update_request_attributes def optional_update_request_attributes
%i[] %i[]
end end
# Returns the attributes that are nullable in the request/response body. This means that they can be present in the request/response body but they can be null.
# They are not required to be present in the request body.
#
# @return [Array<Symbol>] The attributes that are nullable in the request/response body.
#
# @example
# [:name, :email]
def nullable_attributes def nullable_attributes
%i[] %i[]
end end
# Returns the additional create request attributes that are not automatically generated. These attributes are appended to the create request schema.
#
# @return [Hash] The additional create request attributes that are not automatically generated (if any).
#
# @example
# {
# name: { type: :string }
# }
def additional_create_request_attributes def additional_create_request_attributes
{} {}
end end
# Returns the additional update request attributes that are not automatically generated. These attributes are appended to the update request schema.
#
# @return [Hash] The additional update request attributes that are not automatically generated (if any).
#
# @example
# {
# name: { type: :string }
# }
def additional_update_request_attributes def additional_update_request_attributes
{} {}
end end
# Returns the additional response attributes that are not automatically generated. These attributes are appended to the response schema.
#
# @return [Hash] The additional response attributes that are not automatically generated (if any).
#
# @example
# {
# name: { type: :string }
# }
def additional_response_attributes def additional_response_attributes
{} {}
end end
# Returns the additional response relations that are not automatically generated. These relations are appended to the response schema's relationships.
#
# @return [Hash] The additional response relations that are not automatically generated (if any).
#
# @example
# {
# users: {
# type: :object,
# properties: {
# data: {
# type: :array,
# items: {
# type: :object,
# properties: {
# id: { type: :string },
# type: { type: :string }
# }
# }
# }
# }
# }
# }
def additional_response_relations def additional_response_relations
{} {}
end end
# Returns the additional response included that are not automatically generated. These included are appended to the response schema's included.
#
# @return [Hash] The additional response included that are not automatically generated (if any).
#
# @example
# {
# type: :object,
# properties: {
# id: { type: :string },
# type: { type: :string },
# attributes: {
# type: :object,
# properties: {
# name: { type: :string }
# }
# }
# }
# }
def additional_response_included def additional_response_included
{} {}
end end
# Returns the attributes that are excluded from the create request schema.
# These attributes are not required or not needed to be present in the create request body.
#
# @return [Array<Symbol>] The attributes that are excluded from the create request schema.
#
# @example
# [:id, :updated_at, :created_at]
def excluded_create_request_attributes def excluded_create_request_attributes
%i[] %i[]
end end
# Returns the attributes that are excluded from the update request schema.
# These attributes are not required or not needed to be present in the update request body.
#
# @return [Array<Symbol>] The attributes that are excluded from the update request schema.
#
# @example
# [:id, :updated_at, :created_at]
def excluded_update_request_attributes def excluded_update_request_attributes
%i[] %i[]
end end
# Returns the attributes that are excluded from the response schema.
# These attributes are not needed to be present in the response body.
#
# @return [Array<Symbol>] The attributes that are excluded from the response schema.
#
# @example
# [:id, :updated_at, :created_at]
def excluded_response_attributes def excluded_response_attributes
%i[] %i[]
end end
# Returns the relationships that are excluded from the response schema.
# These relationships are not needed to be present in the response body.
#
# @return [Array<Symbol>] The relationships that are excluded from the response schema.
#
# @example
# [:users, :applicants]
def excluded_response_relations def excluded_response_relations
%i[] %i[]
end end
# Returns the included that are excluded from the response schema.
# These included are not needed to be present in the response body.
#
# @return [Array<Symbol>] The included that are excluded from the response schema.
#
# @example
# [:users, :applicants]
#
# @todo
# This method is not used anywhere yet.
def excluded_response_included def excluded_response_included
%i[] %i[]
end end
# Returns the relationships to be further expanded in the response schema.
#
# @return [Hash] The relationships to be further expanded in the response schema.
#
# @example
# {
# applicants: {
# belongs_to: {
# district: Swagger::Definitions::District,
# province: Swagger::Definitions::Province,
# },
# has_many: {
# attachments: Swagger::Definitions::Upload,
# }
# }
# }
def nested_relationships def nested_relationships
{} {}
end end
# Returns the model class (Constantized from the definition class name)
#
# @return [Class] The model class (Constantized from the definition class name)
#
# @example
# User
def model
self.class.name.gsub('Swagger::Definitions::', '').constantize
end
def serialized_instance def serialized_instance
{} {}
end end
# Returns the model name. Used for schema type naming. def model
# self.class.name.gsub('Swagger::Definitions::', '').constantize
# @return [String] The model name. end
#
# @example
# 'users' for the User model
# 'citizen_applications' for the CitizenApplication model
def self.model_name def self.model_name
name.gsub('Swagger::Definitions::', '').pluralize.underscore.downcase name.gsub('Swagger::Definitions::', '').pluralize.underscore.downcase
end end

View File

@ -1,5 +1,251 @@
module Schemable module Schemable
class Definition class Definition
def serializer: -> untyped attr_accessor configuration: Configuration
# Initializes the definition with the configuration.
def initialize: (Configuration) -> void
# Returns the attributes that are nullable in the request/response body. This means that they can be present in the request/response body but they can be null.
# They are not required to be present in the request body.
#
# @return [Array<Symbol>] The attributes that are nullable in the request/response body.
#
# @example
# [:name, :email]
def nullable_attributes: -> Array[Symbol]
# Returns the additional create request attributes that are not automatically generated. These attributes are appended to the create request schema.
#
# @return [Hash] The additional create request attributes that are not automatically generated (if any).
#
# @example
# {
# name: { type: :string }
# }
def additional_create_request_attributes: -> Hash[Symbol, any]
# Returns the additional update request attributes that are not automatically generated. These attributes are appended to the update request schema.
#
# @return [Hash] The additional update request attributes that are not automatically generated (if any).
#
# @example
# {
# name: { type: :string }
# }
def additional_update_request_attributes: -> Hash[Symbol, any]
# Returns the additional response attributes that are not automatically generated. These attributes are appended to the response schema.
#
# @return [Hash] The additional response attributes that are not automatically generated (if any).
#
# @example
# {
# name: { type: :string }
# }
def additional_response_attributes: -> Hash[Symbol, any]
# Returns the additional response relations that are not automatically generated. These relations are appended to the response schema's relationships.
#
# @return [Hash] The additional response relations that are not automatically generated (if any).
#
# @example
# {
# users: {
# type: :object,
# properties: {
# data: {
# type: :array,
# items: {
# type: :object,
# properties: {
# id: { type: :string },
# type: { type: :string }
# }
# }
# }
# }
# }
# }
def additional_response_relations: -> Hash[Symbol, any]
# Returns the additional response included that are not automatically generated. These included are appended to the response schema's included.
#
# @return [Hash] The additional response included that are not automatically generated (if any).
#
# @example
# {
# type: :object,
# properties: {
# id: { type: :string },
# type: { type: :string },
# attributes: {
# type: :object,
# properties: {
# name: { type: :string }
# }
# }
# }
# }
def additional_response_included: -> Hash[Symbol, any]
# Returns the attributes that are excluded from the create request schema.
# These attributes are not required or not needed to be present in the create request body.
#
# @return [Array<Symbol>] The attributes that are excluded from the create request schema.
#
# @example
# [:id, :updated_at, :created_at]
def excluded_create_request_attributes: -> Array[Symbol]
# Returns the attributes that are excluded from the response schema.
# These attributes are not needed to be present in the response body.
#
# @return [Array<Symbol>] The attributes that are excluded from the response schema.
#
# @example
# [:id, :updated_at, :created_at]
def excluded_response_attributes: -> Array[Symbol]
# Returns the attributes that are excluded from the update request schema.
# These attributes are not required or not needed to be present in the update request body.
#
# @return [Array<Symbol>] The attributes that are excluded from the update request schema.
#
# @example
# [:id, :updated_at, :created_at]
def excluded_update_request_attributes: -> Array[Symbol]
# Returns the relationships that are excluded from the response schema.
# These relationships are not needed to be present in the response body.
#
# @return [Array<Symbol>] The relationships that are excluded from the response schema.
#
# @example
# [:users, :applicants]
def excluded_response_relations: -> Array[Symbol]
# Returns the included that are excluded from the response schema.
# These included are not needed to be present in the response body.
#
# @return [Array<Symbol>] The included that are excluded from the response schema.
#
# @example
# [:users, :applicants]
#
# @todo
# This method is not used anywhere yet.
def excluded_response_included: -> Array[Symbol]
# Returns the relationships to be further expanded in the response schema.
#
# @return [Hash] The relationships to be further expanded in the response schema.
#
# @example
# {
# applicants: {
# belongs_to: {
# district: Swagger::Definitions::District,
# province: Swagger::Definitions::Province,
# },
# has_many: {
# attachments: Swagger::Definitions::Upload,
# }
# }
# }
def nested_relationships: -> Hash[Symbol, any]
# Returns the resource serializer to be used for serialization. This method must be implemented in the definition class.
#
# @abstract This method must be implemented in the definition class.
#
# @raise [NotImplementedError] If the method is not implemented in the definition class.
#
# @return [Class] The resource serializer class.
#
# @example
# V1::UserSerializer
def serializer: -> Class?
# Returns the attributes defined in the serializer (Auto generated from the serializer), or from a custom method, or from attributes_names method.
#
# @return [Array<Symbol>] The attributes to be generated.
#
# @example
# [:id, :name, :email, :created_at, :updated_at]
def attributes: -> Array[Symbol]
# Returns the relationships defined in the serializer.
#
# @return [Hash] The relationships defined in the serializer.
#
# @note Note that the format of the relationships is as follows:
# { belongs_to: { relationship_name: relationship_definition }, has_many: { relationship_name: relationship_definition }
#
# @example
# {
# belongs_to: {
# district: Swagger::Definitions::District,
# user: Swagger::Definitions::User
# },
# has_many: {
# applicants: Swagger::Definitions::Applicant,
# }
# }
def relationships: -> Hash[Symbol, any]
# Returns a hash of all the arrays defined for the model. The schema for each array is defined in the definition class manually.
# This method must be implemented in the definition class if there are any arrays.
#
# @return [Hash] The arrays of the model and their schemas.
#
# @example
# {
# metadata: {
# type: :array,
# items: {
# type: :object, nullable: true,
# properties: { name: { type: :string, nullable: true } }
# }
# }
# }
def array_types: -> Hash[Symbol, any]
# Returns the attributes that are optional in the create request body. This means that they are not required to be present in the create request body thus they are taken out of the required array.
#
# @return [Array<Symbol>] The attributes that are optional in the create request body.
#
# @example
# [:name, :email]
def optional_create_request_attributes: -> Array[Symbol]
# Returns the attributes that are optional in the update request body. This means that they are not required to be present in the update request body thus they are taken out of the required array.
#
# @return [Array<Symbol>] The attributes that are optional in the update request body.
#
# @example
# [:name, :email]
def optional_update_request_attributes: -> Array[Symbol]
# Returns an instance of the model class that is already serialized into jsonapi format.
#
# @return [Hash] The serialized instance of the model class.
def serialized_instance: -> Hash[Symbol, any]
# Returns the model class (Constantized from the definition class name)
#
# @return [Class] The model class (Constantized from the definition class name)
#
# @example
# User
def model: -> Class
# Returns the model name. Used for schema type naming.
#
# @return [String] The model name.
#
# @example
# 'users' for the User model
# 'citizen_applications' for the CitizenApplication model
def self.model_name: -> String
end end
end end