Module: EasyTalk::Model

Defined in:
lib/easy_talk/model.rb

Overview

The ‘Model` module is a mixin that provides functionality for defining and accessing the schema of a model.

It includes methods for defining the schema, retrieving the schema definition, and generating the JSON schema for the model.

Example usage:

class Person
  include EasyTalk::Model

  define_schema do
    property :name, String, description: 'The person\'s name'
    property :age, Integer, description: 'The person\'s age'
  end
end

Person.json_schema #=> returns the JSON schema for Person
jim = Person.new(name: 'Jim', age: 30)
jim.valid? #=> returns true

See Also:

Defined Under Namespace

Modules: ClassMethods Classes: SchemaValidator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/easy_talk/model.rb', line 38

def self.included(base)
  base.include ActiveModel::API # Include ActiveModel::API in the class including EasyTalk::Model
  base.include ActiveModel::Validations
  base.extend ActiveModel::Callbacks
  base.validates_with SchemaValidator
  base.extend(ClassMethods)
end

Instance Method Details

#propertiesObject

Returns the properties of the model as a hash with symbolized keys.



62
63
64
# File 'lib/easy_talk/model.rb', line 62

def properties
  as_json.symbolize_keys!
end