module AppleCore::Extensions::Class::ClassMethods
Public Instance Methods
class_attribute(method_name, options = {})
click to toggle source
# File lib/apple_core/extensions/class.rb, line 15 def class_attribute(method_name, options = {}) instance_reader = options.fetch(:instance_accessor, true) && options.fetch(:instance_reader, true) instance_writer = options.fetch(:instance_accessor, true) && options.fetch(:instance_writer, true) remove_possible_singleton_method(method_name) define_singleton_method(method_name) { options[:default] } ivar = "@#{method_name}" remove_possible_singleton_method("#{method_name}=") define_singleton_method("#{method_name}=") do |val| singleton_class.class_eval do remove_possible_method(method_name) define_method(method_name) { val } end if singleton_class? class_eval do remove_possible_method(method_name) define_method(method_name) do if instance_variable_defined? ivar instance_variable_get ivar else singleton_class.send method_name end end end end val end if instance_reader remove_possible_method method_name define_method(method_name) do if instance_variable_defined?(ivar) instance_variable_get ivar else self.class.public_send method_name end end end if instance_writer remove_possible_method "#{method_name}=" attr_writer method_name end end