Class: Schemable::RequestSchemaGenerator
- Inherits:
-
Object
- Object
- Schemable::RequestSchemaGenerator
- Defined in:
- lib/schemable/request_schema_generator.rb
Overview
The RequestSchemaGenerator class is responsible for generating JSON schemas for create and update requests. This class generates schemas based on the model definition, including additional and excluded attributes.
Instance Attribute Summary collapse
-
#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_for_create ⇒ Hash
Generates the JSON schema for a create request.
-
#generate_for_update ⇒ Hash
Generates the JSON schema for a update request.
-
#initialize(model_definition) ⇒ RequestSchemaGenerator
constructor
Initializes a new RequestSchemaGenerator instance.
Constructor Details
#initialize(model_definition) ⇒ RequestSchemaGenerator
Initializes a new RequestSchemaGenerator instance.
15 16 17 18 |
# File 'lib/schemable/request_schema_generator.rb', line 15 def initialize(model_definition) @model_definition = model_definition @schema_modifier = SchemaModifier.new end |
Instance Attribute Details
#model_definition ⇒ Object (readonly)
Returns the value of attribute model_definition.
7 8 9 |
# File 'lib/schemable/request_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/request_schema_generator.rb', line 7 def schema_modifier @schema_modifier end |
Instance Method Details
#generate_for_create ⇒ Hash
Generates the JSON schema for a create request. It generates a schema for the model attributes and then modifies it based on the additional and excluded attributes for create requests. It also determines the required attributes based on the optional and nullable attributes. Note that it is presumed that the model is using the same fields/columns for create as well as responses.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/schemable/request_schema_generator.rb', line 29 def generate_for_create schema = { type: :object, properties: { data: AttributeSchemaGenerator.new(@model_definition).generate } } @schema_modifier.add_properties(schema, @model_definition.additional_create_request_attributes, 'properties.data.properties') @model_definition.excluded_create_request_attributes.each do |key| @schema_modifier.delete_properties(schema, "properties.data.properties.#{key}") end required_attributes = { required: ( schema.as_json['properties']['data']['properties'].keys - @model_definition.optional_create_request_attributes.map(&:to_s) - @model_definition.nullable_attributes.map(&:to_s) ).map { |key| key.to_s.camelize(:lower).to_sym } } @schema_modifier.add_properties(schema, required_attributes, 'properties.data') end |
#generate_for_update ⇒ Hash
Generates the JSON schema for a update request. It generates a schema for the model attributes and then modifies it based on the additional and excluded attributes for update requests. It also determines the required attributes based on the optional and nullable attributes. Note that it is presumed that the model is using the same fields/columns for update as well as responses.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/schemable/request_schema_generator.rb', line 63 def generate_for_update schema = { type: :object, properties: { data: AttributeSchemaGenerator.new(@model_definition).generate } } @schema_modifier.add_properties(schema, @model_definition.additional_update_request_attributes, 'properties.data.properties') @model_definition.excluded_update_request_attributes.each do |key| @schema_modifier.delete_properties(schema, "properties.data.properties.#{key}") end required_attributes = { required: ( schema.as_json['properties']['data']['properties'].keys - @model_definition.optional_update_request_attributes.map(&:to_s) - @model_definition.nullable_attributes.map(&:to_s) ).map { |key| key.to_s.camelize(:lower).to_sym } } @schema_modifier.add_properties(schema, required_attributes, 'properties.data') end |