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 ⇒ Object
-
#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
154 155 156 |
# File 'lib/easy_talk/model.rb', line 154 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.
163 164 165 |
# File 'lib/easy_talk/model.rb', line 163 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.
137 138 139 140 141 142 143 144 145 |
# File 'lib/easy_talk/model.rb', line 137 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_schema ⇒ Hash
Returns the JSON schema for the model.
129 130 131 |
# File 'lib/easy_talk/model.rb', line 129 def json_schema @json_schema ||= schema.as_json end |
#properties ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/easy_talk/model.rb', line 118 def properties @properties ||= begin return unless schema[:properties].present? schema[:properties].keys.map(&:to_sym) end end |
#ref_template ⇒ String
Returns the reference template for the model.
114 115 116 |
# File 'lib/easy_talk/model.rb', line 114 def ref_template "#/$defs/#{name}" end |
#schema ⇒ Schema
Returns the schema for the model.
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/easy_talk/model.rb', line 98 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.
150 151 152 |
# File 'lib/easy_talk/model.rb', line 150 def schema_definition @schema_definition ||= {} end |