Created
June 7, 2019 09:21
-
-
Save marzdgzmn/5e6771bcf4f44451c854ed3ada018bca to your computer and use it in GitHub Desktop.
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 AnimalMixin | |
class << self | |
def included(base) | |
base.extend ClassMethods | |
end | |
end | |
module ClassMethods | |
def inheritable_attributes(*args) | |
@inheritable_attributes ||= [:inheritable_attributes] | |
@inheritable_attributes += args | |
args.each do |arg| | |
class_eval %( | |
class << self; attr_accessor:#{arg} end | |
), __FILE__, __LINE__ - 2 | |
end | |
@inheritable_attributes | |
end | |
def inherited(subclass) | |
@inheritable_attributes.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