From 861a740f61084c7d8ad3164cad44ed298db72dd8 Mon Sep 17 00:00:00 2001 From: Muhammad Nawzad Date: Sun, 12 Nov 2023 10:32:28 +0300 Subject: [PATCH] Renames method --- lib/schemable.rb | 2 +- lib/schemable/attribute_schema_generator.rb | 2 +- lib/schemable/included_schema_generator.rb | 30 ++++++++++---------- sig/schemable/attribute_schema_generator.rbs | 2 +- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/schemable.rb b/lib/schemable.rb index 034d2a7..5792227 100644 --- a/lib/schemable.rb +++ b/lib/schemable.rb @@ -25,7 +25,7 @@ module Schemable klasses.each do |klass| model_definition = klass.new - schema = AttributeSchemaGenerator.new(model_definition).generate_attributes_schema + schema = AttributeSchemaGenerator.new(model_definition).generate generated_schemas << schema end diff --git a/lib/schemable/attribute_schema_generator.rb b/lib/schemable/attribute_schema_generator.rb index cdc4a5a..c5803f2 100644 --- a/lib/schemable/attribute_schema_generator.rb +++ b/lib/schemable/attribute_schema_generator.rb @@ -11,7 +11,7 @@ module Schemable end # Generate the JSON schema for attributes - def generate_attributes_schema + def generate schema = { type: :object, properties: @model_definition.attributes.index_with do |attr| diff --git a/lib/schemable/included_schema_generator.rb b/lib/schemable/included_schema_generator.rb index 8bc6997..4786c01 100644 --- a/lib/schemable/included_schema_generator.rb +++ b/lib/schemable/included_schema_generator.rb @@ -11,21 +11,6 @@ module Schemable @relationships_to_exclude_from_expansion = relationships_to_exclude_from_expansion end - def prepare_schema_for_included(model_definition, expand: false, relationships_to_exclude_from_expansion: []) - attributes_schema = AttributeSchemaGenerator.new(model_definition).generate_attributes_schema - relationships_schema = RelationshipSchemaGenerator.new(model_definition, relationships_to_exclude_from_expansion:, expand:) - - { - type: :object, - properties: { - type: { type: :string, default: model_definition.model_name }, - id: { type: :string }, - attributes: attributes_schema, - relationships: relationships_schema ? {} : relationships_schema - } - }.compact_blank - end - def generate(expand: false, relationships_to_exclude_from_expansion: []) return {} if @relationships.blank? return {} if @relationships == { belongs_to: {}, has_many: {} } @@ -68,5 +53,20 @@ module Schemable schema end + + 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:) + + { + type: :object, + properties: { + type: { type: :string, default: model_definition.model_name }, + id: { type: :string }, + attributes: attributes_schema, + relationships: relationships_schema ? {} : relationships_schema + } + }.compact_blank + end end end diff --git a/sig/schemable/attribute_schema_generator.rbs b/sig/schemable/attribute_schema_generator.rbs index e3013e0..b321b5e 100644 --- a/sig/schemable/attribute_schema_generator.rbs +++ b/sig/schemable/attribute_schema_generator.rbs @@ -7,7 +7,7 @@ module Schemable attr_accessor schema_modifier: SchemaModifier def initialize: (Definition) -> void - def generate_attributes_schema: -> (Hash[Symbol, any] | Array[any]) + def generate: -> (Hash[Symbol, any] | Array[any]) def generate_attribute_schema: (Symbol) -> Hash[Symbol, any] end end