Class: Class
Overview
:nodoc:
Instance Method Summary collapse
-
#class_attribute(*attrs) ⇒ Object
:nodoc:.
Instance Method Details
#class_attribute(*attrs) ⇒ Object
:nodoc:
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/stub_requests/core_ext/class/attribute.rb', line 6 def class_attribute(*attrs) = attrs. default_value = .fetch(:default, nil) attrs.each do |name| singleton_class.silence_redefinition_of_method(name) define_singleton_method(name) { nil } singleton_class.silence_redefinition_of_method("#{name}?") define_singleton_method("#{name}?") { !!public_send(name) } ivar = "@#{name}" singleton_class.silence_redefinition_of_method("#{name}=") define_singleton_method("#{name}=") do |val| singleton_class.class_eval do redefine_method(name) { val } end if singleton_class? class_eval do redefine_method(name) do if instance_variable_defined? ivar instance_variable_get ivar else singleton_class.send name end end end end val end redefine_method(name) do if instance_variable_defined?(ivar) instance_variable_get ivar else self.class.public_send name end end redefine_method("#{name}?") { !!public_send(name) } redefine_method("#{name}=") do |val| instance_variable_set ivar, val end unless default_value.nil? self.send("#{name}=", default_value) end end end |