abstract class Rome::Model

Included Modules

Extended Modules

Defined in:

model.cr
persistence.cr

Constructors

Class Method Summary

Instance Method Summary

Macro Summary

Instance methods inherited from module Rome::Serialization

attributes=(pull : JSON::PullParser) : Nil attributes=, to_json(io : IO, indent = nil) : Nil
to_json(builder : JSON::Builder) : JSON::Builder
to_json(indent = nil) : String
to_json

Constructor methods inherited from module Rome::Serialization

new(pull : JSON::PullParser) : self new

Constructor Detail

def self.create(**args) : self #

Creates a record in database with the specified attributes. For example:

user = User.create(name: "julien", group_id: 2)
# => User(id: 1, name: "julien", group_id: 2)

Automatically fills the created_at and updated_at columns if they exist and are nil.


[View source]

Class Method Detail

def self.delete(*ids) : Nil #

Deletes one or many records identified by ids from the database.

User.delete(1)
User.delete(1, 2, 3)

[View source]
def self.update(id, args) : Nil #

Updates one or many records identified by id in the database.

User.update(1, { name: julien })
User.update([1, 2, 3], { group_id: 2 })

[View source]
def self.update(id, **args) : Nil #

Updates one or many records identified by id in the database.

User.update(1, { name: julien })
User.update([1, 2, 3], { group_id: 2 })

[View source]

Instance Method Detail

def ==(other : self) : Bool #
Description copied from class Reference

Returns true if this reference is the same as other. Invokes same?.


[View source]
def ==(other) : Bool #
Description copied from class Reference

Returns false (other can only be a Value here).


[View source]
abstract def [](attr_name : Symbol) : Value #

Generic accessor for an attribute. Only valid for known columns. Raises a Rome::MissingAttribute exception when the attribute is undefined (e.g. not loaded from the database).


[View source]
abstract def [](attr_name : String) : Value #

Generic accessor for an attribute or extraneous attribute. Raises a Rome::MissingAttribute exception when the attribute is undefined (e.g. not loaded from the database).


[View source]
abstract def []=(attr_name : Symbol, value : Value) : Value #

Generic setter for an attribute. Only valid for known columns.


[View source]
abstract def []?(attr_name : Symbol) : Value | Nil #

Generic accessor for an attribute. Returns nil when the attribute is undefined (e.g. not loaded from the database).


[View source]
abstract def []?(attr_name : String) : Value | Nil #

Generic accessor for an attribute or extraneous attribute. Returns nil when the attribute is undefined (e.g. not loaded from the database).


[View source]
abstract def attributes=(attrs : NamedTuple) : Nil #

Sets many attributes at once. For example:

user = User.find(1)
user.attributes = {
  group_id: 2,
  name: "julien",
}

user.group_id  # => 2
user.name      # => "julien"

[View source]
def changed? : Bool #

Returns true if any attribute has changed since this model was initialized or last saved.


[View source]
def changes : Hash(Symbol, Tuple(Rome::Value, Rome::Value)) #

Returns the list of changed attributes.


[View source]
def changes_applied : Nil #

Tell that all changes have been applied to the database, thus clearing all change information.


[View source]
def clear_changes_information : Nil #

Clears all dirty attribute information.


[View source]
def delete : Nil #

Deletes the record from the database. Marks the record as deleted.


[View source]
def deleted? : Bool #

Returns true if the record has been deleted from the database.


[View source]
def destroy : Nil #

Deletes the record and dependent associations from the database. Marks the record as deleted.


[View source]
abstract def id #

Always returns this record's primary key value, even when the primary key isn't named #id.


[View source]
abstract def id? #

Same as #id but may return nil when the record hasn't been saved instead of raising.


[View source]
def new_record? : Bool #

Returns true if the record hasn't been saved to the database, yet.


[View source]
def persisted? : Bool #

Returns true is the record is known to exist in the database (has either been loaded or saved and not deleted).


[View source]
def reload : self #

Reloads a record from the database. This will reset all changed attributes and all change information.


[View source]
abstract def restore_attributes : Nil #

Restores all dirty attributes to their pristine value. Eventually clears change information.


[View source]
def save : Nil #

Persists the record into the database. Either creates a new row or updates an existing row.

user = User.new(name: "julien", group_id: 2)
user.save # => INSERT

user.name = "alice"
user.save # => UPDATE

[View source]
abstract def to_h : Hash #

Exports this record as a Hash.


[View source]
def update(**attributes) : self #

Updates a record into the database, optionally setting the specified attributes if present.

Automatically updates the updated_at column if it exists.

Raises a ReadOnlyRecord exception if the record has been deleted.


[View source]

Macro Detail

macro columns(properties, strict = false) #

Declares the model schema. For example:

class User < Rome::Model
  columns(
    id:       {type: Int32, primary_key: true},
    group_id: {type: UUID, null: true},
    name:     {type: String},
  )
end

Available options:

  • type —the column type (required);
  • primary_key —whether the column is the table primary key (default: false);
  • null —whether the column accepts NULL values (default: false).
  • converter —specify a converter type that must implement .from_rs(DB::ResultSet) and should implement .from_json(JSON::PullParser).

[View source]
macro columns(**properties) #

[View source]