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
-
#define_schema { ... } ⇒ Object
Define the schema for the model using the provided block.
-
#function_name ⇒ String
Returns the name of the model as a human-readable function name.
-
#inherits_schema? ⇒ Boolean
Returns true if the class inherits a schema.
-
#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
#define_schema { ... } ⇒ Object
Define the schema for the model using the provided block.
115 116 117 118 119 120 121 122 123 |
# File 'lib/easy_talk/model.rb', line 115 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_defintion end |
#function_name ⇒ String
Returns the name of the model as a human-readable function name.
92 93 94 |
# File 'lib/easy_talk/model.rb', line 92 def function_name name.humanize.titleize end |
#inherits_schema? ⇒ Boolean
Returns true if the class inherits a schema.
78 79 80 |
# File 'lib/easy_talk/model.rb', line 78 def inherits_schema? false end |
#json_schema ⇒ Hash
Returns the JSON schema for the model.
107 108 109 |
# File 'lib/easy_talk/model.rb', line 107 def json_schema @json_schema ||= schema.as_json end |
#properties ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/easy_talk/model.rb', line 96 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.
85 86 87 |
# File 'lib/easy_talk/model.rb', line 85 def ref_template "#/$defs/#{name}" end |
#schema ⇒ Schema
Returns the schema for the model.
71 72 73 |
# File 'lib/easy_talk/model.rb', line 71 def schema @schema ||= build_schema(schema_definition) end |
#schema_definition ⇒ SchemaDefinition
Returns the unvalidated schema definition for the model.
128 129 130 |
# File 'lib/easy_talk/model.rb', line 128 def schema_definition @schema_definition ||= {} end |