Passes parameters to the function on invoke rather than on class initialization

This commit is contained in:
Muhammad Nawzad 2023-11-12 11:00:40 +03:00
parent 861a740f61
commit 15fb52eef1
No known key found for this signature in database
GPG Key ID: B954B6AAE33940B2
4 changed files with 10 additions and 19 deletions

View File

@ -1,14 +1,11 @@
module Schemable
class IncludedSchemaGenerator
attr_accessor :model_definition, :schema_modifier, :configuration, :relationships, :expand, :relationships_to_exclude_from_expansion
attr_accessor :model_definition, :schema_modifier, :relationships
def initialize(model_definition, relationships_to_exclude_from_expansion: [], expand: false)
@expand = expand
def initialize(model_definition)
@model_definition = model_definition
@schema_modifier = SchemaModifier.new
@configuration = Schemable.configuration
@relationships = @model_definition.relationships
@relationships_to_exclude_from_expansion = relationships_to_exclude_from_expansion
end
def generate(expand: false, relationships_to_exclude_from_expansion: [])
@ -56,7 +53,7 @@ module Schemable
def prepare_schema_for_included(model_definition, expand: false, relationships_to_exclude_from_expansion: [])
attributes_schema = AttributeSchemaGenerator.new(model_definition).generate
relationships_schema = RelationshipSchemaGenerator.new(model_definition, relationships_to_exclude_from_expansion:, expand:)
relationships_schema = RelationshipSchemaGenerator.new(model_definition).generate(relationships_to_exclude_from_expansion:, expand:)
{
type: :object,

View File

@ -1,17 +1,14 @@
module Schemable
class RelationshipSchemaGenerator
attr_accessor :model_definition, :schema_modifier, :configuration, :relationships, :expand, :relationships_to_exclude_from_expansion
attr_accessor :model_definition, :schema_modifier, :relationships
def initialize(model_definition, relationships_to_exclude_from_expansion: [], expand: false)
@expand = expand
def initialize(model_definition)
@model_definition = model_definition
@schema_modifier = SchemaModifier.new
@configuration = Schemable.configuration
@relationships = model_definition.relationships
@relationships_to_exclude_from_expansion = relationships_to_exclude_from_expansion
end
def generate
def generate(relationships_to_exclude_from_expansion: [], expand: false)
return {} if @relationships.blank? || @relationships == { belongs_to: {}, has_many: {} }
schema = {
@ -35,7 +32,7 @@ module Schemable
result = relation_type == :belongs_to ? generate_schema(definition.model_name) : generate_schema(definition.model_name, collection: true)
result = non_expanded_data_properties if !expand || @relationships_to_exclude_from_expansion.include?(definition.model_name)
result = non_expanded_data_properties if !expand || relationships_to_exclude_from_expansion.include?(definition.model_name)
schema[:properties].merge!(relation => result)
end

View File

@ -7,7 +7,7 @@ module Schemable
attr_accessor relationships: Hash[Symbol, any]
attr_accessor relationships_to_exclude_from_expansion: Array[String]
def initialize: (Definition, ?relationships_to_exclude_from_expansion: Array[String], ?expand: bool) -> void
def initialize: (Definition) -> void
def prepare_schema_for_included: (Definition, ?relationships_to_exclude_from_expansion: Array[String], ?expand: bool) -> Hash[Symbol, any]

View File

@ -1,15 +1,12 @@
module Schemable
class RelationshipSchemaGenerator
attr_accessor expand: bool
attr_accessor model_definition: Definition
attr_accessor configuration: Configuration
attr_accessor schema_modifier: SchemaModifier
attr_accessor relationships: Hash[Symbol, any]
attr_accessor relationships_to_exclude_from_expansion: Array[String]
def initialize: (Definition, ?relationships_to_exclude_from_expansion: Array[String], ?expand: bool) -> void
def initialize: (Definition) -> void
def generate: -> (Hash[Symbol, any] | Array[Hash[Symbol, any]])
def generate: (?relationships_to_exclude_from_expansion: Array[String], ?expand: bool) -> (Hash[Symbol, any] | Array[Hash[Symbol, any]])
def generate_schema: (String, ?collection: bool) -> Hash[Symbol, any]
end