Class: Sqlx::Enum
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
-
#initialize(pairs) ⇒ Enum
constructor
validates the whole mapping at construction so a sound declaration is the only kind that exists.
-
#label(value) ⇒ String?
the stored value → label reader, the read half of the declaration.
-
#label?(raw, label) ⇒ Boolean
the predicate half: answers whether a stored slot holds the given label's value, for hand-written enum predicates.
-
#value(text) ⇒ Integer
the label → stored value write half: the Integer an INSERT or UPDATE stores.
-
#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.
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.
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.
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.
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.
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.
115 |
# File 'runtime/sqlx/enum.rb', line 115
def writer: (table: String, column: String) -> EnumWriter
|