Module: Castkit::ActiveRecord::ClassMethods

Includes:
Serialization
Defined in:
lib/castkit/active_record.rb

Overview

Defines class-level behavior for ActiveRecord integration.

Instance Method Summary collapse

Methods included from Serialization

#from_model, #from_relation, #nested_associations

Instance Method Details

#ensure_model_defined!(klass = self) ⇒ Object

Ensures that a model has been defined for the given DataObject class.

Parameters:

  • klass (Class) (defaults to: self)

    the DataObject class (defaults to self)

Raises:



56
57
58
# File 'lib/castkit/active_record.rb', line 56

def ensure_model_defined!(klass = self)
  raise Castkit::ActiveRecord::ModelNotDefined, klass unless klass.model
end

#ensure_model_type!(obj, klass = self) ⇒ Object

Ensures that the given object is an instance of the associated model class.

Parameters:

  • obj (Object)

    the object to validate

  • klass (Class) (defaults to: self)

    the DataObject class (defaults to self)

Raises:



66
67
68
69
# File 'lib/castkit/active_record.rb', line 66

def ensure_model_type!(obj, klass = self)
  ensure_model_defined!(klass)
  raise Castkit::ActiveRecord::InvalidModel.new(model, obj) unless obj.is_a?(model)
end

#model(model = nil) ⇒ Class

Defines or returns the ActiveRecord model class associated with the DataObject.

Parameters:

  • model (Class, nil) (defaults to: nil)

    the model to assign

Returns:

  • (Class)

    the current model class



48
49
50
# File 'lib/castkit/active_record.rb', line 48

def model(model = nil)
  model ? (@model_class = model) : @model_class
end