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
326 327 328 329 330 |
# File 'lib/easy_talk/model.rb', line 326 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.
344 345 346 |
# File 'lib/easy_talk/model.rb', line 344 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.
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/easy_talk/model.rb', line 222 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 = () # 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
335 336 337 |
# File 'lib/easy_talk/model.rb', line 335 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.
193 194 195 196 197 198 199 |
# File 'lib/easy_talk/model.rb', line 193 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.
322 323 324 |
# File 'lib/easy_talk/model.rb', line 322 def schema_definition @schema_definition ||= {} end |