Adds configurations to infer attributes from

This commit is contained in:
Muhammad Nawzad 2023-11-09 15:52:18 +03:00
parent 8e3cd6e033
commit dd7598ba56
No known key found for this signature in database
GPG Key ID: B954B6AAE33940B2
3 changed files with 18 additions and 5 deletions

View File

@ -8,7 +8,9 @@ module Schemable
:custom_type_mappers, :custom_type_mappers,
:disable_factory_bot, :disable_factory_bot,
:use_serialized_instance, :use_serialized_instance,
:custom_defined_enum_method :custom_defined_enum_method,
:infer_attributes_from_custom_method,
:infer_attributes_from_jsonapi_serializable
) )
def initialize def initialize
@ -18,8 +20,10 @@ module Schemable
@custom_type_mappers = {} @custom_type_mappers = {}
@decimal_as_string = false @decimal_as_string = false
@disable_factory_bot = true @disable_factory_bot = true
@custom_defined_enum_method = nil
@use_serialized_instance = false @use_serialized_instance = false
@custom_defined_enum_method = nil
@infer_attributes_from_custom_method = nil
@infer_attributes_from_jsonapi_serializable = false
end end
def type_mapper(type_name) def type_mapper(type_name)

View File

@ -18,7 +18,9 @@ module Schemable
# V1::UserSerializer # V1::UserSerializer
# #
def serializer def serializer
raise NotImplementedError, 'serializer method must be implemented in the definition class' 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
end end
# Returns the attributes defined in the serializer (Auto generated from the serializer). # Returns the attributes defined in the serializer (Auto generated from the serializer).
@ -28,7 +30,11 @@ module Schemable
# @example # @example
# [:id, :name, :email, :created_at, :updated_at] # [:id, :name, :email, :created_at, :updated_at]
def attributes def attributes
serializer.attribute_blocks.transform_keys { |key| key.to_s.underscore.to_sym }.keys || nil return (serializer&.attribute_blocks&.transform_keys { |key| key.to_s.underscore.to_sym }&.keys || nil) if configuration.infer_attributes_from_jsonapi_serializable
return model.send(configuration.infer_attributes_from_custom_method).map(&:to_sym) if configuration.infer_attributes_from_custom_method
model.attribute_names
end end
# Returns the relationships defined in the serializer. # Returns the relationships defined in the serializer.
@ -271,7 +277,7 @@ module Schemable
# @example # @example
# User # User
def model def model
self.class.name.gsub('Swagger::Definitions::', '').gsub(':Class', '').constantize self.class.name.gsub('Swagger::Definitions::', '').constantize
end end
def serialized_instance def serialized_instance

View File

@ -1,5 +1,6 @@
module Schemable module Schemable
class Configuration class Configuration
attr_accessor orm: Symbol attr_accessor orm: Symbol
attr_accessor timestamps: bool attr_accessor timestamps: bool
attr_accessor float_as_string: bool attr_accessor float_as_string: bool
@ -8,6 +9,8 @@ module Schemable
attr_accessor use_serialized_instance: bool attr_accessor use_serialized_instance: bool
attr_accessor custom_defined_enum_method: Symbol? attr_accessor custom_defined_enum_method: Symbol?
attr_accessor custom_type_mappers: Hash[Symbol, any] attr_accessor custom_type_mappers: Hash[Symbol, any]
attr_accessor infer_attributes_from_jsonapi_serializable: bool
attr_accessor infer_attributes_from_custom_method: Symbol?
def initialize: -> void def initialize: -> void