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)


208
209
210
# File 'lib/easy_talk/model.rb', line 208

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.



224
225
226
# File 'lib/easy_talk/model.rb', line 224

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.



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/easy_talk/model.rb', line 173

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.

Returns:

  • (Hash)

    The JSON schema for the model.



165
166
167
# File 'lib/easy_talk/model.rb', line 165

def json_schema
  @json_schema ||= schema.as_json
end

#propertiesArray<Symbol>

Returns the property names defined in the schema

Returns:

  • (Array<Symbol>)

    Array of property names as symbols



215
216
217
# File 'lib/easy_talk/model.rb', line 215

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.



158
159
160
# File 'lib/easy_talk/model.rb', line 158

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
149
150
151
152
153
# File 'lib/easy_talk/model.rb', line 142

def schema
  @schema ||= if defined?(@schema_definition) && @schema_definition
                # Schema defined explicitly via define_schema
                build_schema(@schema_definition)
              elsif respond_to?(:active_record_schema_definition)
                # ActiveRecord model without explicit schema definition
                build_schema(active_record_schema_definition)
              else
                # Default case - empty schema
                {}
              end
end

#schema_definitionSchemaDefinition

Returns the unvalidated schema definition for the model.

Returns:



204
205
206
# File 'lib/easy_talk/model.rb', line 204

def schema_definition
  @schema_definition ||= {}
end