active_job-inlined 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 89782b40c03fd934069853898a20d23838c3d7c9a1c04a35d43a66ed5f681952
4
+ data.tar.gz: fd51b2a53836b3fb1edd122cbce929940aaa47b4adbc213e2932d4ce193cfc87
5
+ SHA512:
6
+ metadata.gz: 1942d43ec4559b193cbae3a22d5771532c80b14e4a2c8df7e0819c50fc7a64f17db9699f0d565c8978876b170d5be9eceaea68bc5b137c7acc447c77f1a944af
7
+ data.tar.gz: d2b8bdd83c8b0b78e1209011cb958ad4d11239cc7bcd170a70d80c33501476f200a11478ae431d31e71120feff9d1a358e53faeadce1a37f83667805942e3586
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-09-26
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in active_job-inlined.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
11
+ gem "minitest-sprint"
data/Gemfile.lock ADDED
@@ -0,0 +1,42 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ active_job-inlined (0.1.0)
5
+ activejob (>= 6.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activejob (7.0.4)
11
+ activesupport (= 7.0.4)
12
+ globalid (>= 0.3.6)
13
+ activesupport (7.0.4)
14
+ concurrent-ruby (~> 1.0, >= 1.0.2)
15
+ i18n (>= 1.6, < 2)
16
+ minitest (>= 5.1)
17
+ tzinfo (~> 2.0)
18
+ concurrent-ruby (1.1.10)
19
+ globalid (1.0.0)
20
+ activesupport (>= 5.0)
21
+ i18n (1.12.0)
22
+ concurrent-ruby (~> 1.0)
23
+ minitest (5.16.3)
24
+ minitest-sprint (1.2.2)
25
+ path_expander (~> 1.1)
26
+ path_expander (1.1.1)
27
+ rake (13.0.6)
28
+ tzinfo (2.0.5)
29
+ concurrent-ruby (~> 1.0)
30
+
31
+ PLATFORMS
32
+ arm64-darwin-20
33
+ x86_64-linux
34
+
35
+ DEPENDENCIES
36
+ active_job-inlined!
37
+ minitest (~> 5.0)
38
+ minitest-sprint
39
+ rake (~> 13.0)
40
+
41
+ BUNDLED WITH
42
+ 2.3.21
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Kasper Timm Hansen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # ActiveJob::Inlined
2
+
3
+ `ActiveJob::Inlined` lets you run jobs inline within other jobs.
4
+
5
+ Set this up in your app:
6
+
7
+ ```ruby
8
+ class ApplicationJob < ActiveJob::Base
9
+ extend ActiveJob::Inlined
10
+ end
11
+ ```
12
+
13
+ Then use it:
14
+
15
+ ```ruby
16
+ class Post < ActiveRecord::Base
17
+ def release_later
18
+ Release::SequenceJob.perform_later self
19
+ end
20
+
21
+ def release
22
+ prep_marketing_department pizzazz_required: "✨✨✨✨"
23
+ some_other_step
24
+ # …then more stuff in the sequence, but at one point we run:
25
+ publish_later
26
+ end
27
+
28
+ def publish_later
29
+ # Now, we mark the job as `inlined`, i.e. we'll really call `perform_now`
30
+ # if we're within another job (or do the usual `perform_later` if not).
31
+ PublishJob.inlined.perform_later self
32
+ end
33
+
34
+ def publish
35
+ puts "lol"
36
+ end
37
+ end
38
+
39
+ class Post::Release::SequenceJob < ApplicationJob
40
+ def perform(post)
41
+ post.release
42
+ end
43
+ end
44
+
45
+ class Post::PublishJob < ApplicationJob
46
+ def perform(post)
47
+ post.publish
48
+ end
49
+ end
50
+ ```
51
+
52
+ ## Installation
53
+
54
+ Install the gem and add to the application's Gemfile by executing:
55
+
56
+ $ bundle add active_job-inlined
57
+
58
+ If bundler is not being used to manage dependencies, install the gem by executing:
59
+
60
+ $ gem install active_job-inlined
61
+
62
+ ## Usage
63
+
64
+ TODO: Write usage instructions here
65
+
66
+ ## Development
67
+
68
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
69
+
70
+ 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`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
71
+
72
+ ## Contributing
73
+
74
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kaspth/active_job-inlined.
75
+
76
+ ## License
77
+
78
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/active_job/inlined/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "active_job-inlined"
7
+ spec.version = ActiveJob::Inlined::VERSION
8
+ spec.authors = ["Kasper Timm Hansen"]
9
+ spec.email = ["hey@kaspth.com"]
10
+
11
+ spec.summary = "ActiveJob::Inlined lets you run jobs inline within other jobs."
12
+ spec.homepage = "https://github.com/kaspth/active_job-inlined"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 3.0.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(__dir__) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
25
+ end
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_dependency "activejob", ">= 6.1"
32
+ end
@@ -0,0 +1,25 @@
1
+ require "active_support"
2
+ require "active_support/current_attributes"
3
+
4
+ class ActiveJob::ExecutionContext < ActiveSupport::CurrentAttributes
5
+ module Integration
6
+ def self.included(job)
7
+ job.before_perform :set_context_job
8
+ job.singleton_class.delegate :executing?, to: ActiveJob::ExecutionContext
9
+ end
10
+
11
+ def set_context_job
12
+ ActiveJob::ExecutionContext.job = self
13
+ end
14
+ end
15
+
16
+ attribute :job
17
+
18
+ def executing?
19
+ job.present?
20
+ end
21
+
22
+ def in_job?(object)
23
+ executing? && job.arguments.include?(object)
24
+ end
25
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveJob
4
+ module Inlined
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "inlined/version"
4
+
5
+ module ActiveJob; end
6
+ require_relative "execution_context"
7
+
8
+ module ActiveJob::Inlined
9
+ using Module.new {
10
+ refine Module do
11
+ def invert_method(invert_method, original_method)
12
+ class_eval "def #{invert_method}; !#{original_method}; end", __FILE__, __LINE__ + 1
13
+ end
14
+ end
15
+ }
16
+
17
+ def self.extended(klass)
18
+ klass.include ActiveJob::ExecutionContext::Integration
19
+ end
20
+
21
+ def inlined
22
+ inlineable? ? Proxy.new(self) : self
23
+ end
24
+
25
+ def inlineable?
26
+ executing? && inline_enrolled?
27
+ end
28
+
29
+ def inline_exempt?
30
+ @inline_exemption&.call(self)
31
+ end
32
+ invert_method :inline_enrolled?, :inline_exempt?
33
+
34
+ # By default, jobs are considered inlineable. But they can declare themselves
35
+ # exempt by calling `inline_exempt`, like this:
36
+ #
37
+ # class Post::PublishJob < ApplicationJob
38
+ # inline_exempt
39
+ # inline_exempt -> job { job.arguments.size.even? }
40
+ # inline_exempt { |job| job.arguments.size.even? }
41
+ # end
42
+ def inline_exempt(context = nil, &block)
43
+ @inline_exemption = context || block || -> { true }
44
+ end
45
+
46
+ class Proxy
47
+ def initialize(job)
48
+ @job = job
49
+ end
50
+
51
+ def perform_later(...)
52
+ @job.perform_now(...)
53
+ nil
54
+ end
55
+
56
+ def self.chains_on(method)
57
+ class_eval "def #{method}(...); self.class.new @job.#{method}(...); end", __FILE__, __LINE__ + 1
58
+ end
59
+ chains_on :set
60
+ chains_on :with
61
+ end
62
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_job-inlined
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kasper Timm Hansen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-09-26 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: '6.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '6.1'
27
+ description:
28
+ email:
29
+ - hey@kaspth.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - CHANGELOG.md
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - active_job-inlined.gemspec
41
+ - lib/active_job/execution_context.rb
42
+ - lib/active_job/inlined.rb
43
+ - lib/active_job/inlined/version.rb
44
+ homepage: https://github.com/kaspth/active_job-inlined
45
+ licenses:
46
+ - MIT
47
+ metadata:
48
+ homepage_uri: https://github.com/kaspth/active_job-inlined
49
+ source_code_uri: https://github.com/kaspth/active_job-inlined
50
+ changelog_uri: https://github.com/kaspth/active_job-inlined/blob/main/CHANGELOG.md
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 3.0.0
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubygems_version: 3.3.21
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: ActiveJob::Inlined lets you run jobs inline within other jobs.
70
+ test_files: []