class

PlaceOS::Model::Shortener

Inherits PlaceOS::Model::Timestamps < PlaceOS::Model::ModelBase < PlaceOS::Model::Associations < Neuroplastic < PgORM::Base < PgORM::Validators < PgORM::Table < PgORM::Persistence < PgORM::Associations < ActiveModel::Callbacks < ActiveModel::Validation < ActiveModel::Model < DB::Mappable < DB::Serializable < YAML::Serializable < JSON::Serializable < Reference < Object

Constants

CALLBACKS = { before_save: [] of Nil, after_save: [] of Nil, before_create: [do self.created_at = self.updated_at = Time.utc end, :short_id] of Nil, after_create: [] of Nil, before_update: [do self.updated_at = Time.utc end] of Nil, after_update: [] of Nil, before_destroy: [] of Nil, after_destroy: [] of Nil, }
CREATE_ID_ATTEMPTS = 10

#short_id derives the id from the creation second plus rand(63), i.e. only 63 possible ids per wall-clock second, so two same-second creates collide on the primary key with probability ~1/63. The compact format is deliberate (the id is the public short URL), so instead of widening it we retry the insert: each attempt re-runs the before_create callback, rolling a fresh random component, and a failed attempt is just a cheap single-row insert.

Log = ::Log.for(self)
PRIMARY_KEY_TYPES = {:id => (String | Nil)} of Nil => Nil

macro level key => type

PRIMARY_KEYS = [{:id}]
TIME_OFFSET = (Time.utc(2024, 3, 1)).to_unix

Constructors

from_json(string_or_io : String | IO, trusted : Bool = false) : self
from_json(string_or_io : String | IO, root : String, trusted : Bool = false) : self

Deserializes the given JSON in string_or_io into an instance of self, assuming the JSON consists of an JSON object with key root, and whose value is the value to deserialize. Will not deserialise from fields with mass_assign: false

class User < ActiveModel::Model
  attribute name : String
  attribute google_id : UUID, mass_assign: false
end

User.from_json(%({"main": {"name": "Jason", "google_id": "f6f70bfb-c882-446d-8758-7ce47db39620"}}), root: "main") # => #<User:0x103131b20 @name="Jason">
from_trusted_json(string_or_io : String | IO, root : String) : self
from_trusted_json(string_or_io : String | IO) : self

Serialize from a trusted JSON source

from_trusted_yaml(string_or_io : String | IO) : self

Serialize from a trusted YAML source

from_yaml(string_or_io : String | IO, trusted : Bool = false) : self
new(rs : DB::ResultSet)
new(created_at : Time | ActiveModel::Model::None = ::ActiveModel::Model::None.new, updated_at : Time | ActiveModel::Model::None = ::ActiveModel::Model::None.new, name : String | ActiveModel::Model::None = ::ActiveModel::Model::None.new, uri : String | ActiveModel::Model::None = ::ActiveModel::Model::None.new, description : String | ActiveModel::Model::None = ::ActiveModel::Model::None.new, user_id : String | ActiveModel::Model::None = ::ActiveModel::Model::None.new, user_email : PlaceOS::Model::Email | ActiveModel::Model::None = ::ActiveModel::Model::None.new, user_name : String | ActiveModel::Model::None = ::ActiveModel::Model::None.new, redirect_count : Int64 | ActiveModel::Model::None = ::ActiveModel::Model::None.new, enabled : Bool | ActiveModel::Model::None = ::ActiveModel::Model::None.new, valid_from : Time | Nil | ActiveModel::Model::None = ::ActiveModel::Model::None.new, valid_until : Time | Nil | ActiveModel::Model::None = ::ActiveModel::Model::None.new, authority_id : String | Nil | ActiveModel::Model::None = ::ActiveModel::Model::None.new, id : String | Nil | ActiveModel::Model::None = ::ActiveModel::Model::None.new)

Initialize PlaceOS::Model::Shortener from HTTP::Params.

Class methods

attributes

Returns all attribute keys.

by_authority_id(id)

Look up instances of this model dependent on the foreign key

Source
changes(id : String | Nil | Nil = nil) : ChangeFeed

Changefeed at row (if id passed) or whole table level. Returns a ChangeFeed instance which can be used to invoke async callbacks via on or use blocking Iterator via each method.

elastic
from_rs(__temp_2492 : DB::ResultSet)
on_error(err : Exception | IO::Error)
primary_key

