Class: Schemable::ResponseSchemaGenerator
- Inherits:
-
Object
- Object
- Schemable::ResponseSchemaGenerator
- Defined in:
- lib/schemable/response_schema_generator.rb
Overview
The ResponseSchemaGenerator class is responsible for generating JSON schemas for responses. This class generates schemas based on the model definition, including attributes, relationships, and included resources.
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#model_definition ⇒ Object
readonly
Returns the value of attribute model_definition.
-
#schema_modifier ⇒ Object
readonly
Returns the value of attribute schema_modifier.
Instance Method Summary collapse
-
#generate(expand: false, relationships_to_exclude_from_expansion: [], collection: false, expand_nested: false) ⇒ Hash
Generates the JSON schema for a response.
-
#initialize(model_definition) ⇒ ResponseSchemaGenerator
constructor
Initializes a new ResponseSchemaGenerator instance.
-
#jsonapi ⇒ Hash
Generates the JSON schema for the ‘jsonapi’ part of a response.
-
#meta ⇒ Hash
Generates the JSON schema for the ‘meta’ part of a response.
Constructor Details
#initialize(model_definition) ⇒ ResponseSchemaGenerator
Initializes a new ResponseSchemaGenerator instance.
15 16 17 18 19 20 |
# File 'lib/schemable/response_schema_generator.rb', line 15 def initialize(model_definition) @model_definition = model_definition @model = model_definition.model @schema_modifier = SchemaModifier.new @configuration = Schemable.configuration end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
7 8 9 |
# File 'lib/schemable/response_schema_generator.rb', line 7 def configuration @configuration end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
7 8 9 |
# File 'lib/schemable/response_schema_generator.rb', line 7 def model @model end |
#model_definition ⇒ Object (readonly)
Returns the value of attribute model_definition.
7 8 9 |
# File 'lib/schemable/response_schema_generator.rb', line 7 def model_definition @model_definition end |
#schema_modifier ⇒ Object (readonly)
Returns the value of attribute schema_modifier.
7 8 9 |
# File 'lib/schemable/response_schema_generator.rb', line 7 def schema_modifier @schema_modifier end |
Instance Method Details
#generate(expand: false, relationships_to_exclude_from_expansion: [], collection: false, expand_nested: false) ⇒ Hash
Generates the JSON schema for a response. It generates a schema for the model attributes and relationships, and if the ‘expand’ option is true, it also includes the included resources in the schema. It also adds meta and jsonapi information to the schema.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/schemable/response_schema_generator.rb', line 36 def generate(expand: false, relationships_to_exclude_from_expansion: [], collection: false, expand_nested: false) # Override expand_nested if infer_expand_nested_from_expand is true = if @configuration. data = { type: :object, properties: { type: { type: :string, default: @model_definition.model_name }, id: { type: :string }, attributes: AttributeSchemaGenerator.new(@model_definition).generate }.merge( if @model_definition.relationships.blank? || @model_definition.relationships == { belongs_to: {}, has_many: {} } {} else { relationships: RelationshipSchemaGenerator.new(@model_definition).generate(expand:, relationships_to_exclude_from_expansion:) } end ) } schema = collection ? { data: { type: :array, items: data } } : { data: } if included_schema = IncludedSchemaGenerator.new(@model_definition).generate(expand: , relationships_to_exclude_from_expansion:) @schema_modifier.add_properties(schema, included_schema, '.') end @schema_modifier.add_properties(schema, { meta: }, '.') if collection @schema_modifier.add_properties(schema, { jsonapi: }, '.') { type: :object, properties: schema }.compact_blank end |
#jsonapi ⇒ Hash
Generates the JSON schema for the ‘jsonapi’ part of a response.
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/schemable/response_schema_generator.rb', line 116 def jsonapi { type: :object, properties: { version: { type: :string, default: '1.0' } } } end |
#meta ⇒ Hash
Generates the JSON schema for the ‘meta’ part of a response. It returns a custom meta response schema if one is defined in the configuration, otherwise it generates a default meta schema.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/schemable/response_schema_generator.rb', line 75 def return @configuration. if @configuration..present? if @configuration.pagination_enabled { type: :object, properties: { page: { type: :object, properties: { totalPages: { type: :integer, default: 1 }, count: { type: :integer, default: 1 }, rowsPerPage: { type: :integer, default: 1 }, currentPage: { type: :integer, default: 1 } } } } } else {} end end |