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
- #additional_properties_allowed? ⇒ Boolean
-
#build_schema(schema_definition) ⇒ Schema
Builds the schema using the provided schema definition.
-
#define_schema { ... } ⇒ Object
Define the schema for the model using the provided block.
-
#json_schema ⇒ Hash
Returns the JSON schema for the model.
-
#properties ⇒ Array<Symbol>
Returns the property names defined in the schema.
-
#ref_template ⇒ String
Returns the reference template for the model.
-
#schema ⇒ Schema
Returns the schema for the model.
-
#schema_definition ⇒ SchemaDefinition
Returns the unvalidated schema definition for the model.
Instance Method Details
#additional_properties_allowed? ⇒ Boolean
254 255 256 |
# File 'lib/easy_talk/model.rb', line 254 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.
270 271 272 |
# File 'lib/easy_talk/model.rb', line 270 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.
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/easy_talk/model.rb', line 219 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_schema ⇒ Hash
Returns the JSON schema for the model. This is the final output that includes the $schema keyword if configured.
161 162 163 |
# File 'lib/easy_talk/model.rb', line 161 def json_schema @json_schema ||= build_json_schema end |
#properties ⇒ Array<Symbol>
Returns the property names defined in the schema
261 262 263 |
# File 'lib/easy_talk/model.rb', line 261 def properties (@schema_definition&.schema&.dig(:properties) || {}).keys end |
#ref_template ⇒ String
Returns the reference template for the model.
153 154 155 |
# File 'lib/easy_talk/model.rb', line 153 def ref_template "#/$defs/#{name}" end |
#schema ⇒ Schema
Returns the schema for the model.
142 143 144 145 146 147 148 |
# File 'lib/easy_talk/model.rb', line 142 def schema @schema ||= if defined?(@schema_definition) && @schema_definition build_schema(@schema_definition) else {} end end |
#schema_definition ⇒ SchemaDefinition
Returns the unvalidated schema definition for the model.
250 251 252 |
# File 'lib/easy_talk/model.rb', line 250 def schema_definition @schema_definition ||= {} end |