Class: Mailkube::Events::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/mailkube/events/node.rb,
sig/mailkube/events/node.rbs

Overview

Event models are the one place this gem does not use Data.define.

The contract inverts the response-model rule for webhooks: unknown fields are preserved, not dropped, at every depth. A Data has fixed members and drops them, so the decoded hash is the state instead, and each subclass declares one-line readers over it.

Those readers are real defs, so they are declared here as defattr_reader in these signatures is reserved for the readers Data.define actually generates (see sig/mailkube/types.rbs).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Node

Returns a new instance of Node.

Parameters:

  • raw (Hash{String => Object})

    the decoded JSON object for this block.

  • (Hash[String, untyped])


27
28
29
30
# File 'lib/mailkube/events/node.rb', line 27

def initialize(raw)
  @raw = raw
  freeze
end

Instance Attribute Details

#rawHash{String => Object} (readonly)

Returns the verbatim decoded object, unknown fields included.

Returns:

  • (Hash{String => Object})

    the verbatim decoded object, unknown fields included.



24
25
26
# File 'lib/mailkube/events/node.rb', line 24

def raw
  @raw
end

Instance Method Details

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

Returns true when both are the same class over the same payload.

Parameters:

  • other (Object)

    the object to compare with.

  • (Object)

Returns:

  • (Boolean)

    true when both are the same class over the same payload.



46
# File 'lib/mailkube/events/node.rb', line 46

def ==(other) = other.instance_of?(self.class) && other.raw == @raw

#[](key) ⇒ Object?

Read any field by its wire name, whether or not this release models it.

This is the forward-compatibility escape hatch the contract asks for: a field the platform adds after this gem ships is reachable here on the day it appears.

Parameters:

  • key (String)

    the wire field name.

  • (String)

Returns:

  • (Object, nil)

    the value, or nil when the server did not send it.



39
# File 'lib/mailkube/events/node.rb', line 39

def [](key) = @raw[key]

#block(type, key) ⇒ Object

Deliberately untyped rather than a bounded generic: Ruby has no generics, and every caller is a one-line reader that already declares its own concrete return type — which is where the type belongs.

Parameters:

  • (Object)
  • (String)

Returns:

  • (Object)


59
# File 'lib/mailkube/events/node.rb', line 59

def block(type, key) = type.new(self[key] || {})

#hashInteger

Returns a hash consistent with #==.

Returns:

  • (Integer)

    a hash consistent with #==.



50
# File 'lib/mailkube/events/node.rb', line 50

def hash = [self.class, @raw].hash

#to_hHash{String => Object}

Returns the verbatim decoded object, for logging or forwarding.

Returns:

  • (Hash{String => Object})

    the verbatim decoded object, for logging or forwarding.



42
# File 'lib/mailkube/events/node.rb', line 42

def to_h = @raw