Module: Rage::Events::Subscriber::ClassMethods

Defined in:
lib/rage/events/subscriber.rb

Instance Method Summary collapse

Instance Method Details

#deferred?Boolean

Check if the subscriber is executed in the background.

Returns:

  • (Boolean)

    true if the subscriber is deferred, false otherwise



157
158
159
# File 'lib/rage/events/subscriber.rb', line 157

def deferred?
  @__is_deferred
end

#rescue_from(*klasses, with: nil) {|exception| ... } ⇒ Object

Note:

If you do not re-raise exceptions in deferred subscribers, the subscriber will be marked as successful and Rage will not attempt to retry it.

Define exception handlers for the subscriber.

Parameters:

  • klasses (Class, Array<Class>)

    one or more exception classes to handle

  • with (Symbol, String) (defaults to: nil)

    the method name to call when an exception is raised

Yields:

  • (exception)

    optional block to handle the exception



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/rage/events/subscriber.rb', line 135

def rescue_from(*klasses, with: nil, &block)
  unless with
    if block_given?
      with = Rage::Internal.define_dynamic_method(self, block)
    else
      raise ArgumentError, "No handler provided. Pass the `with` keyword argument or provide a block."
    end
  end

  if @__rescue_handlers.nil?
    @__rescue_handlers = []
  elsif @__rescue_handlers.frozen?
    @__rescue_handlers = @__rescue_handlers.dup
  end

  @__rescue_handlers.unshift([klasses, with])

  rebuild_exception_handler!
end

#subscribe_to(*event_classes, deferred: false) ⇒ Object

Subscribe the class to one or more events.

Parameters:

  • event_classes (Class, Array<Class>)

    one or more event classes to subscribe to

  • deferred (Boolean) (defaults to: false)

    whether to process events asynchronously



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/rage/events/subscriber.rb', line 114

def subscribe_to(*event_classes, deferred: false)
  @__event_classes = (@__event_classes || []) | event_classes
  @__is_deferred = !!deferred
  @__log_context = { subscriber: name }.freeze

  @__event_classes.each do |event_class|
    Rage::Events.__register_subscriber(event_class, self)
  end

  if @__is_deferred
    include Rage::Deferred::Task
    alias_method :perform, :__call
  end
end