rails_async_methods 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 672c0a204c6d32b0c4b33f9da9b1c9964afc45d9626128ddccf52f77a12bd8c1
4
- data.tar.gz: a4cf18a167be4b327aaab34b49875280444fb7f6f52743a85aa1564f2cba2f33
3
+ metadata.gz: 70a7606803b98d462e11d6de8ee772cf66fd9aa7fe2355a4fc4a315a3b9f6372
4
+ data.tar.gz: 0ef6b6034c8096c385aac47a0f1cfe7d65e30480d55dbb22f9035de0ddf6091c
5
5
  SHA512:
6
- metadata.gz: 650de1333dc34b7d8307508d12f4d49eda5ab5e249342727500b7431a4fd81d6ae66cd8d9ad3d5a28a06c2000d24dff7dd7ed625cad55bdb8dfb755a6c8cbd61
7
- data.tar.gz: 1e17bb9130bbfef087ebc8cfe86051284c44a12c0234287373dd63536157b47a2bbc31f320a129827c38a0950266b792ebe77bb746360466e8884ea92fb3913e
6
+ metadata.gz: d6f9cd94c505732b33480f69ebac5d8700a87227b3dd3f803cf39699f41f98d4397e6229286a6e3da4b08d04636eba028d91c0da31303bfcfe3aa559a642dd90
7
+ data.tar.gz: be4580294350b8bc8d2b67f7946a71f7e4f0b16fd5e08ebc472e5f34e4202a0ada9b3133b886a59752fda6075e85953e7e9bc64a44d5df8c68cc55cc10d9b98b
data/README.md CHANGED
@@ -1,14 +1,15 @@
1
1
  # RailsAsyncMethods
2
- Rails Async Methods is an opinionated gem meant to remove boilerplate while creating asynchronous rails jobs. It provides a declaritive interface for converting any model method into an asychronous method by provided an abstracted wrapper around rails ActiveJob API.
2
+ Rails Async Methods is an opinionated gem meant to remove boilerplate while creating Rails ActiveJobs. It provides a declarative interface for converting any model method into an asychronous method by providing an abstracted wrapper around rails ActiveJob API.
3
3
 
4
4
  ## Usage
5
+ ### Model Declaration
5
6
  ```ruby
6
7
  class User
7
8
  def example_method
8
9
  # logic...
9
10
  end
11
+ async :example_method
10
12
  end
11
- async :example_method
12
13
  ```
13
14
  This will give you access to ```user_instance.async_example_method```, which when called will use ActiveJob's API to create an ActiveJob with your backend of choice and call the example_method when the job is ran.
14
15
 
@@ -19,8 +20,30 @@ def example_method_with_args(a:, b:)
19
20
  end
20
21
  async :example_method_with_args
21
22
  ```
22
- the ```async_example_method_with_args``` method will have a signature that matches the original method. This makes testing and debugging during development faster, as both sync and async method calls will fail when called instead of silently failing as an active job.
23
+ the ```async_example_method_with_args``` method will have a signature that matches the original method. This makes testing and debugging during development faster, as both sync and async method calls will fail when called with improper arguments instead of silently failing as an active job.
23
24
 
25
+ ### Object Wrapper
26
+ Alternatively, if you don't to declare methods as async in your model, you can utilize the async object wrapper made available globally to all objects.
27
+ ```ruby
28
+ class ResourceController < Application Controller
29
+ def create
30
+ async(@resource).any_method_available_to_resource
31
+ end
32
+ end
33
+ ```
34
+ If you don't want to wrap objects in a utility method, you can also use the expressive type converter.
35
+ ### Type Converter
36
+ ```ruby
37
+ class ResourceService
38
+ def run
39
+ @resource.to_active_job.any_method_available_to_resource
40
+ end
41
+ end
42
+ ```
43
+
44
+ Note: Under the hood, the object wrapper just makes a call ```to_active_job```, so these objects work the same.
45
+
46
+ It is recommended that you default to the model declaration syntax.
24
47
  ### Options
25
48
 
26
49
  You can pass the following options to your async declaration.
@@ -34,7 +57,8 @@ user_instance.asynchronous_example_method
34
57
 
35
58
  - job: use a custom job other than the generated ```RailsAsyncMethods::AbstractJob``` - see section on Custom Jobs below i.e.
36
59
  ```ruby
