rails_async_methods 0.1.0 → 0.2.1
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 +4 -4
- data/README.md +43 -5
- data/lib/generators/rails_async_methods_generator.rb +1 -1
- data/lib/rails_async_methods/active_job_options_parser.rb +1 -1
- data/lib/rails_async_methods/core_ext.rb +10 -0
- data/lib/rails_async_methods/version.rb +1 -1
- data/lib/rails_async_methods/wrapper/async_jobs.rb +9 -0
- data/lib/rails_async_methods/wrapper/async_wrapper.rb +28 -0
- data/lib/rails_async_methods.rb +3 -0
- metadata +12 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bc5c3f909c325ba16b8fe08b6a49cc02a92d2ba47ae8bff9641ed9be90a21721
|
4
|
+
data.tar.gz: 07c6c15681d352409635553dac5bfb8778cc509eac5977659ae0f067deafc31d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 592c26c40c0d23e4d6a52676bb7323e271e66e54940031ea242cffe029e300473dfd0ce44b7260c3d81de4c1ef34a99becbb48584c65e87979898bcaed271839
|
7
|
+
data.tar.gz: 705aff342859b61a299f378e6d2ae9f62950a35c13735f280505bdba28255f2f6534950bf1878c51b22ba4b68ad50728bb3699ce483c926a4965d1511b6313bb
|
data/README.md
CHANGED
@@ -1,14 +1,21 @@
|
|
1
1
|
# RailsAsyncMethods
|
2
|
-
Rails Async Methods is an opinionated gem meant to remove boilerplate while creating
|
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
|
12
|
+
|
13
|
+
# or...
|
14
|
+
|
15
|
+
async def example_method2
|
16
|
+
# logic...
|
17
|
+
end
|
10
18
|
end
|
11
|
-
async :example_method
|
12
19
|
```
|
13
20
|
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
21
|
|
@@ -19,8 +26,30 @@ def example_method_with_args(a:, b:)
|
|
19
26
|
end
|
20
27
|
async :example_method_with_args
|
21
28
|
```
|
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.
|
29
|
+
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
30
|
|
31
|
+
### Object Wrapper
|
32
|
+
Alternatively, if you don't want to declare methods as async in your model, you can utilize the async object wrapper made available globally to all objects.
|
33
|
+
```ruby
|
34
|
+
class ResourceController < ApplicationController
|
35
|
+
def create
|
36
|
+
async(@resource).any_method_available_to_resource
|
37
|
+
end
|
38
|
+
end
|
39
|
+
```
|
40
|
+
If you don't want to wrap objects in a utility method, you can also use the expressive type converter.
|
41
|
+
### Type Converter
|
42
|
+
```ruby
|
43
|
+
class ResourceService
|
44
|
+
def run
|
45
|
+
@resource.to_active_job.any_method_available_to_resource
|
46
|
+
end
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
Note: Under the hood, the object wrapper just makes a call ```to_active_job```, so these objects work the same.
|
51
|
+
|
52
|
+
It is recommended that you default to the model declaration syntax.
|
24
53
|
### Options
|
25
54
|
|
26
55
|
You can pass the following options to your async declaration.
|
@@ -34,7 +63,10 @@ user_instance.asynchronous_example_method
|
|
34
63
|
|
35
64
|
- job: use a custom job other than the generated ```RailsAsyncMethods::AbstractJob``` - see section on Custom Jobs below i.e.
|
36
65
|
```ruby
|
37
|
-
async :example_method, job: CustomExampleMethodJob
|
66
|
+
async :example_method, job: CustomExampleMethodJob # defined in model
|
67
|
+
async(@resource, job: CustomExampleMethodJob).example_method # callable anywhere
|
68
|
+
@resource.to_active_job(job: CustomExampleMethodJob).example_method
|
69
|
+
|
38
70
|
```
|
39
71
|
|
40
72
|
- ActiveJob configurations
|
@@ -43,10 +75,16 @@ async :example_method, job: CustomExampleMethodJob
|
|
43
75
|
- wait: give an amount of time for the job to wait until executing
|
44
76
|
- priority: delayed job priority
|
45
77
|
```ruby
|
46
|
-
async :example_method,
|
78
|
+
async :example_method,
|
79
|
+
queue: :fast,
|
80
|
+
wait_until: 1.week.from_now,
|
81
|
+
wait: 1.week,
|
82
|
+
priority: 1 # defined in model
|
83
|
+
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
84
|
```
|
48
85
|
|
49
86
|
## Installation
|
87
|
+
First, make sure your [ActiveJob is setup with the backend of your choice](https://edgeguides.rubyonrails.org/active_job_basics.html#job-execution).
|
50
88
|
Add this line to your application's Gemfile:
|
51
89
|
|
52
90
|
```ruby
|
@@ -9,7 +9,7 @@ module RailsAsyncMethods
|
|
9
9
|
@prefix = method_prefix(opts[:prefix])
|
10
10
|
@queue = opts[:queue]
|
11
11
|
@wait_until = opts[:wait_until].to_f if opts[:wait_until]
|
12
|
-
@wait = opts[:wait].seconds.
|
12
|
+
@wait = opts[:wait].seconds.to_f if opts[:wait]
|
13
13
|
@priority = opts[:priority].to_i if opts[:priority]
|
14
14
|
@job = get_job_obj(opts[:job])
|
15
15
|
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
|
data/lib/rails_async_methods.rb
CHANGED
@@ -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,43 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_async_methods
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- benngarcia
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-05-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: rails
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 7.0.2.4
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 7.0.2.4
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: minitest
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -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
|
@@ -132,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
132
135
|
- !ruby/object:Gem::Version
|
133
136
|
version: '0'
|
134
137
|
requirements: []
|
135
|
-
rubygems_version: 3.
|
138
|
+
rubygems_version: 3.1.6
|
136
139
|
signing_key:
|
137
140
|
specification_version: 4
|
138
141
|
summary: Quickly, create async callers and receivers for your rails methods, because
|