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.

Returns:

  • (Boolean)

    True if additional properties are allowed.



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

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.



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

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.

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.



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

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

#ref_templateString Originally defined in module EasyTalk::SchemaMethods

Returns the reference template for the model.

Returns:

  • (String)

    The reference template for the model.

#schemaHash

Returns the schema for the model.

Returns:

  • (Hash)

    The schema for the model.



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

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.

Returns:



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

def schema_definition
  @schema_definition ||= {}
end