active_model-jobs 0.1.0 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4b425410108badbd825b1d7abf39fa6c95e0af10
4
- data.tar.gz: 23282f539b1055e3009985dc473f15463527ae9d
3
+ metadata.gz: 3498758a1e49f162cf4ac307c59fcc514f84cf4b
4
+ data.tar.gz: d6cfb41f2ea4efa81fd306bafd2f7e0bf48feac1
5
5
  SHA512:
6
- metadata.gz: 49d7d44acac33e64ea7d3ea22b5e9915cb58e20522659870b2efff77dabf883f73c9607285a052a2c5ee3d4a9396805cd5662a77ff9fca365782fbd6e6e2a654
7
- data.tar.gz: 791e870af887280896309501e6c16dbb9cef9318ff8f4297879c21d3bb6c218b71f5544aac6a745653ada7d22b45fdde40954cf183fb25aaf231a6004a81da47
6
+ metadata.gz: 56cf8488a7d852d7f226bf97a9213e7fa783611bb5b6b1473f11e5da8fe609dff8bbb6016c88146477b639f5ded2a65e2a060eb6363e7ca764acaf2c53da4dea
7
+ data.tar.gz: 6c7d2fc095551477b5f815b1569b43e20d9faf8370e948d8501b4efe17f92b3b2082d5cba94ac4bc5a3017e42a46e0ba01bbdace7231ace9d3cae21264ef1289
data/.rubocop.yml CHANGED
@@ -8,7 +8,7 @@ AllCops:
8
8
  # Don't require documentation for mocks
9
9
  Style/Documentation:
10
10
  Exclude:
11
- - spec/support/mocks.rb
11
+ - spec/**/*
12
12
 
13
13
  # Allow any kind of string literal
14
14
  Style/StringLiterals:
data/README.md CHANGED
@@ -12,25 +12,14 @@ gem 'active_model-jobs'
12
12
 
13
13
  And then execute:
14
14
 
15
- $ bundle
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
- In your model:
26
-
27
- ```ruby
28
- class Track < ActiveRecord::Base
29
- after_commit :upload!
30
- end
31
- ```
19
+ ## Usage
32
20
 
33
- Now, generate a job called `UploadTrackJob`:
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
- You can also call `track.upload!` to kick off the job on demand.
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
- ## Development
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
- 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.
62
+ ```ruby
63
+ ActiveRecord::Base.send :include, ActiveModel::Jobs
64
+ ```
59
65
 
60
- ### Contribution Guidelines
66
+ ## Development
61
67
 
62
- All contributions must be made in a pull request and include
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/[my-github-username]/active_model-jobs/fork )
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).
@@ -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
- return super unless respond_to? method
20
- job_for(method).perform_later(self)
21
- end
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 method_for_job?(method)
35
- return false unless method.to_s =~ BANG
36
- job_for(method).present?
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
@@ -2,6 +2,6 @@
2
2
  module ActiveModel
3
3
  module Jobs
4
4
  # @type [String]
5
- VERSION = "0.1.0"
5
+ VERSION = "0.1.1"
6
6
  end
7
7
  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.0
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: