Module: EasyTalk::Schema::ClassMethods

Includes:
EasyTalk::SchemaMethods
Defined in:
lib/easy_talk/schema.rb

Overview

Class methods for schema-only models.

Instance Method Summary collapse

Instance Method Details

#additional_properties_allowed?Boolean

Check if additional properties are allowed.



177
178
179
# File 'lib/easy_talk/schema.rb', line 177

def additional_properties_allowed?
  @schema_definition&.schema&.fetch(:additional_properties, false)
end

#define_schema { ... } ⇒ Object

Define the schema for the model using the provided block. Unlike EasyTalk::Model, this does NOT apply any validations.

Yields:

  • The block to define the schema.

Raises:

  • (ArgumentError)

    If the class does not have a name.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/easy_talk/schema.rb', line 151

def define_schema(&)
  raise ArgumentError, 'The class must have a name' unless name.present?

  @schema_definition = SchemaDefinition.new(name)
  @schema_definition.klass = self
  @schema_definition.instance_eval(&)

  # Define accessors for all properties
  defined_properties = (@schema_definition.schema[:properties] || {}).keys
  attr_accessor(*defined_properties)

  # NO validations are applied - this is schema-only

  @schema_definition
end

#json_schemaHash Originally defined in module EasyTalk::SchemaMethods

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

#propertiesArray<Symbol>

Returns the property names defined in the schema.



184
185
186
# File 'lib/easy_talk/schema.rb', line 184

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

#ref_templateString Originally defined in module EasyTalk::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.

#schemaHash

Returns the schema for the model.



138
139
140
141
142
143
144
# File 'lib/easy_talk/schema.rb', line 138

def schema
  @schema ||= if defined?(@schema_definition) && @schema_definition
                build_schema(@schema_definition)
              else
                {}
              end
end

#schema_definitionSchemaDefinition

Returns the schema definition for the model.



170
171
172
# File 'lib/easy_talk/schema.rb', line 170

def schema_definition
  @schema_definition ||= {}
end