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)


139
140
141
# File 'lib/easy_talk/model.rb', line 139

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.

Yields:

  • The block to define the schema.

Raises:

  • (ArgumentError)

    If the class does not have a name.



122
123
124
125
126
127
128
129
130
# File 'lib/easy_talk/model.rb', line 122

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

  @schema_definition = SchemaDefinition.new(name)
  @schema_definition.instance_eval(&block)
  attr_accessor(*properties)

  @schema_definition
end

#json_schemaHash

Returns the JSON schema for the model.

Returns:

  • (Hash)

    The JSON schema for the model.



114
115
116
# File 'lib/easy_talk/model.rb', line 114

def json_schema
  @json_schema ||= schema.as_json
end

#propertiesObject



103
104
105
106
107
108
109
# File 'lib/easy_talk/model.rb', line 103

def properties
  @properties ||= begin
    return unless schema[:properties].present?

    schema[:properties].keys.map(&:to_sym)
  end
end

#ref_templateString

Returns the reference template for the model.

Returns:

  • (String)

    The reference template for the model.



99
100
101
# File 'lib/easy_talk/model.rb', line 99

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

#schemaSchema

Returns the schema for the model.

Returns:

  • (Schema)

    The schema for the model.



92
93
94
# File 'lib/easy_talk/model.rb', line 92

def schema
  @schema ||= build_schema(schema_definition)
end

#schema_definitionSchemaDefinition

Returns the unvalidated schema definition for the model.

Returns:



135
136
137
# File 'lib/easy_talk/model.rb', line 135

def schema_definition
  @schema_definition ||= {}
end