active_model-jobs 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -1
- data/README.md +38 -25
- data/lib/active_model/jobs.rb +15 -37
- data/lib/active_model/jobs/performer.rb +74 -0
- data/lib/active_model/jobs/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3498758a1e49f162cf4ac307c59fcc514f84cf4b
|
4
|
+
data.tar.gz: d6cfb41f2ea4efa81fd306bafd2f7e0bf48feac1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 56cf8488a7d852d7f226bf97a9213e7fa783611bb5b6b1473f11e5da8fe609dff8bbb6016c88146477b639f5ded2a65e2a060eb6363e7ca764acaf2c53da4dea
|
7
|
+
data.tar.gz: 6c7d2fc095551477b5f815b1569b43e20d9faf8370e948d8501b4efe17f92b3b2082d5cba94ac4bc5a3017e42a46e0ba01bbdace7231ace9d3cae21264ef1289
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -12,25 +12,14 @@ gem 'active_model-jobs'
|
|
12
12
|
|
13
13
|
And then execute:
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
## Usage
|
18
|
-
|
19
|
-
In an initializer:
|
20
|
-
|
21
|
-
```ruby
|
22
|
-
ActiveRecord::Base.send :include, ActiveModel::Jobs
|
15
|
+
```bash
|
16
|
+
$ bundle
|
23
17
|
```
|
24
18
|
|
25
|
-
|
26
|
-
|
27
|
-
```ruby
|
28
|
-
class Track < ActiveRecord::Base
|
29
|
-
after_commit :upload!
|
30
|
-
end
|
31
|
-
```
|
19
|
+
## Usage
|
32
20
|
|
33
|
-
|
21
|
+
Given we already have a model called **Track**, generate a job
|
22
|
+
called `UploadTrackJob`:
|
34
23
|
|
35
24
|
```ruby
|
36
25
|
require 'aws/s3'
|
@@ -50,24 +39,48 @@ class UploadTrackJob < ActiveRecord::Base
|
|
50
39
|
end
|
51
40
|
```
|
52
41
|
|
53
|
-
|
42
|
+
Now, you can kick off that job by calling its "action method" on your
|
43
|
+
model:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
class Track < ActiveRecord::Base
|
47
|
+
include ActiveModel::Jobs
|
48
|
+
|
49
|
+
after_commit :upload!, on: :create
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
Since this is just an instance method, you can call `track.upload!` to
|
54
|
+
kick off the job at any time outside of the callback lifecycle.
|
54
55
|
|
56
|
+
### Global Inclusion
|
55
57
|
|
56
|
-
|
58
|
+
You can include this module in every ActiveRecord model by inserting the
|
59
|
+
following code into an initializer at
|
60
|
+
**config/initializers/active_model_jobs.rb**:
|
57
61
|
|
58
|
-
|
62
|
+
```ruby
|
63
|
+
ActiveRecord::Base.send :include, ActiveModel::Jobs
|
64
|
+
```
|
59
65
|
|
60
|
-
|
66
|
+
## Development
|
61
67
|
|
62
|
-
|
63
|
-
accompanying tests. Pull requests will not be merged until they pass the
|
64
|
-
CI build for all supported Ruby and Rails versions.
|
65
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
68
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
66
69
|
|
67
70
|
## Contributing
|
68
71
|
|
69
|
-
1. Fork it ( https://github.com/
|
72
|
+
1. Fork it ( https://github.com/tubbo/active_model-jobs/fork )
|
70
73
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
71
74
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
72
75
|
4. Push to the branch (`git push origin my-new-feature`)
|
73
76
|
5. Create a new Pull Request
|
77
|
+
|
78
|
+
All contributions must be made in a pull request and include accompanying tests.
|
79
|
+
Pull requests will not be merged until they pass the CI build for all supported
|
80
|
+
Ruby and Rails versions. To install this gem onto your local machine, run
|
81
|
+
`bundle exec rake install`. To release a new version, update the version number
|
82
|
+
in `version.rb`, and then run `bundle exec rake release` to create a git
|
83
|
+
tag for the version, push git commits and tags, and push the `.gem` file
|
84
|
+
to [rubygems.org](https://rubygems.org).
|
85
|
+
|
86
|
+
Also see our [Code of Conduct](https://github.com/tubbo/active_model-jobs/blob/master/CODE_OF_CONDUCT.md).
|
data/lib/active_model/jobs.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
+
require "active_support/all"
|
1
2
|
require "active_model"
|
2
3
|
require "active_model/jobs/version"
|
4
|
+
require "active_model/jobs/performer"
|
3
5
|
|
4
6
|
module ActiveModel
|
5
7
|
# Include this module into your model to take advantage of
|
@@ -8,51 +10,27 @@ module ActiveModel
|
|
8
10
|
#
|
9
11
|
# @api public
|
10
12
|
module Jobs
|
11
|
-
# @type [RegExp]
|
12
|
-
BANG = /!\Z/
|
13
|
-
|
14
13
|
# Call +perform_later+ on an ActiveJob class corresponding to an
|
15
|
-
# undefined action method name.
|
14
|
+
# undefined action method name. Most of the work here is done in the
|
15
|
+
# +Performer+ class, which takes care of discoevering whether the
|
16
|
+
# method passed in corresponds to a given job or whether we should
|
17
|
+
# just delegate back to +ActiveRecord::Base+. This method will
|
18
|
+
# prevent a new +Perfomer+ class from being instantiated for every
|
19
|
+
# method call by using a guard clause to check whether the method is
|
20
|
+
# an action method before proceeding on further checks.
|
16
21
|
#
|
17
22
|
# @throws NoMethodError if no job matches the action method
|
18
23
|
def method_missing(method, *arguments)
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
# Test whether the action method exists on this class.
|
24
|
-
#
|
25
|
-
# @returns [FalseClass] if the method does not end in a '!'
|
26
|
-
# @returns [FalseClass] if the method does not correspond to a job
|
27
|
-
# @returns [TrueClass] if a job with a corresponding name is found
|
28
|
-
def respond_to?(method)
|
29
|
-
method_for_job?(method) || super
|
24
|
+
performer = job_performer(method)
|
25
|
+
return super unless performer.present?
|
26
|
+
performer.call self
|
30
27
|
end
|
31
28
|
|
32
29
|
private
|
33
30
|
|
34
|
-
def
|
35
|
-
return
|
36
|
-
|
37
|
-
end
|
38
|
-
|
39
|
-
def job_for(method)
|
40
|
-
job_name(method).classify.constantize
|
41
|
-
rescue LoadError
|
42
|
-
logger.debug "#{job_name(method)} is not defined"
|
43
|
-
nil
|
44
|
-
end
|
45
|
-
|
46
|
-
def job_name(method)
|
47
|
-
[
|
48
|
-
job_action_name(method.to_s),
|
49
|
-
model_name,
|
50
|
-
'job'
|
51
|
-
].join '_'
|
52
|
-
end
|
53
|
-
|
54
|
-
def job_action_name(method)
|
55
|
-
method.gsub(BANG, '')
|
31
|
+
def job_performer(method)
|
32
|
+
return unless method =~ Performer::BANG
|
33
|
+
Performer.new method, model_name
|
56
34
|
end
|
57
35
|
end
|
58
36
|
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
#
|
3
|
+
module Jobs
|
4
|
+
# A support class for finding the +ActiveJob::Base+ that corresponds
|
5
|
+
# to a given action method on a given model. When the job class is
|
6
|
+
# found, the action method fires off a new instance of the job.
|
7
|
+
#
|
8
|
+
# @api private
|
9
|
+
# @private
|
10
|
+
class Performer
|
11
|
+
# @type [RegExp]
|
12
|
+
BANG = /!\Z/
|
13
|
+
|
14
|
+
# @attr_reader
|
15
|
+
# @type [String]
|
16
|
+
attr_reader :method_name
|
17
|
+
|
18
|
+
# @attr_reader
|
19
|
+
# @type [String]
|
20
|
+
attr_reader :model_name
|
21
|
+
|
22
|
+
# @param [String] method_name - A method corresponding to a job.
|
23
|
+
# @param [String] model_name - The model we are calling this from.
|
24
|
+
def initialize(method_name, model_name)
|
25
|
+
@method_name = method_name.to_s
|
26
|
+
@model_name = model_name.to_s
|
27
|
+
end
|
28
|
+
|
29
|
+
# Tests whether this method name corresponds to a job class in the
|
30
|
+
# application.
|
31
|
+
#
|
32
|
+
# @returns [Boolean] whether this job exists or not
|
33
|
+
def job?
|
34
|
+
method_name =~ BANG && job_class.present?
|
35
|
+
end
|
36
|
+
|
37
|
+
# Attempts to find the job class for this method and return it,
|
38
|
+
# otherwise it returns +nil+ when encountering a +NameError+.
|
39
|
+
#
|
40
|
+
# @returns [ActiveJob::Base] a job class or nil
|
41
|
+
def job_class
|
42
|
+
job_name.classify.constantize
|
43
|
+
rescue NameError
|
44
|
+
nil
|
45
|
+
end
|
46
|
+
|
47
|
+
# Build the conventional job name from the given method and model.
|
48
|
+
# Suffix with +job+ and separate with underscores.
|
49
|
+
#
|
50
|
+
# @returns [String] the underscored job class name
|
51
|
+
def job_name
|
52
|
+
[
|
53
|
+
action_name, model_name, 'job'
|
54
|
+
].join '_'
|
55
|
+
end
|
56
|
+
|
57
|
+
# Strip the '!' off of the end of the method.
|
58
|
+
#
|
59
|
+
# @returns [String]
|
60
|
+
def action_name
|
61
|
+
method_name.gsub BANG, ''
|
62
|
+
end
|
63
|
+
|
64
|
+
# Perform this action on the given model.
|
65
|
+
#
|
66
|
+
# @param [ActiveModel::Model]
|
67
|
+
# @returns [TrueClass]
|
68
|
+
def call(model)
|
69
|
+
return false unless job?
|
70
|
+
job_class.perform_later model
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_model-jobs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tom Scott
|
@@ -126,6 +126,7 @@ files:
|
|
126
126
|
- bin/yardoc
|
127
127
|
- bin/yri
|
128
128
|
- lib/active_model/jobs.rb
|
129
|
+
- lib/active_model/jobs/performer.rb
|
129
130
|
- lib/active_model/jobs/version.rb
|
130
131
|
homepage: http://github.com/tubbo/active_model-jobs
|
131
132
|
licenses:
|