Class: Schemable::Definition
- Inherits:
-
Object
- Object
- Schemable::Definition
- Defined in:
- lib/schemable/definition.rb
Overview
The Definition class provides a blueprint for generating and modifying schemas. It includes methods for handling attributes, relationships, and various request and response attributes. The definition class is meant to be inherited by a class that represents a model. This class should be configured to match the model’s attributes and relationships. The default configuration is set in this class, but can be overridden in the model’s definition class.
Instance Attribute Summary collapse
-
#additional_create_request_attributes ⇒ Hash
Returns the additional create request attributes that are not automatically generated.
-
#additional_update_request_attributes ⇒ Hash
Returns the additional update request attributes that are not automatically generated.
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#relationships ⇒ Hash
Returns the relationships defined in the serializer.
Class Method Summary collapse
-
.generate ⇒ Array<Hash>
Returns the schema for the create request body, update request body, and response body.
Instance Method Summary collapse
-
#additional_response_attributes ⇒ Hash
Returns the additional response attributes that are not automatically generated.
-
#additional_response_included ⇒ Hash
Returns the additional response included that are not automatically generated.
-
#additional_response_relations ⇒ Hash
Returns the additional response relations that are not automatically generated.
-
#array_types ⇒ Hash
Returns a hash of all the arrays defined for the model.
-
#attributes ⇒ Array<Symbol>
Returns the attributes for the definition based on the configuration.
-
#camelize_keys(hash) ⇒ Hash, Array
Given a hash, it returns a new hash with all the keys camelized.
-
#default_value_for_enum_attributes ⇒ Hash
Returns the default value for the enum attributes.
-
#excluded_create_request_attributes ⇒ Array<Symbol>
Returns the attributes that are excluded from the create request schema.
-
#excluded_response_attributes ⇒ Array<Symbol>
Returns the attributes that are excluded from the update request schema.
-
#excluded_response_included ⇒ Array<Symbol>
Returns the included that are excluded from the response schema.
-
#excluded_response_relations ⇒ Array<Symbol>
Returns the relationships that are excluded from the response schema.
-
#excluded_update_request_attributes ⇒ Array<Symbol>
Returns the attributes that are excluded from the response schema.
-
#initialize ⇒ Definition
constructor
A new instance of Definition.
-
#model ⇒ Class
Returns the model class (Constantized from the definition class name).
-
#model_name ⇒ String
Returns the model name.
-
#nullable_attributes ⇒ Array<Symbol>
Returns the attributes that are nullable in the request/response body.
-
#nullable_relationships ⇒ Array<String>
Returns the relationships that are nullable in the response body.
-
#optional_create_request_attributes ⇒ Array<Symbol>
Attributes that are not required in the create request.
-
#optional_update_request_attributes ⇒ Array<Symbol>
Attributes that are not required in the update request.
-
#serialized_instance ⇒ Hash
Returns an instance of the model class that is already serialized into jsonapi format.
-
#serializer ⇒ JSONAPI::Serializable::Resource?
Returns the serializer of the model for the definition.
Constructor Details
#initialize ⇒ Definition
Returns a new instance of Definition.
13 14 15 |
# File 'lib/schemable/definition.rb', line 13 def initialize @configuration = Schemable.configuration end |
Instance Attribute Details
#additional_create_request_attributes ⇒ Hash
Returns the additional create request attributes that are not automatically generated. These attributes are appended to the create request schema.
147 148 149 |
# File 'lib/schemable/definition.rb', line 147 def additional_create_request_attributes {} end |
#additional_update_request_attributes ⇒ Hash
Returns the additional update request attributes that are not automatically generated. These attributes are appended to the update request schema.
158 159 160 |
# File 'lib/schemable/definition.rb', line 158 def additional_update_request_attributes {} end |
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
10 11 12 |
# File 'lib/schemable/definition.rb', line 10 def configuration @configuration end |
#relationships ⇒ Hash
Note that the format of the relationships is as follows: {
belongs_to: { relationship_name: relationship_definition },
has_many: { relationship_name: relationship_definition },
addition_to_included: { relationship_name: relationship_definition }
}
The addition_to_included is used to define the extra nested relationships that are not defined in the belongs_to or has_many for included.
Returns the relationships defined in the serializer.
70 71 72 |
# File 'lib/schemable/definition.rb', line 70 def relationships { belongs_to: {}, has_many: {} } end |
Class Method Details
.generate ⇒ Array<Hash>
Returns the schema for the create request body, update request body, and response body.
336 337 338 339 340 341 342 343 344 345 |
# File 'lib/schemable/definition.rb', line 336 def self.generate instance = new [ "#{instance.model}CreateRequest": instance.camelize_keys(RequestSchemaGenerator.new(instance).generate_for_create), "#{instance.model}UpdateRequest": instance.camelize_keys(RequestSchemaGenerator.new(instance).generate_for_update), "#{instance.model}Response": instance.camelize_keys(ResponseSchemaGenerator.new(instance).generate(collection: true)), "#{instance.model}ResponseExpanded": instance.camelize_keys(ResponseSchemaGenerator.new(instance).generate(expand: true)) ] end |
Instance Method Details
#additional_response_attributes ⇒ Hash
Returns the additional response attributes that are not automatically generated. These attributes are appended to the response schema.
168 169 170 |
# File 'lib/schemable/definition.rb', line 168 def additional_response_attributes {} end |
#additional_response_included ⇒ Hash
Returns the additional response included that are not automatically generated. These included additions are appended to the response schema’s included.
218 219 220 |
# File 'lib/schemable/definition.rb', line 218 def additional_response_included {} end |
#additional_response_relations ⇒ Hash
Returns the additional response relations that are not automatically generated. These relations are appended to the response schema’s relationships.
195 196 197 |
# File 'lib/schemable/definition.rb', line 195 def additional_response_relations {} end |
#array_types ⇒ Hash
Returns a hash of all the arrays defined for the model. The schema for each array is defined in the definition class manually. This method must be implemented in the definition class if there are any arrays.
90 91 92 |
# File 'lib/schemable/definition.rb', line 90 def array_types {} end |
#attributes ⇒ Array<Symbol>
Returns the attributes for the definition based on the configuration. The attributes are inferred from the model’s attribute names by default. If the infer_attributes_from_custom_method configuration option is set, the attributes are inferred from the method specified. If the infer_attributes_from_jsonapi_serializable configuration option is set, the attributes are inferred from the serializer’s attribute blocks.
36 37 38 39 40 41 42 |
# File 'lib/schemable/definition.rb', line 36 def attributes 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.map(&:to_sym) end |
#camelize_keys(hash) ⇒ Hash, Array
Given a hash, it returns a new hash with all the keys camelized.
329 330 331 |
# File 'lib/schemable/definition.rb', line 329 def camelize_keys(hash) hash.deep_transform_keys { |key| key.to_s.camelize(:lower).to_sym } end |
#default_value_for_enum_attributes ⇒ Hash
Returns the default value for the enum attributes.
289 290 291 |
# File 'lib/schemable/definition.rb', line 289 def default_value_for_enum_attributes {} end |
#excluded_create_request_attributes ⇒ Array<Symbol>
Returns the attributes that are excluded from the create request schema. These attributes are not required or not needed to be present in the create request body.
229 230 231 |
# File 'lib/schemable/definition.rb', line 229 def excluded_create_request_attributes %i[] end |
#excluded_response_attributes ⇒ Array<Symbol>
Returns the attributes that are excluded from the update request schema. These attributes are not required or not needed to be present in the update request body.
251 252 253 |
# File 'lib/schemable/definition.rb', line 251 def excluded_response_attributes %i[] end |
#excluded_response_included ⇒ Array<Symbol>
This method is not used anywhere yet.
Returns the included that are excluded from the response schema. These included are not needed to be present in the response body.
276 277 278 |
# File 'lib/schemable/definition.rb', line 276 def excluded_response_included %i[] end |
#excluded_response_relations ⇒ Array<Symbol>
Returns the relationships that are excluded from the response schema. These relationships are not needed to be present in the response body.
262 263 264 |
# File 'lib/schemable/definition.rb', line 262 def excluded_response_relations %i[] end |
#excluded_update_request_attributes ⇒ Array<Symbol>
Returns the attributes that are excluded from the response schema. These attributes are not needed to be present in the response body.
240 241 242 |
# File 'lib/schemable/definition.rb', line 240 def excluded_update_request_attributes %i[] end |
#model ⇒ Class
Returns the model class (Constantized from the definition class name)
306 307 308 |
# File 'lib/schemable/definition.rb', line 306 def model self.class.name.gsub('Swagger::Definitions::', '').constantize end |
#model_name ⇒ String
Returns the model name. Used for schema type naming.
317 318 319 |
# File 'lib/schemable/definition.rb', line 317 def model_name self.class.name.gsub('Swagger::Definitions::', '').pluralize.underscore.downcase end |
#nullable_attributes ⇒ Array<Symbol>
Returns the attributes that are nullable in the request/response body. This means that they can be present in the request/response body but they can be null. They are not required to be present in the request body.
124 125 126 |
# File 'lib/schemable/definition.rb', line 124 def nullable_attributes %i[] end |
#nullable_relationships ⇒ Array<String>
Returns the relationships that are nullable in the response body. This means that they can be present in the response body but they can be null. They are not required to be present in the request body.
136 137 138 |
# File 'lib/schemable/definition.rb', line 136 def nullable_relationships %w[] end |
#optional_create_request_attributes ⇒ Array<Symbol>
Attributes that are not required in the create request.
101 102 103 |
# File 'lib/schemable/definition.rb', line 101 def optional_create_request_attributes %i[] end |
#optional_update_request_attributes ⇒ Array<Symbol>
Attributes that are not required in the update request.
112 113 114 |
# File 'lib/schemable/definition.rb', line 112 def optional_update_request_attributes %i[] end |
#serialized_instance ⇒ Hash
Returns an instance of the model class that is already serialized into jsonapi format.
296 297 298 |
# File 'lib/schemable/definition.rb', line 296 def serialized_instance {} end |
#serializer ⇒ JSONAPI::Serializable::Resource?
Returns the serializer of the model for the definition.
21 22 23 24 25 |
# File 'lib/schemable/definition.rb', line 21 def serializer 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 |