From 0c53921fc577d6eb1dce6947717d9b8046886a85 Mon Sep 17 00:00:00 2001 From: Mike Perham Date: Mon, 1 May 2017 12:07:10 -0700 Subject: [PATCH] Pull in latest class_attribute to fix warnings when not using Rails --- lib/sidekiq/core_ext.rb | 103 +++++++++++++++++++++++++--------------- 1 file changed, 64 insertions(+), 39 deletions(-) diff --git a/lib/sidekiq/core_ext.rb b/lib/sidekiq/core_ext.rb index c6b39704..1b230ae4 100644 --- a/lib/sidekiq/core_ext.rb +++ b/lib/sidekiq/core_ext.rb @@ -2,51 +2,76 @@ begin require 'active_support/core_ext/class/attribute' rescue LoadError - - # A dumbed down version of ActiveSupport's + # A dumbed down version of ActiveSupport 5.1.0's # Class#class_attribute helper. - class Class - def class_attribute(*attrs) - instance_writer = true - - attrs.each do |name| - class_eval <<-RUBY, __FILE__, __LINE__ + 1 - def self.#{name}() nil end - def self.#{name}?() !!#{name} end - - def self.#{name}=(val) - singleton_class.class_eval do - define_method(:#{name}) { val } - end - - if singleton_class? - class_eval do - def #{name} - defined?(@#{name}) ? @#{name} : singleton_class.#{name} - end - end - end - val - end - - def #{name} - defined?(@#{name}) ? @#{name} : self.class.#{name} - end - - def #{name}? - !!#{name} - end - RUBY - - attr_writer name if instance_writer + class Module + # Removes the named method, if it exists. + def remove_possible_method(method) + if method_defined?(method) || private_method_defined?(method) + undef_method(method) end end - private - def singleton_class? - ancestors.first != self + # Removes the named singleton method, if it exists. + def remove_possible_singleton_method(method) + singleton_class.instance_eval do + remove_possible_method(method) + end end end + + class Class + def class_attribute(*attrs) + instance_reader = true + instance_writer = true + + attrs.each do |name| + remove_possible_singleton_method(name) + define_singleton_method(name) { nil } + + ivar = "@#{name}" + + remove_possible_singleton_method("#{name}=") + define_singleton_method("#{name}=") do |val| + singleton_class.class_eval do + remove_possible_method(name) + define_method(name) { val } + end + + if singleton_class? + class_eval do + remove_possible_method(name) + define_method(name) do + if instance_variable_defined? ivar + instance_variable_get ivar + else + singleton_class.send name + end + end + end + end + val + end + + if instance_reader + remove_possible_method name + define_method(name) do + if instance_variable_defined?(ivar) + instance_variable_get ivar + else + self.class.public_send name + end + end + end + + if instance_writer + remove_possible_method "#{name}=" + attr_writer name + end + end + end + + end end begin