Class: Dinie::Internal::Model

Inherits:
Object
  • Object
show all
Defined in:
sig/dinie/runtime/model.rbs,
lib/dinie/runtime/model.rb

Overview

Base class for every generated model (architecture §5.1, RB3/RB18).

Gives POROs value-semantics without Data.define, so the floor stays at Ruby 3.1 (Block 0). A subclass declares its fields with Model.attribute in alphabetical order (R-ORDER); the source-emission order is the single source of truth for to_h and inspect, so the runtime never sorts at call time. Instances are frozen after construction, mirroring the read-only character of the sdk-js models.

Examples:

class Customer < Dinie::Internal::Model
  attribute :cpf, :id, :name, :status
end

c = Customer.new(cpf: "***", id: "cust_1", name: "Ana", status: "active")
c.id                       # => "cust_1"
c.to_h                     # => {cpf: "***", id: "cust_1", name: "Ana", status: "active"}
c == Customer.new(...)     # value-equality by attributes
case c; in {status:}; end  # pattern matching via deconstruct_keys

Constant Summary collapse

REDACTED_ATTRIBUTES =

PII attribute names redacted by #inspect. Kept in sync with the logger's body redaction list (architecture §9 / story 005, which owns the canonical set).

%i[
  access_token account client_secret cnpj cpf cvv password phone secret
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attrs) ⇒ Model

Build a frozen instance from keyword arguments — one per declared attribute.

Every declared attribute is required (hydration always passes a value, nil included), and unknown keywords are rejected, so a contract drift surfaces loudly instead of silently dropping data.

Parameters:

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

    one value per declared attribute

Raises:

  • (ArgumentError) —

    when a declared attribute is missing or an unknown one is given



28
# File 'sig/dinie/runtime/model.rbs', line 28

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

Class Method Details

.attribute(*names) ⇒ void

This method returns an undefined value.

Declare one or more attributes: defines an attr_reader for each and appends it to attributes in declaration order. Idempotent per name.

Parameters:

  • names (Array<Symbol, String>) —

    attribute names, alphabetical (R-ORDER)



22
# File 'sig/dinie/runtime/model.rbs', line 22

def self.attribute: (*Symbol | String names) -> void

.attributes ⇒ Array<Symbol>

The declared attribute names, in declaration (R-ORDER) order. Subclasses inherit and may extend their parent's list.

Returns:

  • (Array<Symbol>)


19
# File 'sig/dinie/runtime/model.rbs', line 19

def self.attributes: () -> Array[Symbol]

.inherited(subclass) ⇒ void

This method returns an undefined value.

Carry the parent's attribute list into each subclass.

Parameters:

  • subclass (Class)


25
# File 'sig/dinie/runtime/model.rbs', line 25

def self.inherited: (Class subclass) -> void

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Value-equality: same class and same attribute values.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


38
# File 'sig/dinie/runtime/model.rbs', line 38

def ==: (untyped other) -> bool

#deconstruct_keys(_keys) ⇒ Hash{Symbol => Object}

Enable pattern matching (case model; in {status: "active"}).

Parameters:

  • _keys (Array<Symbol>, nil) —

    requested keys (ignored — the full hash matches)

Returns:

  • (Hash{Symbol => Object})


35
# File 'sig/dinie/runtime/model.rbs', line 35

def deconstruct_keys: (Array[Symbol]? keys) -> Hash[Symbol, untyped]

#hash ⇒ Integer

Returns consistent with #eql? so instances work as Hash keys.

Returns:

  • (Integer) —

    consistent with #eql? so instances work as Hash keys



132
# File 'lib/dinie/runtime/model.rb', line 132

def hash: () -> Integer

#inspect ⇒ String

Like #to_h, but with PII redacted — safe for logs and the console.

Returns:

  • (String)


44
# File 'sig/dinie/runtime/model.rbs', line 44

def inspect: () -> String

#to_h ⇒ Hash{Symbol => Object} Also known as: to_hash

Returns attributes in declaration (R-ORDER) order — deterministic.

Returns:

  • (Hash{Symbol => Object}) —

    attributes in declaration (R-ORDER) order — deterministic



31
# File 'sig/dinie/runtime/model.rbs', line 31

def to_h: () -> Hash[Symbol, untyped]