class

Lustra::Model::Column(T, C)

Inherits Lustra::ErrorMessages < Reference < Object

A column of a Model Provide some methods like:

  • Informations persistance (value before, value changed?)
  • Raise error if we try to access the value of a field which is not gathered through the query system (uninitialized column). Or use the get_def to get with default value

Constants

UNKNOWN = UnknownClass.new

Constructors

new(name : String, value : T | UnknownClass = UNKNOWN, has_db_default : Bool = false)
Source

Instance methods

change

Returns the change for this column as a tuple of {old_value, new_value}. Returns nil if the column hasn't changed or if old_value is not defined.

user.email = "new@test.com"
user.email_column.change # => {"old@test.com", "new@test.com"}
user.name_column.change  # => nil (if not changed)
Source
changed?
Source
clear

Completely clear the column, remove both value and old_value and turning the column in a non-defined state.

Source
clear_change_flag

Reset changed? flag to false. See Column(T)#dirty! for the counter part.

Source
defined?

Check whether the column is defined or not.

Source
dirty!

Reset changed? flag to true. See Column(T)#clear_change_flag for the counter part.

Source
has_db_default?
Source
inspect

Inspect this column. If a column is not loaded (e.g. not defined once), it will show "#undef". If a column is dirty (e.g. change hasn't be saved), it will show a "*" after the value.

Source
name
Source
nilable?

Return true if the value is an union of a Type with Nilable, false otherwise.

Source
old_value
Source
reset(x : T | Nil)

Reset the current field. Restore the old_value state to current value. Reset the flag changed to false.

Source
reset_convert(x)
Source
revert

If the column is dirty (e.g the value has been changed), return to the previous state.

Source
set(x : T | Nil)
Source
set_convert(x)
Source
to_sql_value(default = nil) : Lustra::SQL::Any

Return the database converted value using the converter

Source
value(default : X) : T | X forall X

Returns the current value of this column or default if the value is undefined.

Source
value

Returns the current value of this column. If the value has never been initialized, throw an exception

Source
value=(x : T)

Set the value of the column to the value x. If x is not equal to the old value, then the column changed? flag is set to true.

Source

Nested types