Class: RailsAsyncMethods::AsyncWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_async_methods/wrapper/async_wrapper.rb

Defined Under Namespace

Classes: NotPersistedError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(receiver, options = {}) ⇒ AsyncWrapper

Returns a new instance of AsyncWrapper.



8
9
10
11
# File 'lib/rails_async_methods/wrapper/async_wrapper.rb', line 8

def initialize(receiver, options = {})
  @receiver = receiver
  @options = RailsAsyncMethods::ActiveJobOptionsParser.new(options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, **kwargs) ⇒ Object

Raises:



13
14
15
16
17
18
19
20
21
22
# File 'lib/rails_async_methods/wrapper/async_wrapper.rb', line 13

def method_missing(method, *args, **kwargs)
  raise NotPersistedError, 'Instance must be persisted to run asynchronously' unless receiver.persisted?
  raise TypeError, 'Cannot pass a block to an asynchronous method' if block_given?

  if @receiver.methods.include? method
    @options.job.set(**@options.to_h).perform_later(@receiver, method, *args, **kwargs)
  else
    super
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/rails_async_methods/wrapper/async_wrapper.rb', line 6

def options
  @options
end

#receiverObject (readonly)

Returns the value of attribute receiver.



6
7
8
# File 'lib/rails_async_methods/wrapper/async_wrapper.rb', line 6

def receiver
  @receiver
end

Instance Method Details

#respond_to_missing?(method) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/rails_async_methods/wrapper/async_wrapper.rb', line 24

def respond_to_missing?(method)
  @receiver.methods.include?(method) || super
end