jobify 0.1.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 68c0e9df1850d8df3ffd79918d74a14b8c5269b1e546107af418b04770032e42
4
- data.tar.gz: e1414b2383e972103a9808401a077fc05eead62d35661f99339cb857a9e5052b
3
+ metadata.gz: 8c34bf93e6019d918312706b17ed0503044d2dc4ea16de824702223afa879d49
4
+ data.tar.gz: 2328a20bc2654f9053d85694d062bda712762d00100747f0a68166436df56729
5
5
  SHA512:
6
- metadata.gz: a9fce3c64c0b07da031c0c8cc47394920b272f5dc96a1a8e0bd0e3d9bc3b5f3217eecc04fbaf19a1ee69b77cb594bcddadc617e781140065f9f4b2d4c0e340b2
7
- data.tar.gz: f9d835f57374eec52d20902055ad5fc21cadcf58d8b0bff107a206912b19213f28ba676086e510334b745bb2960250a290781d267135da8d5ec3a1017f28d5d1
6
+ metadata.gz: 5ed50a875bdff43a912fa392cc852c4f80ddc43c10d453037ecc5c64c1407a93d83a23f3ae078d27a6ba63d8588ef3d2c5bdc3dd148a5e190e0e19020c577ce5
7
+ data.tar.gz: 9dd6f7b8765756f3ec85979e75564254970ac8851b42d23431b8434f24a79d5114a6bb61aa181b230d6889c2d4aa906cecb9c2fd349c5d878586d97fb63f79a3
data/.rubocop.yml CHANGED
@@ -1,5 +1,7 @@
1
1
  AllCops:
2
2
  TargetRubyVersion: 2.6
3
+ NewCops: enable
4
+ SuggestExtensions: false
3
5
  Exclude:
4
6
  - 'test/*'
5
7
 
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.6
data/CHANGELOG.md CHANGED
@@ -8,3 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
8
8
 
9
9
  ## [0.1.0] - 2022-10-25
10
10
  - Initial release
11
+
12
+ ## [0.2.0] - 2022-10-27
13
+ - Add support for my_method?
14
+ - Add support for my_method!
15
+ - Allow POROs with same-name class and instance methods to jobify class methods
16
+
17
+ ## [0.3.0] - 2022-11-13
18
+ - Add support for private methods
data/Gemfile CHANGED
@@ -6,7 +6,3 @@ source "https://rubygems.org"
6
6
  gemspec
7
7
 
8
8
  gem "rake", "~> 13.0"
9
-
10
- gem "minitest", "~> 5.0"
11
-
12
- gem "rubocop", "~> 1.21"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- jobify (0.1.0)
4
+ jobify (0.3.0)
5
5
  activejob
6
6
  activesupport
7
7
 
@@ -31,14 +31,14 @@ GEM
31
31
  rake (13.0.6)
32
32
  regexp_parser (2.6.0)
33
33
  rexml (3.2.5)
34
- rubocop (1.37.0)
34
+ rubocop (1.38.0)
35
35
  json (~> 2.3)
36
36
  parallel (~> 1.10)
37
37
  parser (>= 3.1.2.1)
38
38
  rainbow (>= 2.2.2, < 4.0)
39
39
  regexp_parser (>= 1.8, < 3.0)
40
40
  rexml (>= 3.2.5, < 4.0)
41
- rubocop-ast (>= 1.22.0, < 2.0)
41
+ rubocop-ast (>= 1.23.0, < 2.0)
42
42
  ruby-progressbar (~> 1.7)
43
43
  unicode-display_width (>= 1.4.0, < 3.0)
44
44
  rubocop-ast (1.23.0)
@@ -50,6 +50,7 @@ GEM
50
50
 
51
51
  PLATFORMS
52
52
  x86_64-darwin-20
53
+ x86_64-darwin-21
53
54
  x86_64-linux
54
55
 
55
56
  DEPENDENCIES
@@ -59,4 +60,4 @@ DEPENDENCIES
59
60
  rubocop (~> 1.21)
60
61
 
61
62
  BUNDLED WITH
62
- 2.3.14
63
+ 2.3.18
data/jobify.gemspec ADDED
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/jobify/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "jobify"
7
+ spec.version = Jobify::VERSION
8
+ spec.authors = ["Søren Houen"]
9
+ spec.email = ["s@houen.net"]
10
+
11
+ spec.summary = "Turn any method into a background job (`jobify :hello_world` generates `def perform_hello_world_later`"
12
+ spec.homepage = "https://github.com/houen/jobify"
13
+
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/houen/jobify"
21
+ spec.metadata["changelog_uri"] = "https://github.com/houen/jobify/CHANGELOG.md"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(__dir__) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
28
+ end
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ # Uncomment to register a new dependency of your gem
35
+ spec.add_dependency "activejob"
36
+ spec.add_dependency "activesupport"
37
+
38
+ spec.add_development_dependency "minitest", "~> 5.0"
39
+ spec.add_development_dependency "rubocop", "~> 1.21"
40
+
41
+ # For more information and examples about making a new gem, check out our
42
+ # guide at: https://bundler.io/guides/creating_gem.html
43
+ spec.metadata["rubygems_mfa_required"] = "true"
44
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Jobify
4
- VERSION = "0.1.1"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/jobify.rb CHANGED
@@ -17,33 +17,43 @@ module Jobify
17
17
 
18
18
  included do
19
19
  @jobified_methods = {
20
- instance: {},
20
+ instance: {},
21
21
  singleton: {}
22
22
  }
23
23
 
24
- def self.jobify(method_name, job_method_name: "perform_#{method_name}_later")
24
+ def self.jobify(method_name, job_method_name: nil)
25
25
  raise "method name cannot be blank" if method_name.blank?
