class

Luna::BaseModel

Inherits Luna::Associations < Luna::StateMachine < Luna::Sti < ActiveModel::Validation < ActiveModel::Callbacks < ActiveModel::Model < DB::Mappable < DB::Serializable < YAML::Serializable < JSON::Serializable < Reference < Object

Constants

AM_PARENT_TYPE = {:type => Luna::BaseModel} of Nil => Nil
CALLBACKS = { before_save: [] of Nil, after_save: [] of Nil, before_create: [] of Nil, after_create: [] of Nil, before_update: [] of Nil, after_update: [] of Nil, before_destroy: [] of Nil, after_destroy: [] of Nil, }

Constructors

__from_db_hash(row : Hash(String, DB::Any)) : self
Source
find!(id : Int64) : self
Source
find_by!(filters : Hash(Symbol, DB::Any) | NamedTuple) : self
Source
from_db_row(rs : DB::ResultSet) : self
Source
from_db_rs(rs : DB::ResultSet) : self

Class methods

__eager_load_paths(records : Array(self), paths : Array(Array(Symbol)))
Source
__evict_cached_record(record : self) : Nil
Source
__read_row_hash(rs : DB::ResultSet) : Hash(String, DB::Any)
Source
__sti_class_for(type_value : String) : Luna::BaseModel.class | Nil
Source
__write_cached_record(record : self) : Nil
Source
attributes

Returns all attribute keys.

avg(column : String, as : T.class) : T | Nil forall T
Source
cache_by_id_enabled?
Source
cache_by_id_limit
Source
cache_by_id_ttl_seconds
Source
connection_name

--- Instance persistence ---

Source
count(column : String = "*", distinct : Bool = false) : Int64

Aggregates & helpers (class level)

Source
db_connection

--- Class helpers ---

Source
db_dialect
Source
enum_from_db(raw : DB::Any, enum_klass : T.class) : T forall T
Source
exists?(filters : Hash(Symbol, DB::Any) | NamedTuple | Nil = nil) : Bool
Source
find(id : Int64) : self | Nil
Source
find_by(filters : Hash(Symbol, DB::Any) | NamedTuple) : self | Nil
Source
from_rs(rs : DB::ResultSet)
Source
includes(*incs : Symbol, **nested) : Relation(self)
Source
limit(n : Int32) : Relation(self)
Source
max(column : String, as : T.class) : T | Nil forall T
Source
min(column : String, as : T.class) : T | Nil forall T
Source
offset(n : Int32) : Relation(self)
Source
order(*cols : String) : Relation(self)
Source
pluck(column : String, as : T.class) : Array(T) forall T
Source
primary_key_field
Source
select(*cols : String) : Relation(self)
Source
sti_column_name
Source
sti_persisted_type_value
Source
sti_scope_type_value
Source
sum(column : String, as : T.class) : T | Nil forall T
Source
table_name

--- Table name helpers ---

Source
transaction

Transactions

Source
where(filters : Hash(Symbol, DB::Any) | NamedTuple) : Relation(self)
Source
where(cond : String, *params : DB::Any) : Relation(self)
Source

Instance methods

__assign_field_from_db_any(column_name : String, value : DB::Any)
after_create
after_destroy
after_save
after_update
apply_defaults

Generate code to apply default values

assign_attributes(params : HTTP::Params | Hash(String, String) | Tuple(String, String))

Assign to multiple attributes via HTTP::Params.

assign_attributes(model : Luna::BaseModel)

Assign to multiple attributes from a model object

assign_attributes

Assign to multiple attributes.

attributes

Returns a Hash of all attribute values

attributes_tuple

Returns a NamedTuple of all attribute values.

before_create
before_destroy
before_save
before_update
destroy
Source
fetched=(fetched : Bool)
Source
get_preloaded(name : Symbol) : Luna::BaseModel | Nil
Source
get_preloaded_many(name : Symbol) : Array(Luna::BaseModel) | Nil
Source
persistent_attributes

Returns a Hash of all attributes that can be persisted.

read_attribute(key : String) : DB::Any
Source
read_attribute?(key : String) : DB::Any | Nil
Source
run_create_callbacks

Wrap a block with callbacks for the appropriate crud operation.

Defined once here rather than once per subclass, and reached through the before_*/after_* methods rather than by expanding __before_* inline.

These methods yield, and Crystal cannot dispatch a yielding method through a pointer -- it inlines the body at each call site. Defining them per subclass therefore meant that a run_*_callbacks call on an abstract receiver emitted one inlined copy per concrete subclass, each containing a type-id dispatch chain for every self call in the block. Nesting two of them (run_update_callbacks { run_save_callbacks { ... } }) made that quadratic in the number of models.

With a single definition there is one inlined copy, and before_* / after_* are ordinary non-yielding overrides that share one dispatch wrapper.

Source
run_destroy_callbacks
Source
run_save_callbacks
Source
run_update_callbacks
Source
save
Source
set_preloaded(name : Symbol, value : Luna::BaseModel | Nil)

Default preload hooks. Association macros override these and chain with previous_def.

Source
set_preloaded_many(name : Symbol, value : Array(Luna::BaseModel))
Source
table_name
Source
update
Source

Macros

__customize_orm__

--- Row mapping macro hook ---

Source
cache_by_id(ttl_seconds = 60, limit = 1000)
Source
connection(name)

--- Macros ---

Source
primary_key(field)
Source