Skip to content

Instantly share code, notes, and snippets.

@kainio
Created July 12, 2018 06:39
Show Gist options
  • Save kainio/64879f7482f733af5c5f579d831639ed to your computer and use it in GitHub Desktop.
Save kainio/64879f7482f733af5c5f579d831639ed to your computer and use it in GitHub Desktop.
A Class Instance Variable
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