26
26
 
27
- method_name = method_name.to_s
27
+ method_name = method_name.to_s
28
+ job_method_name = job_method_name(method_name, job_method_name)
28
29
 
29
- if method_defined?(method_name) && !@jobified_methods[:instance][method_name]
30
+ if has_class_method?(method_name) && !@jobified_methods[:singleton][method_name]
31
+ # Class methods are jobified first,
32
+ # so that class methods can be jobified by POROs with same-name instance methods
33
+ params = method(method_name).parameters
34
+ _define_job_class(method_name, job_method_name, params, singleton_method: true)
35
+ @jobified_methods[:singleton][method_name] = true
36
+ elsif has_instance_method?(method_name) && !@jobified_methods[:instance][method_name]
30
37
  # Instance method
31
38
  params = instance_method(method_name).parameters
32
- _define_job_class(method_name, job_method_name, params, false)
39
+ _define_job_class(method_name, job_method_name, params, singleton_method: false)
33
40
  @jobified_methods[:instance][method_name] = true
34
- elsif respond_to?(method_name) && !@jobified_methods[:singleton][method_name]
35
- # Singleton method (Class method)
36
- params = method(method_name).parameters
37
- _define_job_class(method_name, job_method_name, params, true)
38
- @jobified_methods[:singleton][method_name] = true
39
41
  end
40
42
  end
41
43
 
42
- def self._define_job_class(method_name, job_method_name, params, singleton_method)
43
- job_class_name = singleton_method ? "JobifyClassMethod_#{method_name}_Job" : "JobifyInstanceMethod_#{method_name}_Job"
44
- parent_class = defined?(ApplicationJob) ? ApplicationJob : ActiveJob::Base
45
- job_class = Class.new(parent_class)
46
- caller_class = self
44
+ def self.job_method_name(method_name, job_method_name)
45
+ return method_name unless job_method_name.nil?
46
+ return "perform_#{method_name[0..-2]}_later?" if method_name.end_with?('?')
47
+ return "perform_#{method_name[0..-2]}_later!" if method_name.end_with?('!')
48
+ "perform_#{method_name}_later"
49
+ end
50
+
51
+ def self._define_job_class(method_name, job_method_name, params, singleton_method:)
52
+ method_name_for_class_constant = method_name.gsub('?', '_question_').gsub('!', '_bang_')
53
+ job_class_name = singleton_method ? "JobifyClassMethod_#{method_name_for_class_constant}_Job" : "JobifyInstanceMethod_#{method_name_for_class_constant}_Job"
54
+ parent_class = defined?(ApplicationJob) ? ApplicationJob : ActiveJob::Base
55
+ job_class = Class.new(parent_class)
56
+ caller_class = self
47
57
  const_set(job_class_name, job_class)
48
58
 
49
59
  # Define perform method on job class
@@ -59,9 +69,9 @@ module Jobify
59
69
  def self.singleton__define_job_perform_method(job_class, caller_class, method_name)
60
70
  job_class.define_method(:perform) do |*args, **kw_args|
61
71
  if kw_args.empty?
62
- caller_class.public_send(method_name, *args)
72
+ caller_class.send(method_name, *args)
63
73
  else
64
- caller_class.public_send(method_name, *args, **kw_args)
74
+ caller_class.send(method_name, *args, **kw_args)
65
75
  end
66
76
  end
67
77
  end
@@ -88,7 +98,7 @@ module Jobify
88
98
  raise "Something has gone wrong. Record id is required" unless id
89
99
 
90
100
  record = caller_class.find(id)
91
- record.public_send(method_name, *args, **kw_args)
101
+ record.send(method_name, *args, **kw_args)
92
102
  end
93
103
  end
94
104
 
@@ -129,5 +139,14 @@ module Jobify
129
139
  def ensure_all_args_present!(req_args, args)
130
140
  self.class.ensure_all_args_present!(req_args, args)
131
141
  end
142
+
143
+ def self.has_class_method?(method_name)
144
+ respond_to?(method_name, true)
145
+ end
146
+
147
+ def self.has_instance_method?(method_name)
148
+ method_defined?(method_name) || private_method_defined?(method_name)
149
+ end
150
+
132
151
  end
133
152
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jobify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Søren Houen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-26 00:00:00.000000000 Z
11
+ date: 2022-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activejob
@@ -38,6 +38,34 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.21'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.21'
41
69
  description:
42
70
  email:
43
71
  - s@houen.net
@@ -46,12 +74,14 @@ extensions: []
46
74
  extra_rdoc_files: []
47
75
  files:
48
76
  - ".rubocop.yml"
77
+ - ".ruby-version"
49
78
  - CHANGELOG.md
50
79
  - Gemfile
51
80
  - Gemfile.lock
52
81
  - LICENSE.txt
53
82
  - README.md
54
83
  - Rakefile
84
+ - jobify.gemspec
55
85
  - lib/jobify.rb
56
86
  - lib/jobify/version.rb
57
87
  - sig/jobify.rbs
@@ -63,6 +93,7 @@ metadata:
63
93
  homepage_uri: https://github.com/houen/jobify
64
94
  source_code_uri: https://github.com/houen/jobify
65
95
  changelog_uri: https://github.com/houen/jobify/CHANGELOG.md
96
+ rubygems_mfa_required: 'true'
66
97
  post_install_message:
67
98
  rdoc_options: []
68
99
  require_paths:
@@ -78,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
109
  - !ruby/object:Gem::Version
79
110
  version: '0'
80
111
  requirements: []
81
- rubygems_version: 3.2.32
112
+ rubygems_version: 3.1.6
82
113
  signing_key:
83
114
  specification_version: 4
84
115
  summary: Turn any method into a background job (`jobify :hello_world` generates `def