lib/delayable.rb in delayable-0.3.0 vs lib/delayable.rb in delayable-0.4.0
- old
+ new
@@ -1,10 +1,18 @@
+require "active_support/concern"
+require "active_support/core_ext/string/inflections"
+
require "delayable/version"
module Delayable
extend ActiveSupport::Concern
+ included do
+ fail "Please define ApplicationJob before including Delayable" unless defined?(ApplicationJob)
+ fail "Please define Current before including Delayable" unless defined?(Current)
+ end
+
class_methods do
def delay(method_name, class_method: false, wait: nil, limits_concurrency: nil, queue: :default)
job = const_set(delayable_job_name(method_name, class_method:),
delayable_job_class(method_name, class_method:, concurrency_limits: limits_concurrency, queue:))
@@ -32,14 +40,20 @@
end
end
end
def delayable_job_name(method_name, class_method: false)
- :"#{'Class' if class_method}#{method_name.to_s.camelize}Job"
+ class_name = bang_method?(method_name) ? "#{method_name.to_s.chomp('!').camelize}Bang" : method_name.to_s.camelize
+ :"#{'Class' if class_method}#{class_name}Job"
end
def delayable_define_method(class_method:, method_name:, &block)
define_method_type = class_method ? :define_singleton_method : :define_method
- public_send(define_method_type, "#{method_name}_later", &block)
+ method_name = bang_method?(method_name) ? "#{method_name.to_s.chomp('!')}_later!" : "#{method_name}_later"
+ public_send(define_method_type, method_name, &block)
+ end
+
+ def bang_method?(method_name)
+ method_name.to_s.end_with?('!')
end
end
end