Module: EasyTalk::Model::ClassMethods

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)


254
255
256
# File 'lib/easy_talk/model.rb', line 254

def additional_properties_allowed?
  @schema_definition&.schema&.fetch(:additional_properties, false)
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.



270
271
272
# File 'lib/easy_talk/model.rb', line 270

def build_schema(schema_definition)
  Builders::ObjectBuilder.new(schema_definition).build
end

#define_schema { ... } ⇒ Object

Define the schema for the model using the provided block.

Yields:

  • The block to define the schema.

Raises:

  • (ArgumentError)

    If the class does not have a name.



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/easy_talk/model.rb', line 219

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(&)

  # 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

  # Apply auto-validations immediately after definition
  if EasyTalk.configuration.auto_validations
    (@schema_definition.schema[:properties] || {}).each do |prop_name, prop_def|
      # Only apply validations if they haven't been applied yet
      unless @validated_properties.include?(prop_name)
        ValidationBuilder.build_validations(self, prop_name, prop_def[:type], prop_def[:constraints])
        @validated_properties.add(prop_name)
      end
    end
  end

  @schema_definition
end

#json_schemaHash

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.



161
162
163
# File 'lib/easy_talk/model.rb', line 161

def json_schema
  @json_schema ||= build_json_schema
end

#propertiesArray<Symbol>

Returns the property names defined in the schema

Returns:

  • (Array<Symbol>)

    Array of property names as symbols



261
262
263
# File 'lib/easy_talk/model.rb', line 261

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

#ref_templateString

Returns the reference template for the model.

Returns:

  • (String)

    The reference template for the model.



153
154
155
# File 'lib/easy_talk/model.rb', line 153

def ref_template
  "#/$defs/#{name}"
end

#schemaSchema

Returns the schema for the model.

Returns:

  • (Schema)

    The schema for the model.



142
143
144
145
146
147
148
# File 'lib/easy_talk/model.rb', line 142

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:



250
251
252
# File 'lib/easy_talk/model.rb', line 250

def schema_definition
  @schema_definition ||= {}
end