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

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

Overview

Payload accessor with method-style access

Instance Method Summary collapse

Constructor Details

#initialize(payload) ⇒ PayloadAccessor

Returns a new instance of PayloadAccessor.



42
43
44
# File 'lib/jetstream_bridge/models/event.rb', line 42

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



46
47
48
49
50
# File 'lib/jetstream_bridge/models/event.rb', line 46

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



56
57
58
# File 'lib/jetstream_bridge/models/event.rb', line 56

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

#dig(*keys) ⇒ Object



60
61
62
# File 'lib/jetstream_bridge/models/event.rb', line 60

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

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

Returns:

  • (Boolean)


52
53
54
# File 'lib/jetstream_bridge/models/event.rb', line 52

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

#to_hObject Also known as: to_hash



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

def to_h
  @payload
end