Sha256: 52649165fd16ede808c050d4293fb0714fc018b4e783b1162a1b0064888bb7f4
Contents?: true
Size: 1.46 KB
Versions: 6
Compression:
Stored size: 1.46 KB
Contents
# frozen_string_literal: true # :nodoc: class Class # :nodoc: def class_attribute(*attrs) options = attrs.extract_options! default_value = options.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 unless method_defined?(:class_attribute) end
Version data entries
6 entries across 6 versions & 1 rubygems