Updates method names and generate_attributes_schema's logic

This commit is contained in:
Muhammad Nawzad 2023-11-09 10:53:58 +03:00
parent 213fecefd2
commit 15fae701b5
No known key found for this signature in database
GPG Key ID: B954B6AAE33940B2
2 changed files with 13 additions and 7 deletions

View File

@ -11,21 +11,27 @@ module Schemable
end
# Generate the JSON schema for attributes
def generate_attribute_schema
def generate_attributes_schema
schema = {
type: :object,
properties: {}
properties: attributes.index_with do |attr|
generate_attribute_schema(attr)
end
}
@model.attribute_names.each do |attribute|
schema[:properties][attribute.to_sym] = generate_property_schema(attribute)
# modify the schema to include additional response relations
schema = @schema_modifier.add_properties(schema, @model_definition.additional_response_attributes, 'properties')
# modify the schema to exclude response relations
@model_definition.excluded_response_attributes.each do |key|
schema = @schema_modifier.delete_properties(schema, "properties.#{key}")
end
schema
end
# Generate the JSON schema for a specific attribute
def generate_property_schema(attribute)
def generate_attribute_schema(attribute)
if @configuration.orm == :mongoid
# Get the column hash for the attribute
attribute_hash = @model.fields[attribute.to_s]

View File

@ -7,8 +7,8 @@ module Schemable
attr_accessor schema_modifier: SchemaModifier
def initialize: (Definition, Configuration) -> void
def generate_attribute_schema: -> Hash[Symbol, any]
def generate_attributes_schema: -> (Hash[Symbol, any] | Array[any])
def generate_property_schema: (Symbol) -> Hash[Symbol, any]
def generate_attribute_schema: (Symbol) -> Hash[Symbol, any]
end
end