module Ktistec::Model::InstanceMethods

Direct including types

Defined in:

framework/model.cr

Constructors

Macro Summary

Instance Method Summary

Constructor Detail

def self.new(properties : Hash(String, Any), *, _strict : Bool = false) forall Any #

Initializes a new instance.

Specified properties are assigned via setter methods. If a property lacks a setter the property is read-only and cannot be assigned. Non-nilable properties must be assigned.

To allow initialization of multiple models from a single collection of properties, initialize ignores specified properties that do not exist on the model. Specify _strict: true to change this behavior to raise an error instead.


[View source]
def self.new(*, _strict : Bool = false, **properties) #

Initializes a new instance.

Specified properties are assigned via setter methods. If a property lacks a setter the property is read-only and cannot be assigned. Non-nilable properties must be assigned.

To allow initialization of multiple models from a single collection of properties, initialize ignores specified properties that do not exist on the model. Specify _strict: true to change this behavior to raise an error instead.


[View source]

Macro Detail

macro belongs_to(name, primary_key = id, foreign_key = nil, class_name = nil, inverse_of = nil) #

Specifies a one-to-one association with another model.


[View source]
macro derived(decl, *, aliased_to) #

Specifies a property that is derived from another property.


[View source]
macro has_many(name, primary_key = id, foreign_key = nil, class_name = nil, inverse_of = nil) #

Specifies a one-to-many association with another model.


[View source]
macro has_one(name, primary_key = id, foreign_key = nil, class_name = nil, inverse_of = nil) #

Specifies a one-to-one association with another model.


[View source]
macro validates(property, &block) #

Adds a validation to a property on an instance.

validates xyz { "is blank" if xyz.blank? }

[View source]

Instance Method Detail

def ==(other) #

Returns true if all persistent properties are equal.


[View source]
def _run_validations #

[View source]
def _save_model(skip_validation = false) #

[View source]
def _serialize_graph(nodes, association = nil, index = nil, skip_associated = false) #

[View source]
def assign(properties : Hash(String, Any), *, _strict : Bool = false) forall Any #

Bulk assigns properties.

Specified properties are assigned via setter methods. If a property lacks a setter the property is read-only and cannot be assigned.

To allow assignment of multiple models from a single collection of properties, #assign ignores specified properties that do not exist on the model. Specify _strict: true to change this behavior to raise an error instead.


[View source]
def assign(*, _strict : Bool = false, **properties) #

Bulk assigns properties.

Specified properties are assigned via setter methods. If a property lacks a setter the property is read-only and cannot be assigned.

To allow assignment of multiple models from a single collection of properties, #assign ignores specified properties that do not exist on the model. Specify _strict: true to change this behavior to raise an error instead.


[View source]
def changed!(*properties : Symbol) #

[View source]
def changed? #

[View source]
def changed?(*properties : Symbol) #

[View source]
def clear! #

[View source]
def clear!(*properties : Symbol) #

[View source]
def destroy #

Destroys the instance.


[View source]
def destroyed? #

[View source]
def errors #

[View source]
def hash(hasher) #

Computes the hash for this instance.


[View source]
def inspect(io : IO) #

[View source]
def new_record? #

[View source]
def reload! #

Reloads the properties from the database.

Only reloads the persistent properties. Does not trigger any side effects. Does not ensure that the instance's state is otherwise valid.


[View source]
def save(skip_validation = false, skip_associated = false) #

Saves the instance.


[View source]
def serialize_graph(skip_associated = false) #

[View source]
def table_name #

Returns the table name.


[View source]
def to_h #

[View source]
def to_json(json : JSON::Builder) #

[View source]
def to_s(io : IO) #

[View source]
def update_property(property, value) #

Updates and persists property value.

This method is meant for simple state changes -- it does not validate model state or run before and after actions! Prefer assign/save methods.


[View source]
def valid?(skip_associated = false) #

Returns true if the instance is valid.


[View source]
def validate(skip_associated = false) #

Validates the instance and returns any errors.


[View source]