mirror of
https://github.com/ditkrg/schemable.git
synced 2026-01-22 22:26:41 +00:00
Passes parameters to the function on invoke rather than on class initialization
This commit is contained in:
parent
861a740f61
commit
15fb52eef1
@ -1,14 +1,11 @@
|
|||||||
module Schemable
|
module Schemable
|
||||||
class IncludedSchemaGenerator
|
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)
|
def initialize(model_definition)
|
||||||
@expand = expand
|
|
||||||
@model_definition = model_definition
|
@model_definition = model_definition
|
||||||
@schema_modifier = SchemaModifier.new
|
@schema_modifier = SchemaModifier.new
|
||||||
@configuration = Schemable.configuration
|
|
||||||
@relationships = @model_definition.relationships
|
@relationships = @model_definition.relationships
|
||||||
@relationships_to_exclude_from_expansion = relationships_to_exclude_from_expansion
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def generate(expand: false, relationships_to_exclude_from_expansion: [])
|
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: [])
|
def prepare_schema_for_included(model_definition, expand: false, relationships_to_exclude_from_expansion: [])
|
||||||
attributes_schema = AttributeSchemaGenerator.new(model_definition).generate
|
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,
|
type: :object,
|
||||||
|
|||||||
@ -1,17 +1,14 @@
|
|||||||
module Schemable
|
module Schemable
|
||||||
class RelationshipSchemaGenerator
|
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)
|
def initialize(model_definition)
|
||||||
@expand = expand
|
|
||||||
@model_definition = model_definition
|
@model_definition = model_definition
|
||||||
@schema_modifier = SchemaModifier.new
|
@schema_modifier = SchemaModifier.new
|
||||||
@configuration = Schemable.configuration
|
|
||||||
@relationships = model_definition.relationships
|
@relationships = model_definition.relationships
|
||||||
@relationships_to_exclude_from_expansion = relationships_to_exclude_from_expansion
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def generate
|
def generate(relationships_to_exclude_from_expansion: [], expand: false)
|
||||||
return {} if @relationships.blank? || @relationships == { belongs_to: {}, has_many: {} }
|
return {} if @relationships.blank? || @relationships == { belongs_to: {}, has_many: {} }
|
||||||
|
|
||||||
schema = {
|
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 = 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)
|
schema[:properties].merge!(relation => result)
|
||||||
end
|
end
|
||||||
|
|||||||
@ -7,7 +7,7 @@ module Schemable
|
|||||||
attr_accessor relationships: Hash[Symbol, any]
|
attr_accessor relationships: Hash[Symbol, any]
|
||||||
attr_accessor relationships_to_exclude_from_expansion: Array[String]
|
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]
|
def prepare_schema_for_included: (Definition, ?relationships_to_exclude_from_expansion: Array[String], ?expand: bool) -> Hash[Symbol, any]
|
||||||
|
|
||||||
|
|||||||
@ -1,15 +1,12 @@
|
|||||||
module Schemable
|
module Schemable
|
||||||
class RelationshipSchemaGenerator
|
class RelationshipSchemaGenerator
|
||||||
attr_accessor expand: bool
|
|
||||||
attr_accessor model_definition: Definition
|
attr_accessor model_definition: Definition
|
||||||
attr_accessor configuration: Configuration
|
|
||||||
attr_accessor schema_modifier: SchemaModifier
|
attr_accessor schema_modifier: SchemaModifier
|
||||||
attr_accessor relationships: Hash[Symbol, any]
|
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]
|
def generate_schema: (String, ?collection: bool) -> Hash[Symbol, any]
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user