abstract class Marten::DB::Field::Base

Overview

Abstract base field implementation.

Direct Known Subclasses

Defined in:

marten/db/field/base.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(id : ::String, primary_key : ::Bool = false, blank : ::Bool = false, null : ::Bool = false, unique : ::Bool = false, index : ::Bool = false, db_column : ::String | Symbol | Nil = nil) #

[View source]

Instance Method Detail

def blank? : Bool #

Returns a boolean indicating whether the field can be blank validation-wise.


[View source]
def db_column #

Returns the name of the column associated with the considered field.


[View source]
def db_column! : ::String #

Returns a mandatory non-nil version of the DB column (and raise otherwise).


[View source]
def db_column? #

Returns true if the field is associated with an in-DB column.


[View source]
abstract def default #

Returns the default value of the field if any.


[View source]
def empty_value?(value) : ::Bool #

Returns true if the value is considered empty by the field.


[View source]
abstract def from_db(value) #

Converts the raw DB value to the corresponding field value.


[View source]
abstract def from_db_result_set(result_set : ::DB::ResultSet) #

Extracts the field value from a DB result set and returns the right object corresponding to this value.


[View source]
def getter_value?(value) : ::Bool #

Returns true if true should be returned for getter?-like methods for the field.

Usually, true would be returned for values that are truthy and not empty, but this logic can be overridden on a per-field implementation basis.


[View source]
def id : String #

Returns the ID of the field used in the associated model.


[View source]
def index? : Bool #

Returns true if an index should be created at the database level for the field.


[View source]
def null? : Bool #

Returns a boolean indicating whether the field can be null at the database level.


[View source]
def prepare_save(record, new_record = false) #

Runs pre-save logic for the specific field and record at hand.

This method does nothing by default but can be overridden for specific fields that need to set values on the model instance before save or perform any pre-save logic.


[View source]
def primary_key? : Bool #

Returns a boolean indicating whether the field is a primary key.


[View source]
def related_model #

Returns the related model associated with the field.

This method will raise a NotImplementedError exception by default and should only be overriden if the #relation? method returns true (this is the case for fields such as many to one, one to one, etc).


[View source]
def relation? #

Returns true if the field is a relation.

By default this method will always return false. It should be overriden if the field is intended to handle a relation with another model (eg. like many to one or one to one fields).


[View source]
def relation_name #

Returns the name of the relation on the model associated with the field.

This method will raise a NotImplementedError exception by default and should only be overriden if the #relation? method returns true (this is the case for fields such as many to one, one to one, etc).


[View source]
abstract def to_column : Management::Column::Base | Nil #

Returns a migration column object corresponding to the field at hand.


[View source]
abstract def to_db(value) : ::DB::Any #

Converts the field value to the corresponding DB value.


[View source]
def truthy_value?(value) #

Returns true if the if the value is considered truthy by the field.


[View source]
def unique? : Bool #

Returns a boolean indicating whether the field value should be unique throughout the associated table.


[View source]
def validate(record, value) #

Runs custom validation logic for a specific model field and model object.

This method should be overriden for each field implementation that requires custom validation logic.


[View source]