Skip to content

Instantly share code, notes, and snippets.

@marzdgzmn
Created June 7, 2019 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marzdgzmn/5e6771bcf4f44451c854ed3ada018bca to your computer and use it in GitHub Desktop.
Save marzdgzmn/5e6771bcf4f44451c854ed3ada018bca to your computer and use it in GitHub Desktop.
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