Base class for all Engine models

Source
table_name

Instance methods

__authority
__authority=(__authority : Authority | Nil)
after_create

Base class for all Engine models

after_destroy

Base class for all Engine models

after_initialize(trusted : Bool)
after_save

Base class for all Engine models

after_update

Base class for all Engine models

apply_defaults

Generate code to apply default values

assign_attributes(created_at : Time | Missing = Missing, updated_at : Time | Missing = Missing, name : String | Missing = Missing, uri : String | Missing = Missing, description : String | Missing = Missing, user_id : String | Missing = Missing, user_email : PlaceOS::Model::Email | Missing = Missing, user_name : String | Missing = Missing, redirect_count : Int64 | Missing = Missing, enabled : Bool | Missing = Missing, valid_from : Time | Nil | Missing = Missing, valid_until : Time | Nil | Missing = Missing, authority_id : String | Nil | Missing = Missing, id : String | Nil | Missing = Missing)

Assign to multiple attributes.

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

Assign to multiple attributes via HTTP::Params.

assign_attributes(model : PlaceOS::Model::Shortener)

Assign to multiple attributes from a model object

assign_attributes_from_json(json, root : String)
assign_attributes_from_json(json)
assign_attributes_from_trusted_json(json, root : String)
assign_attributes_from_trusted_json(json)

Assign each field from JSON if field exists in JSON and has changed in model

assign_attributes_from_trusted_yaml(yaml)
assign_attributes_from_yaml(yaml)

Uses the YAML parser as JSON is valid YAML

attributes

Returns a Hash of all attribute values

attributes_tuple

Returns a NamedTuple of all attribute values.

authority

Retrieves the parent relationship

Source
authority!
Source
authority=(parent : Authority)

Sets the parent relationship

Source
authority_id

authority_id getter

authority_id=(value : String | Nil)

authority_id setter

authority_id_assigned?
authority_id_change

Returns a Tuple of the previous and the current value of an instance variable if it has changed

authority_id_changed?
authority_id_default

authority_id's default value

authority_id_present?
authority_id_removed?
authority_id_was
authority_id_will_change!

Include authority_id in the set of changed attributes, whether it has changed or not.

before_create

Base class for all Engine models

before_destroy

Base class for all Engine models

before_save

Base class for all Engine models

before_update

Base class for all Engine models

changed?

Check if any attributes have changed.

changed_attributes

Returns a Hash with all changed attributes.

changed_json(io : IO) : Nil

Serialize the set of changed attributes to JSON.

changed_json

Serialize the set of changed attributes to JSON.

changed_persist_attributes
changed_yaml(io : IO) : Nil

Serialize the set of changed attributes to YAML.

changed_yaml

Serialize the set of changed attributes to YAML.

clear_changes_information

Reset changes for all attributes.

created_at

created_at getter

created_at=(value : Time)

Setters created_at setter

created_at?

NOTE: use getter / getter! (not property / property!) so that the setter defined later in __create_initializer__ is the only setter on the field. Crystal's overload resolution silently fails to replace a property-defined setter with one of identical signature for generic types (e.g. Set(String)?, Array(String)?, String | Array(String)), although it does work for non-generic ones like String?. Defining the setter only once via __create_initializer__ ensures change tracking, sanitization, and custom setter blocks run uniformly across all types.

created_at_assigned?
created_at_change

Returns a Tuple of the previous and the current value of an instance variable if it has changed

created_at_changed?
created_at_default

created_at's default value

created_at_present?
created_at_removed?
created_at_was
created_at_will_change!

Include created_at in the set of changed attributes, whether it has changed or not.

description

description getter

description=(value : String)

description setter

description?

NOTE: use getter / getter! (not property / property!) so that the setter defined later in __create_initializer__ is the only setter on the field. Crystal's overload resolution silently fails to replace a property-defined setter with one of identical signature for generic types (e.g. Set(String)?, Array(String)?, String | Array(String)), although it does work for non-generic ones like String?. Defining the setter only once via __create_initializer__ ensures change tracking, sanitization, and custom setter blocks run uniformly across all types.

description_assigned?
description_change

Returns a Tuple of the previous and the current value of an instance variable if it has changed

description_changed?
description_default

description's default value

Source
description_present?
description_removed?
description_was
description_will_change!

Include description in the set of changed attributes, whether it has changed or not.

enabled

enabled getter

enabled=(value : Bool)

enabled setter

enabled?

NOTE: use getter / getter! (not property / property!) so that the setter defined later in __create_initializer__ is the only setter on the field. Crystal's overload resolution silently fails to replace a property-defined setter with one of identical signature for generic types (e.g. Set(String)?, Array(String)?, String | Array(String)), although it does work for non-generic ones like String?. Defining the setter only once via __create_initializer__ ensures change tracking, sanitization, and custom setter blocks run uniformly across all types.

enabled_assigned?
enabled_change

Returns a Tuple of the previous and the current value of an instance variable if it has changed

enabled_changed?
enabled_default

enabled's default value

Source
enabled_present?
enabled_removed?
enabled_was
enabled_will_change!

Include enabled in the set of changed attributes, whether it has changed or not.

id

id getter

id=(value : String | Nil)

id setter

id?
id_assigned?
id_change

Returns a Tuple of the previous and the current value of an instance variable if it has changed

id_changed?
id_default

id's default value

id_present?
id_removed?
id_was
id_will_change!

Include id in the set of changed attributes, whether it has changed or not.

increment_redirect_count
Source
invoke_props

Base class for all Engine models

name

name getter

name=(value : String)

name setter

name?

NOTE: use getter / getter! (not property / property!) so that the setter defined later in __create_initializer__ is the only setter on the field. Crystal's overload resolution silently fails to replace a property-defined setter with one of identical signature for generic types (e.g. Set(String)?, Array(String)?, String | Array(String)), although it does work for non-generic ones like String?. Defining the setter only once via __create_initializer__ ensures change tracking, sanitization, and custom setter blocks run uniformly across all types.

name_assigned?
name_change

Returns a Tuple of the previous and the current value of an instance variable if it has changed

name_changed?
name_default

name's default value

Source
name_present?
name_removed?
name_was
name_will_change!

Include name in the set of changed attributes, whether it has changed or not.

perform_redirect?
Source
persistent_attributes

Returns a Hash of all attributes that can be persisted.

primary_key

Base class for all Engine models

Source
primary_key_hash
redirect_count

redirect_count getter

redirect_count=(value : Int64)

redirect_count setter

redirect_count?

NOTE: use getter / getter! (not property / property!) so that the setter defined later in __create_initializer__ is the only setter on the field. Crystal's overload resolution silently fails to replace a property-defined setter with one of identical signature for generic types (e.g. Set(String)?, Array(String)?, String | Array(String)), although it does work for non-generic ones like String?. Defining the setter only once via __create_initializer__ ensures change tracking, sanitization, and custom setter blocks run uniformly across all types.

redirect_count_assigned?
redirect_count_change

Returns a Tuple of the previous and the current value of an instance variable if it has changed

redirect_count_changed?
redirect_count_default

redirect_count's default value

Source
redirect_count_present?
redirect_count_removed?
redirect_count_was
redirect_count_will_change!

Include redirect_count in the set of changed attributes, whether it has changed or not.

reset_associations
Source
restore_attributes

Reset each attribute to their previous values and clears all changes.

save!

Saves the record to the database, raising an exception on failure.

For new records, performs an INSERT. For existing records, performs an UPDATE. Only updates changed attributes (dirty tracking).

Example

user = User.new(name: "John")
user.save! # => #<User id: 1, name: "John">

user.name = "Jane"
user.save! # Only updates 'name' column

Raises

  • PgORM::Error::RecordNotSaved if record was destroyed
  • PgORM::Error::RecordInvalid if validation fails
  • PgORM::Error::RecordNotSaved if database operation fails
Source
table_name
updated_at

updated_at getter

updated_at=(value : Time)

updated_at setter

updated_at?

NOTE: use getter / getter! (not property / property!) so that the setter defined later in __create_initializer__ is the only setter on the field. Crystal's overload resolution silently fails to replace a property-defined setter with one of identical signature for generic types (e.g. Set(String)?, Array(String)?, String | Array(String)), although it does work for non-generic ones like String?. Defining the setter only once via __create_initializer__ ensures change tracking, sanitization, and custom setter blocks run uniformly across all types.

updated_at_assigned?
updated_at_change

Returns a Tuple of the previous and the current value of an instance variable if it has changed

updated_at_changed?
updated_at_default

updated_at's default value

updated_at_present?
updated_at_removed?
updated_at_was
updated_at_will_change!

Include updated_at in the set of changed attributes, whether it has changed or not.

uri

uri getter

uri=(value : String)

uri setter

uri?

NOTE: use getter / getter! (not property / property!) so that the setter defined later in __create_initializer__ is the only setter on the field. Crystal's overload resolution silently fails to replace a property-defined setter with one of identical signature for generic types (e.g. Set(String)?, Array(String)?, String | Array(String)), although it does work for non-generic ones like String?. Defining the setter only once via __create_initializer__ ensures change tracking, sanitization, and custom setter blocks run uniformly across all types.

uri_assigned?
uri_change

Returns a Tuple of the previous and the current value of an instance variable if it has changed

uri_changed?
uri_default

uri's default value

Source
uri_present?
uri_removed?
uri_was
uri_will_change!

Include uri in the set of changed attributes, whether it has changed or not.

user=(user)
Source
user_email

user_email getter

user_email=(value : PlaceOS::Model::Email)

user_email setter

user_email?

NOTE: use getter / getter! (not property / property!) so that the setter defined later in __create_initializer__ is the only setter on the field. Crystal's overload resolution silently fails to replace a property-defined setter with one of identical signature for generic types (e.g. Set(String)?, Array(String)?, String | Array(String)), although it does work for non-generic ones like String?. Defining the setter only once via __create_initializer__ ensures change tracking, sanitization, and custom setter blocks run uniformly across all types.

user_email_assigned?
user_email_change

Returns a Tuple of the previous and the current value of an instance variable if it has changed

user_email_changed?
user_email_default

user_email's default value

Source
user_email_present?
user_email_removed?
user_email_was
user_email_will_change!

Include user_email in the set of changed attributes, whether it has changed or not.

user_id

user_id getter

user_id=(value : String)

user_id setter

user_id?

NOTE: use getter / getter! (not property / property!) so that the setter defined later in __create_initializer__ is the only setter on the field. Crystal's overload resolution silently fails to replace a property-defined setter with one of identical signature for generic types (e.g. Set(String)?, Array(String)?, String | Array(String)), although it does work for non-generic ones like String?. Defining the setter only once via __create_initializer__ ensures change tracking, sanitization, and custom setter blocks run uniformly across all types.

user_id_assigned?
user_id_change

Returns a Tuple of the previous and the current value of an instance variable if it has changed

user_id_changed?
user_id_default

user_id's default value

Source
user_id_present?
user_id_removed?
user_id_was
user_id_will_change!

Include user_id in the set of changed attributes, whether it has changed or not.

user_name

user_name getter

user_name=(value : String)

user_name setter

user_name?

NOTE: use getter / getter! (not property / property!) so that the setter defined later in __create_initializer__ is the only setter on the field. Crystal's overload resolution silently fails to replace a property-defined setter with one of identical signature for generic types (e.g. Set(String)?, Array(String)?, String | Array(String)), although it does work for non-generic ones like String?. Defining the setter only once via __create_initializer__ ensures change tracking, sanitization, and custom setter blocks run uniformly across all types.

user_name_assigned?
user_name_change

Returns a Tuple of the previous and the current value of an instance variable if it has changed

user_name_changed?
user_name_default

user_name's default value

Source
user_name_present?
user_name_removed?
user_name_was
user_name_will_change!

Include user_name in the set of changed attributes, whether it has changed or not.

valid_from

valid_from getter

valid_from=(value : Time | Nil)

valid_from setter

valid_from_assigned?
valid_from_change

Returns a Tuple of the previous and the current value of an instance variable if it has changed

valid_from_changed?
valid_from_default

valid_from's default value

Source
valid_from_present?
valid_from_removed?
valid_from_was
valid_from_will_change!

Include valid_from in the set of changed attributes, whether it has changed or not.

valid_until

valid_until getter

valid_until=(value : Time | Nil)

valid_until setter

valid_until_assigned?
valid_until_change

Returns a Tuple of the previous and the current value of an instance variable if it has changed

valid_until_changed?
valid_until_default

valid_until's default value

Source
valid_until_present?
valid_until_removed?
valid_until_was
valid_until_will_change!

Include valid_until in the set of changed attributes, whether it has changed or not.

validate_nilability

Validate that all non-nillable fields have values.

Nested types