Method: Shoryuken::Worker::ClassMethods#shoryuken_class_attribute

Defined in:
lib/shoryuken/worker.rb

#shoryuken_class_attribute(*attrs) ⇒ Object

:nodoc:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/shoryuken/worker.rb', line 52

def shoryuken_class_attribute(*attrs) # :nodoc:
  attrs.each do |name|
    singleton_class.instance_eval do
      undef_method(name) if method_defined?(name) || private_method_defined?(name)
    end
    define_singleton_method(name) { nil }

    ivar = "@#{name}"

    singleton_class.instance_eval do
      m = "#{name}="
      undef_method(m) if method_defined?(m) || private_method_defined?(m)
    end

    define_singleton_method("#{name}=") do |val|
      singleton_class.class_eval do
        undef_method(name) if method_defined?(name) || private_method_defined?(name)
        define_method(name) { val }
      end

      # singleton? backwards compatibility for ruby < 2.1
      singleton_klass = respond_to?(:singleton?) ? singleton? : self != ancestors.first

      if singleton_klass
        class_eval do
          undef_method(name) if method_defined?(name) || private_method_defined?(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

    # instance reader
    undef_method(name) if method_defined?(name) || private_method_defined?(name)
    define_method(name) do
      if instance_variable_defined?(ivar)
        instance_variable_get ivar
      else
        self.class.public_send name
      end
    end

    # instance writer
    m = "#{name}="
    undef_method(m) if method_defined?(m) || private_method_defined?(m)
    attr_writer name
  end
end