Class: JetstreamBridge::Models::Event::PayloadAccessor

Inherits:
Object
  • Object
show all
Defined in:
lib/jetstream_bridge/models/event.rb

Overview

Wraps a Hash payload to allow method-style access to its keys.

Examples:

accessor = PayloadAccessor.new("user_id" => 42)
accessor.user_id  #=> 42
accessor["user_id"]  #=> 42
accessor.to_h  #=> {"user_id" => 42}

Instance Method Summary collapse

Constructor Details

#initialize(payload) ⇒ PayloadAccessor

Returns a new instance of PayloadAccessor.

Parameters:

  • payload (Hash)

    Raw payload hash



65
66
67
# File 'lib/jetstream_bridge/models/event.rb', line 65

def initialize(payload)
  @payload = payload.is_a?(Hash) ? payload.transform_keys(&:to_s) : {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



69
70
71
72
73
# File 'lib/jetstream_bridge/models/event.rb', line 69

def method_missing(method_name, *args)
  return @payload[method_name.to_s] if args.empty? && @payload.key?(method_name.to_s)

  super
end

Instance Method Details

#[](key) ⇒ Object



79
80
81
# File 'lib/jetstream_bridge/models/event.rb', line 79

def [](key)
  @payload[key.to_s]
end

#dig(*keys) ⇒ Object



83
84
85
# File 'lib/jetstream_bridge/models/event.rb', line 83

def dig(*keys)
  @payload.dig(*keys.map(&:to_s))
end

#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/jetstream_bridge/models/event.rb', line 75

def respond_to_missing?(method_name, _include_private = false)
  @payload.key?(method_name.to_s) || super
end

#to_hObject Also known as: to_hash



87
88
89
# File 'lib/jetstream_bridge/models/event.rb', line 87

def to_h
  @payload
end