Module: EasyTalk::Model::ClassMethods
- Includes:
- SchemaMethods
- Defined in:
- lib/easy_talk/model.rb
Overview
Module containing class-level methods for defining and accessing the schema of a model.
Instance Method Summary collapse
- #additional_properties_allowed? ⇒ Boolean
-
#build_schema(schema_definition) ⇒ Schema
Builds the schema using the provided schema definition.
-
#define_schema(options = {}) { ... } ⇒ Object
Define the schema for the model using the provided block.
-
#json_schema ⇒ Hash
included
from SchemaMethods
Returns the JSON schema for the model.
-
#properties ⇒ Array<Symbol>
Returns the property names defined in the schema.
-
#ref_template ⇒ String
included
from SchemaMethods
Returns the reference template for the model.
-
#schema ⇒ Schema
Returns the schema for the model.
-
#schema_definition ⇒ SchemaDefinition
Returns the unvalidated schema definition for the model.
Instance Method Details
#additional_properties_allowed? ⇒ Boolean
311 312 313 314 315 |
# File 'lib/easy_talk/model.rb', line 311 def additional_properties_allowed? ap = @schema_definition&.schema&.fetch(:additional_properties, false) # Allow if true, or if it's a schema object (Class or Hash with type) ap == true || ap.is_a?(Class) || ap.is_a?(Hash) end |
#build_schema(schema_definition) ⇒ Schema
Builds the schema using the provided schema definition. This is the convergence point for all schema generation.
329 330 331 |
# File 'lib/easy_talk/model.rb', line 329 def build_schema(schema_definition) Builders::ObjectBuilder.new(schema_definition).build end |
#define_schema(options = {}) { ... } ⇒ Object
Define the schema for the model using the provided block.
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/easy_talk/model.rb', line 207 def define_schema( = {}, &) raise ArgumentError, 'The class must have a name' unless name.present? @schema_definition = SchemaDefinition.new(name) @schema_definition.klass = self # Pass the model class to the schema definition @schema_definition.instance_eval(&) # Store validation options for this model @validation_options = () # Define accessors immediately based on schema_definition defined_properties = (@schema_definition.schema[:properties] || {}).keys attr_accessor(*defined_properties) # Track which properties have had validations applied @validated_properties ||= Set.new # Initialize mutex eagerly for thread-safe schema-level validation application @schema_level_validation_lock = Mutex.new # Apply validations using the adapter system apply_schema_validations @schema_definition end |
#json_schema ⇒ Hash Originally defined in module SchemaMethods
Returns the JSON schema for the model. This is the final output that includes the $schema keyword if configured.
#properties ⇒ Array<Symbol>
Returns the property names defined in the schema
320 321 322 |
# File 'lib/easy_talk/model.rb', line 320 def properties (@schema_definition&.schema&.dig(:properties) || {}).keys end |
#ref_template ⇒ String Originally defined in module SchemaMethods
Returns the reference template for the model. When prefer_external_refs is enabled and the model has a schema ID, returns the external $id URI. Otherwise, returns the local $defs reference.
#schema ⇒ Schema
Returns the schema for the model.
178 179 180 181 182 183 184 |
# File 'lib/easy_talk/model.rb', line 178 def schema @schema ||= if defined?(@schema_definition) && @schema_definition build_schema(@schema_definition) else {} end end |
#schema_definition ⇒ SchemaDefinition
Returns the unvalidated schema definition for the model.
307 308 309 |
# File 'lib/easy_talk/model.rb', line 307 def schema_definition @schema_definition ||= {} end |