activejob-perform_later 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3b5b407399e36460850a38bd2d99322b5fd57803
4
+ data.tar.gz: 322ad837ea9516cb06506ed841a76e08a139b0c1
5
+ SHA512:
6
+ metadata.gz: 5257e7665e29dcec2d66c42030ceeb5a2f6bfd3879a5754fb50cfe8cf2c07f29127a6d049a97168b1a9a95fac19ec1d30d3e8fba22f6de901ddf0f6b206d798c
7
+ data.tar.gz: 73709080400ecad1ccfb36a77b4ca16e4fd669b3f2f062daac202ba515834d4bea0a69ca5e77ae72f7ba47d8ef37ac4c37ab947baa2b2991873733d8913b2e96
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ sudo: false
3
+ cache: bundler
4
+ rvm:
5
+ - 2.0.0
6
+ - 2.1.0
7
+ - 2.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activejob', github: 'rails/rails', branch: '4-2-stable'
4
+ gem 'activesupport', github: 'rails/rails', branch: '4-2-stable'
5
+
6
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Cristian Bica
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,87 @@
1
+ # Activejob::PerformLater
2
+
3
+ Make any method perfomed later.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'activejob-perform_later'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install activejob-perform_later
18
+
19
+ ## Usage
20
+
21
+ ### Declare your class methods as always performed later
22
+
23
+ ```ruby
24
+ class MyClass
25
+ def self.a_method(arg1, arg2)
26
+ # do stuff
27
+ end
28
+ perform_later :a_method
29
+ perform_later :a_method, queue: :low_priority
30
+ perform_later :a_method, wait: 10.minutes
31
+ perform_later :a_method, wait: 60.minutes, queue: :low_priority
32
+ end
33
+
34
+ #calling the method will actually schedule the method to be performed later by ActiveJob
35
+ MyClass.a_method(1, 2)
36
+ ```
37
+
38
+ ### Perform your class method delayed whenever you need that
39
+
40
+ ```ruby
41
+ class MyClass
42
+ def self.a_method(arg1, arg2)
43
+ # do stuff
44
+ end
45
+ end
46
+
47
+ # calling the method directly will perform it inline
48
+ MyClass.a_method(1,2)
49
+
50
+ # calling the method with perform_later will schedule the performing in Active Job
51
+ MyClass.perform_later.a_method(1,2)
52
+ MyClass.perform_later(queue: :low_priority).a_method(1,2)
53
+ MyClass.perform_later(wait: 10.minutes).a_method(1,2)
54
+ MyClass.perform_later(wait: 10.minutes, queue: :low_priority).a_method(1,2)
55
+ MyClass.perform_later(wait_until: 60.minutes.from_now, queue: :low_priority).a_method(1,2)
56
+ end
57
+ ```
58
+
59
+ ### Perform your instance method delayed
60
+
61
+ This works only if your class is safe to be passed to Active Job (e.g.: it's global
62
+ identificable with GlobalID. Active Record implements GlobalID since 4.2.0):
63
+
64
+ ```ruby
65
+ class User < ActiveRecord::Base
66
+ def recalculate_billing
67
+ # do stuff
68
+ end
69
+ end
70
+
71
+ # calling the method with perform_later will schedule the performing in Active Job
72
+ User.last.perform_later.recalculate_billing
73
+ User.last.perform_later(queue: :low_priority).recalculate_billing
74
+ User.last.perform_later(wait: 10.minutes).recalculate_billing
75
+ User.last.perform_later(wait_until: 60.minutes.from_now).recalculate_billing
76
+ User.last.perform_later(wait: 10.minutes, queue: :low_priority).recalculate_billing
77
+ User.last.perform_later(wait_until: 60.minutes.from_now, queue: :low_priority).recalculate_billing
78
+ end
79
+ ```
80
+
81
+ ## Contributing
82
+
83
+ 1. Fork it ( https://github.com/cristianbica/activejob-perform_later/fork )
84
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
85
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
86
+ 4. Push to the branch (`git push origin my-new-feature`)
87
+ 5. Create a new Pull Request
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.test_files = FileList['test/cases/**/*_test.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'activejob/perform_later/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "activejob-perform_later"
8
+ spec.version = Activejob::PerformLater::VERSION
9
+ spec.authors = ["Cristian Bica"]
10
+ spec.email = ["cristian.bica@gmail.com"]
11
+ spec.summary = %q{Make any method perfomed later.}
12
+ spec.description = %q{Take advantage of Active Job you can perform any class method later.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'activejob'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ end
@@ -0,0 +1,15 @@
1
+ require "activejob/perform_later/version"
2
+ require 'active_support'
3
+ require 'active_job'
4
+
5
+ module Activejob
6
+ module PerformLater
7
+ extend ActiveSupport::Autoload
8
+
9
+ autoload :Job
10
+ autoload :Proxy
11
+ autoload :Mixin
12
+ end
13
+ end
14
+
15
+ Object.send(:include, Activejob::PerformLater::Mixin)
@@ -0,0 +1,10 @@
1
+ module Activejob
2
+ module PerformLater
3
+ class Job < ActiveJob::Base
4
+ def perform(target, method_name, args)
5
+ target = target.safe_constantize if target.is_a?(String)
6
+ target.public_send method_name, args
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,34 @@
1
+ module Activejob
2
+ module PerformLater
3
+ module Mixin
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ def perform_later(*args)
8
+ if args[0].is_a?(Symbol)
9
+ raise ArgumentError, "#{self} does not have a `#{args[0]}` class method" unless respond_to?(args[0])
10
+ method = args[0]
11
+ options = args[1]||{}
12
+
13
+ aliased_method, punctuation = method.to_s.sub(/([?!=])$/, ''), $1
14
+ now_method, later_method = "#{aliased_method}_now#{punctuation}", "#{aliased_method}_later#{punctuation}"
15
+
16
+ singleton_class.send(:define_method, later_method) do |*args|
17
+ Job.new(self.name, now_method, args).enqueue options
18
+ end
19
+
20
+ singleton_class.send(:alias_method, now_method, method)
21
+ singleton_class.send(:alias_method, method, later_method)
22
+ else
23
+ options = args[0]||{}
24
+ Proxy.new(self.name, options)
25
+ end
26
+ end
27
+ end
28
+
29
+ def perform_later(options={})
30
+ Proxy.new(self, options)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,14 @@
1
+ module Activejob
2
+ module PerformLater
3
+ class Proxy < BasicObject
4
+ def initialize(target, options)
5
+ @target = target
6
+ @options = options
7
+ end
8
+
9
+ def method_missing(method_name, *args)
10
+ Job.new(@target, method_name.to_s, args).enqueue @options
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ module Activejob
2
+ module PerformLater
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,56 @@
1
+ require 'helper'
2
+ require 'classes/test_class'
3
+
4
+ class ClassMethodsAlwaysTest < ActiveSupport::TestCase
5
+ include ActiveJob::TestHelper
6
+
7
+ def test_creates_later_and_now_methods
8
+ assert_respond_to TestClass, :one_later
9
+ assert_respond_to TestClass, :one_now
10
+ end
11
+
12
+ def test_creates_later_and_now_methods_with_punctuation
13
+ assert_respond_to TestClass, :four_later?
14
+ assert_respond_to TestClass, :four_now?
15
+ end
16
+
17
+ def test_enqueues_the_job
18
+ assert_enqueued_jobs 1 do
19
+ TestClass.one
20
+ end
21
+ end
22
+
23
+ def test_enqueues_the_job_with_delay
24
+ travel_to Time.now do
25
+ assert_enqueued_with(job: Activejob::PerformLater::Job, args: ["TestClass", "two_now", [1, 2]], at: 60.seconds.from_now.to_f) do
26
+ TestClass.two(1, 2)
27
+ end
28
+ end
29
+ end
30
+
31
+ def test_enqueues_the_job_on_a_queue
32
+ assert_enqueued_with(job: Activejob::PerformLater::Job, args: ["TestClass", "three_now", [1, 2]], queue: 'non_default') do
33
+ TestClass.three(1, 2)
34
+ end
35
+ end
36
+
37
+ def test_performs_the_job
38
+ assert_performed_jobs 1 do
39
+ TestClass.one
40
+ end
41
+ end
42
+
43
+ def test_performs_the_job_with_delay
44
+ travel_to Time.now do
45
+ assert_performed_with(job: Activejob::PerformLater::Job, args: ["TestClass", "two_now", [1, 2]], at: 60.seconds.from_now.to_f) do
46
+ TestClass.two(1, 2)
47
+ end
48
+ end
49
+ end
50
+
51
+ def test_performs_the_job_on_a_queue
52
+ assert_performed_with(job: Activejob::PerformLater::Job, args: ["TestClass", "three_now", [1, 2]], queue: 'non_default') do
53
+ TestClass.three(1, 2)
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,59 @@
1
+ require 'helper'
2
+ require 'classes/test_class'
3
+
4
+ class ClassMethodsAlwaysTest < ActiveSupport::TestCase
5
+ include ActiveJob::TestHelper
6
+
7
+ def test_enqueues_the_job
8
+ assert_enqueued_jobs 1 do
9
+ TestClass.perform_later.five
10
+ end
11
+ end
12
+
13
+ def test_enqueues_the_job_with_arguments
14
+ assert_enqueued_with(job: Activejob::PerformLater::Job, args: ["TestClass", "five", [1, 2]]) do
15
+ TestClass.perform_later.five(1, 2)
16
+ end
17
+ end
18
+
19
+ def test_enqueues_the_job_with_delay
20
+ travel_to Time.now do
21
+ assert_enqueued_with(job: Activejob::PerformLater::Job, args: ["TestClass", "five", [1, 2]], at: 60.seconds.from_now.to_f) do
22
+ TestClass.perform_later(wait: 60.seconds).five(1, 2)
23
+ end
24
+ end
25
+ end
26
+
27
+ def test_enqueues_the_job_on_a_queue
28
+ assert_enqueued_with(job: Activejob::PerformLater::Job, args: ["TestClass", "five", [1, 2]], queue: 'non_default') do
29
+ TestClass.perform_later(queue: "non_default").five(1, 2)
30
+ end
31
+ end
32
+
33
+
34
+ def test_performs_the_job
35
+ assert_performed_jobs 1 do
36
+ TestClass.perform_later.five
37
+ end
38
+ end
39
+
40
+ def test_performs_the_job_with_arguments
41
+ assert_performed_with(job: Activejob::PerformLater::Job, args: ["TestClass", "five", [1, 2]]) do
42
+ TestClass.perform_later.five(1, 2)
43
+ end
44
+ end
45
+
46
+ def test_performs_the_job_with_delay
47
+ travel_to Time.now do
48
+ assert_performed_with(job: Activejob::PerformLater::Job, args: ["TestClass", "five", [1, 2]], at: 60.seconds.from_now.to_f) do
49
+ TestClass.perform_later(wait: 60.seconds).five(1, 2)
50
+ end
51
+ end
52
+ end
53
+
54
+ def test_performs_the_job_on_a_queue
55
+ assert_performed_with(job: Activejob::PerformLater::Job, args: ["TestClass", "five", [1, 2]], queue: 'non_default') do
56
+ TestClass.perform_later(queue: "non_default").five(1, 2)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,60 @@
1
+ require 'helper'
2
+ require 'classes/test_class'
3
+
4
+ class ClassMethodsAlwaysTest < ActiveSupport::TestCase
5
+ include ActiveJob::TestHelper
6
+
7
+ def test_enqueues_the_job
8
+ assert_enqueued_jobs 1 do
9
+ TestClass.new.perform_later.six
10
+ end
11
+ end
12
+
13
+ def test_enqueues_the_job_with_arguments
14
+ assert_enqueued_with(job: Activejob::PerformLater::Job, args: [{"_aj_globalid"=>"gid://ajpl/TestClass/0"}, 'six', [1, 2]]) do
15
+ TestClass.new.perform_later.six(1, 2)
16
+ end
17
+ end
18
+
19
+ def test_enqueues_the_job_with_delay
20
+ travel_to Time.now do
21
+ assert_enqueued_with(job: Activejob::PerformLater::Job, at: 60.seconds.from_now.to_f) do
22
+ TestClass.new.perform_later(wait: 60.seconds).six
23
+ end
24
+ end
25
+ end
26
+
27
+ def test_enqueues_the_job_on_a_queue
28
+ assert_enqueued_with(job: Activejob::PerformLater::Job, queue: 'non_default') do
29
+ TestClass.new.perform_later(queue: "non_default").six
30
+ end
31
+ end
32
+
33
+
34
+ def test_performs_the_job
35
+ assert_performed_jobs 1 do
36
+ TestClass.new.perform_later.six
37
+ end
38
+ end
39
+
40
+ def test_performs_the_job_with_arguments
41
+ assert_performed_with(job: Activejob::PerformLater::Job, args: [{"_aj_globalid"=>"gid://ajpl/TestClass/0"}, 'six', [1, 2]]) do
42
+ TestClass.new.perform_later.six(1, 2)
43
+ end
44
+ end
45
+
46
+ def test_performs_the_job_with_delay
47
+ travel_to Time.now do
48
+ assert_performed_with(job: Activejob::PerformLater::Job, at: 60.seconds.from_now.to_f) do
49
+ TestClass.new.perform_later(wait: 60.seconds).six
50
+ end
51
+ end
52
+ end
53
+
54
+ def test_performs_the_job_on_a_queue
55
+ assert_performed_with(job: Activejob::PerformLater::Job, queue: 'non_default') do
56
+ TestClass.new.perform_later(queue: "non_default").six
57
+ end
58
+ end
59
+
60
+ end
@@ -0,0 +1,33 @@
1
+ class TestClass
2
+ include GlobalID::Identification
3
+
4
+ def self.find(*)
5
+ new
6
+ end
7
+
8
+ def id
9
+ 0
10
+ end
11
+
12
+ def self.one(*)
13
+ end
14
+ perform_later :one
15
+
16
+ def self.two(*)
17
+ end
18
+ perform_later :two, wait: 60
19
+
20
+ def self.three(*)
21
+ end
22
+ perform_later :three, queue: 'non_default'
23
+
24
+ def self.four?(*)
25
+ end
26
+ perform_later :four?
27
+
28
+ def self.five(*)
29
+ end
30
+
31
+ def six(*)
32
+ end
33
+ end
@@ -0,0 +1,14 @@
1
+ require 'bundler'
2
+ Bundler.setup
3
+
4
+ $LOAD_PATH << File.dirname(__FILE__) + '/../lib'
5
+
6
+ require 'activejob/perform_later'
7
+ require 'active_support/core_ext/numeric/time'
8
+
9
+ ActiveSupport::TestCase.test_order = :random
10
+ ActiveJob::Base.queue_adapter = :test
11
+ GlobalID.app = 'ajpl'
12
+ ActiveJob::Base.logger.level = Logger::ERROR
13
+
14
+ require 'active_support/testing/autorun'
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activejob-perform_later
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Cristian Bica
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activejob
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Take advantage of Active Job you can perform any class method later.
56
+ email:
57
+ - cristian.bica@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - activejob-perform_later.gemspec
69
+ - lib/activejob/perform_later.rb
70
+ - lib/activejob/perform_later/job.rb
71
+ - lib/activejob/perform_later/mixin.rb
72
+ - lib/activejob/perform_later/proxy.rb
73
+ - lib/activejob/perform_later/version.rb
74
+ - test/cases/class_methods_always_test.rb
75
+ - test/cases/class_methods_on_demand_test.rb
76
+ - test/cases/instance_methods_on_demand_test.rb
77
+ - test/classes/test_class.rb
78
+ - test/helper.rb
79
+ homepage: ''
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.4.5
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Make any method perfomed later.
103
+ test_files:
104
+ - test/cases/class_methods_always_test.rb
105
+ - test/cases/class_methods_on_demand_test.rb
106
+ - test/cases/instance_methods_on_demand_test.rb
107
+ - test/classes/test_class.rb
108
+ - test/helper.rb
109
+ has_rdoc: