1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00

Pull in latest class_attribute to fix warnings when not using Rails

This commit is contained in:
Mike Perham 2017-05-01 12:07:10 -07:00
parent d699389f9b
commit 0c53921fc5

View file

@ -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