Class: LiveComponent::SafeDispatcher

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/live_component/safe_dispatcher.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSafeDispatcher

Returns a new instance of SafeDispatcher.



11
12
13
# File 'lib/live_component/safe_dispatcher.rb', line 11

def initialize
  @cache = ObjectSpace::WeakKeyMap.new
end

Class Method Details

.send_safelyObject



28
29
30
# File 'lib/live_component/safe_dispatcher.rb', line 28

def self.send_safely(...)
  instance.send_safely(...)
end

Instance Method Details

#send_safely(receiver, method_name, **kwargs) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/live_component/safe_dispatcher.rb', line 15

def send_safely(receiver, method_name, **kwargs)
  if receiver_defines_safe_method?(receiver, method_name)
    receiver.send(method_name, **kwargs)
  else
    raise(
      SafeDispatchError,
      "`#{method_name}' could not be called on an object of type '#{receiver.class.name}'. "\
        "Only public methods defined on classes that inherit from ViewComponent::Base "\
        "may be called."
    )
  end
end