Created
July 12, 2018 06:39
-
-
Save kainio/64879f7482f733af5c5f579d831639ed to your computer and use it in GitHub Desktop.
A Class Instance Variable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ClassLevelInheritableAttributes | |
def self.included(base) | |
base.extend(ClassMethods) | |
end | |
module ClassMethods | |
def cattr_inheritable(*args) | |
@cattr_inheritable_attrs ||= [:cattr_inheritable_attrs] | |
@cattr_inheritable_attrs += args | |
args.each do |arg| | |
class_eval %( | |
class << self; attr_accessor :#{arg} end | |
) | |
end | |
@cattr_inheritable_attrs | |
end | |
def inherited(subclass) | |
@cattr_inheritable_attrs.each do |inheritable_attribute| | |
instance_var = "@#{inheritable_attribute}" | |
subclass.instance_variable_set(instance_var, instance_variable_get(instance_var)) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment