Module: Sqlx::Row

Included in:
Base
Defined in:
runtime/sqlx/row.rb,
runtime/sqlx/row.rbs

Overview

The row-object layer — the instance side of hydration: the generic constructor and the read seam every literal accessor routes through. Included into Base so models inherit the instance methods; a hand-written model initialize still overrides.

Instance Method Summary collapse

  • #initialize(**attrs) ⇒ Object

    the generic row constructor behind the hash-backed accessor path: a model whose readers come from its literal accessor loop needs no hand-written initialize.

  • #read_attribute(name) ⇒ Object

    the read seam of the hash-backed accessor adapter: the model's literal accessor loop routes every column read through this one method, so the storage behind the readers is core-owned and free to change without touching models.

Instance Method Details

#initialize(**attrs) ⇒ Object

the generic row constructor behind the hash-backed accessor path: a model whose readers come from its literal accessor loop needs no hand-written initialize. Keys are checked against only_columns at this entry so a typo'd keyword raises here instead of reading as a silent nil later; missing keys are allowed — an unselected column reads as nil. A model that hand-writes initialize simply overrides this.

Examples:

hydration — the class-side factory hands its columns over

def self.from_row(row)
  new(id: row["id"], name: row["name"])
end

Parameters:

  • attrs (Hash{Symbol => Object}) —

    the row's column values, keyed by column name

Raises:

  • (ArgumentError) —

    when a key is not one of the model's declared columns

  • (NotImplementedError) —

    when the model declares no only_columns (an empty call never reaches the check)



21
# File 'runtime/sqlx/row.rb', line 21

def initialize: (**untyped attrs) -> void

#read_attribute(name) ⇒ Object

the read seam of the hash-backed accessor adapter: the model's literal accessor loop routes every column read through this one method, so the storage behind the readers is core-owned and free to change without touching models. A missing key answers nil — an unselected column and a NULL value read the same way.

Examples:

the accessor loop a model writes against this seam

%w[id email].each { |n| define_method(n) { read_attribute(n) } }

Parameters:

  • name (Symbol, String) —

    the column name (String accepted — the accessor loop iterates member names as written)

Returns:

  • (Object) —

    the stored value, or nil when the key is absent



44
# File 'runtime/sqlx/row.rb', line 44

def read_attribute: (Symbol | String name) -> untyped