Module: EasyTalk::SchemaBase::ClassMethods

Includes:
EasyTalk::SchemaMethods
Included in:
Model::ClassMethods, EasyTalk::Schema::ClassMethods
Defined in:
lib/easy_talk/schema_base.rb

Overview

Class methods shared by Schema and Model.

Instance Method Summary collapse

Instance Method Details

#additional_properties_allowed?Boolean

Returns:

  • (Boolean)


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

def additional_properties_allowed?
  ap = @schema_definition&.schema&.fetch(:additional_properties, false)
  ap == true || ap.is_a?(Class) || ap.is_a?(Hash)
end

#define_schemaObject

Raises:

  • (ArgumentError)


141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/easy_talk/schema_base.rb', line 141

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

  clear_schema_state!

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

  defined_properties = (@schema_definition.schema[:properties] || {}).keys
  attr_accessor(*defined_properties)

  @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.

#propertiesObject



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

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.

Returns:

  • (String)

    The reference template for the model.

#schemaObject



133
134
135
136
137
138
139
# File 'lib/easy_talk/schema_base.rb', line 133

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

#schema_definitionObject



156
157
158
# File 'lib/easy_talk/schema_base.rb', line 156

def schema_definition
  @schema_definition ||= {}
end