Class: Sqlx::Enum

Inherits:
Object show all
Defined in:
runtime/sqlx/enum.rb,
runtime/sqlx/enum.rbs

Overview

The validated declaration behind Sqlx::Base.enum — a plain value object: no method generation (spinel has no metaprogramming for it), so predicates stay literal model source and writers arrive as a bound object. It owns the mapping's single source, the loud edges around it, and the factory for its column-bound writer.

Instance Method Summary collapse

Constructor Details

#initialize(pairs) ⇒ Enum

validates the whole mapping at construction so a sound declaration is the only kind that exists. This body runs behind Sqlx::Base.enum, the entry models use for their constants.

Parameters:

  • pairs (Hash{Symbol => Integer}) —

    label => storage value, in declaration order — the same contract the factory documents

Raises:

  • (ArgumentError) —

    on an empty declaration, a non-Symbol key, a non-Integer value, or a duplicated value



17
# File 'runtime/sqlx/enum.rb', line 17

def initialize: (Hash[Symbol, Integer] pairs) -> void

Instance Method Details

#label(value) ⇒ String?

the stored value → label reader, the read half of the declaration. Answers nil for a value no label names: an unrecognized raw is a data-shape question and does not raise.

Examples:

STATUS.label(@status_raw)
# => "trialing" (nil for an unknown raw)

Parameters:

  • value (Integer) —

    the slot read from the column

Returns:

  • (String?) —

    the label, or nil when no entry claims the value



46
# File 'runtime/sqlx/enum.rb', line 46

def label: (Integer value) -> String?

#label?(raw, label) ⇒ Boolean

the predicate half: answers whether a stored slot holds the given label's value, for hand-written enum predicates. The label half is strict like value: a name the declaration does not carry raises, so a renamed or removed entry surfaces at the first predicate call instead of the predicate answering false forever. An unknown raw answers false — label stays the reader for data-shape questions.

Examples:

STATUS.label?(@status_raw, :trialing)
# => true / false (raises for a label the declaration dropped)

Parameters:

  • raw (Integer) —

    the slot read from the column

  • label (Symbol, String) —

    the declaration label to compare — a stored-value spelling reads the same slot, like value

Returns:

  • (Boolean) —

    true when the slot holds the label's value, false for a different or unknown raw

Raises:

  • (ArgumentError) —

    the label is not in the declaration



71
# File 'runtime/sqlx/enum.rb', line 71

def label?: (Integer raw, untyped label) -> bool

#value(text) ⇒ Integer

the label → stored value write half: the Integer an INSERT or UPDATE stores. An unknown name raises — the enum type's contract, not a to_i fallback that would silently store 0 — and the standard bang writer reads its slot through this, so a redeclared or renumbered enum cannot leave a writer pinned to a stale literal.

Examples:

STATUS.value(:trialing)
# => 0

Parameters:

  • text (Symbol, String) —

    the label, or its stored-value spelling — both read the same slot

Returns:

  • (Integer) —

    the stored value for the label

Raises:

  • (ArgumentError) —

    the name is not in the declaration



88
# File 'runtime/sqlx/enum.rb', line 88

def value: (untyped text) -> Integer

#writer(table:, column:) ⇒ EnumWriter

this declaration's writer for one column: hands the mapping it already owns to a bound writer, so the call site reads STATUS.writer(...) from the same receiver the readers use instead of passing the declaration back in. Core owns the UPDATE shape because it never varies by model (the same SET <column> = ?, updated_at = ? WHERE id = ? every time) — the hand-rolled per-model helper is where the SQL drifts and where each model invents its own helper name.

Examples:

def trialing! = STATUS.writer(table: "widgets", column: "status").write!(@id, :trialing)

Parameters:

  • table (String) —

    the table name — code, not input, and not validated: an unknown table surfaces as a loud SQL error from the driver, the rule where_clause applies to its keys

  • column (String) —

    the column name — the same code-not-input rule as table

Returns:

  • (EnumWriter) —

    a writer bound to the column, carrying this declaration for the label -> stored-value conversion



115
# File 'runtime/sqlx/enum.rb', line 115

def writer: (table: String, column: String) -> EnumWriter