37
- async :example_method, job: CustomExampleMethodJob
60
+ async :example_method, job: CustomExampleMethodJob # defined in model
61
+ async(@resource, job: CustomExampleMethodJob).example_method #callable anywhere
38
62
  ```
39
63
 
40
64
  - ActiveJob configurations
@@ -43,10 +67,16 @@ async :example_method, job: CustomExampleMethodJob
43
67
  - wait: give an amount of time for the job to wait until executing
44
68
  - priority: delayed job priority
45
69
  ```ruby
46
- async :example_method, queue: :fast, wait_until: 1.week.from_now, wait: 1.week, priority: 1
70
+ async :example_method,
71
+ queue: :fast,
72
+ wait_until: 1.week.from_now,
73
+ wait: 1.week,
74
+ priority: 1 # defined in model
75
+ async(@resource, queue: :fast, wait_until: 1.week.from_now, wait: 1.week, priority: 1).example_method # same as calling @resource.async_example_method when above is defined in model
47
76
  ```
48
77
 
49
78
  ## Installation
79
+ First, make sure your [ActiveJob is setup with the backend of your choice](https://edgeguides.rubyonrails.org/active_job_basics.html#job-execution).
50
80
  Add this line to your application's Gemfile:
51
81
 
52
82
  ```ruby
@@ -10,7 +10,7 @@ class RailsAsyncMethodsGenerator < Rails::Generators::Base
10
10
  queue_as :default
11
11
 
12
12
  def perform(receiver, method, *args, **kwargs)
13
- receiver.send(method, *args, **kwargs)
13
+ receiver.public_send(method, *args, **kwargs)
14
14
  end
15
15
  end
16
16
  FILE
@@ -0,0 +1,10 @@
1
+ require 'rails_async_methods/wrapper/async_wrapper'
2
+ require 'rails_async_methods/wrapper/async_jobs'
3
+
4
+ class Object
5
+ include RailsAsyncMethods::AsyncJobs
6
+
7
+ def to_active_job(options = {})
8
+ RailsAsyncMethods::AsyncWrapper.new(self, options)
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module RailsAsyncMethods
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -0,0 +1,9 @@
1
+ require 'rails_async_methods/wrapper/async_wrapper'
2
+
3
+ module RailsAsyncMethods
4
+ module AsyncJobs
5
+ def async(receiver, options = {})
6
+ receiver.to_active_job options
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,28 @@
1
+ require 'rails_async_methods/active_job_options_parser'
2
+
3
+ module RailsAsyncMethods
4
+ class AsyncWrapper
5
+ class NotPersistedError < StandardError; end
6
+ attr_reader :receiver, :options
7
+
8
+ def initialize(receiver, options = {})
9
+ @receiver = receiver
10
+ @options = RailsAsyncMethods::ActiveJobOptionsParser.new(options)
11
+ end
12
+
13
+ def method_missing(method, *args, **kwargs)
14
+ raise NotPersistedError, 'Instance must be persisted to run asynchronously' unless receiver.persisted?
15
+ raise TypeError, 'Cannot pass a block to an asynchronous method' if block_given?
16
+
17
+ if @receiver.methods.include? method
18
+ @options.job.set(**@options.to_h).perform_later(@receiver, method, *args, **kwargs)
19
+ else
20
+ super
21
+ end
22
+ end
23
+
24
+ def respond_to_missing?(method)
25
+ @receiver.methods.include?(method) || super
26
+ end
27
+ end
28
+ end
@@ -1,7 +1,10 @@
1
1
  require "rails_async_methods/version"
2
+ require "rails_async_methods/core_ext"
2
3
 
3
4
  module RailsAsyncMethods
4
5
  autoload :AsyncMethod, 'rails_async_methods/async_method'
6
+ autoload :AsyncWrapper, 'rails_async_methods/wrapper/async_wrapper'
7
+ autoload :AsyncJobs, 'rails_async_methods/wrapper/async_jobs'
5
8
  end
6
9
 
7
10
  ActiveSupport.on_load(:active_record) do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_async_methods
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - benngarcia
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-05-13 00:00:00.000000000 Z
11
+ date: 2022-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -109,8 +109,11 @@ files:
109
109
  - lib/rails_async_methods.rb
110
110
  - lib/rails_async_methods/active_job_options_parser.rb
111
111
  - lib/rails_async_methods/async_method.rb
112
+ - lib/rails_async_methods/core_ext.rb
112
113
  - lib/rails_async_methods/parameter_parser.rb
113
114
  - lib/rails_async_methods/version.rb
115
+ - lib/rails_async_methods/wrapper/async_jobs.rb
116
+ - lib/rails_async_methods/wrapper/async_wrapper.rb
114
117
  homepage: https://github.com/benngarcia/rails_async_methods
115
118
  licenses:
116
119
  - MIT