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
208 209 210 |
# File 'lib/easy_talk/model.rb', line 208 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.
224 225 226 |
# File 'lib/easy_talk/model.rb', line 224 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.
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/easy_talk/model.rb', line 173 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.
165 166 167 |
# File 'lib/easy_talk/model.rb', line 165 def json_schema @json_schema ||= schema.as_json end |
#properties ⇒ Array<Symbol>
Returns the property names defined in the schema
215 216 217 |
# File 'lib/easy_talk/model.rb', line 215 def properties (@schema_definition&.schema&.dig(:properties) || {}).keys end |
#ref_template ⇒ String
Returns the reference template for the model.
158 159 160 |
# File 'lib/easy_talk/model.rb', line 158 def ref_template "#/$defs/#{name}" end |
#schema ⇒ Schema
Returns the schema for the model.
142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/easy_talk/model.rb', line 142 def schema @schema ||= if defined?(@schema_definition) && @schema_definition # Schema defined explicitly via define_schema build_schema(@schema_definition) elsif respond_to?(:active_record_schema_definition) # ActiveRecord model without explicit schema definition build_schema(active_record_schema_definition) else # Default case - empty schema {} end end |
#schema_definition ⇒ SchemaDefinition
Returns the unvalidated schema definition for the model.
204 205 206 |
# File 'lib/easy_talk/model.rb', line 204 def schema_definition @schema_definition ||= {} end |