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

Instance Method Details

#additional_properties_allowed?Boolean

Returns:

  • (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.

Parameters:

Returns:

  • (Schema)

    The validated schema.



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.

Examples:

Disable validations for a model

define_schema(validations: false) do
  property :name, String
end

Use a custom adapter

define_schema(validations: MyCustomAdapter) do
  property :name, String
end

Parameters:

  • options (Hash) (defaults to: {})

    Options for schema definition

Options Hash (options):

  • :validations (Boolean, Symbol, Class)

    Controls validation behavior:

    • true: Enable validations using the configured adapter (default behavior)
    • false: Disable validations for this model
    • :none: Use the NoneAdapter (no validations)
    • :active_model: Use the ActiveModelAdapter
    • CustomAdapter: Use a custom adapter class

Yields:

  • The block to define the schema.

Raises:

  • (ArgumentError)

    If the class does not have a name.



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(options = {}, &)
  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 = normalize_validation_options(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_schemaHash Originally defined in module SchemaMethods

Returns the JSON schema for the model. This is the final output that includes the $schema keyword if configured.

Returns:

  • (Hash)

    The JSON schema for the model.

#propertiesArray<Symbol>

Returns the property names defined in the schema

Returns:

  • (Array<Symbol>)

    Array of property names as symbols



320
321
322
# File 'lib/easy_talk/model.rb', line 320

def properties
  (@schema_definition&.schema&.dig(:properties) || {}).keys
end

#ref_templateString 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.

Returns:

  • (String)

    The reference template for the model.

#schemaSchema

Returns the schema for the model.

Returns:

  • (Schema)

    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_definitionSchemaDefinition

Returns the unvalidated schema definition for the model.

Returns:



307
308
309
# File 'lib/easy_talk/model.rb', line 307

def schema_definition
  @schema_definition ||= {}
end