Created
September 24, 2010 13:38
-
-
Save UserAd/595381 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class VeryBase | |
def self.attr_accessor(*vars) | |
@attributes ||= [] | |
@attributes.concat vars | |
super(*vars) | |
end | |
def self.attributes | |
@attributes || [] | |
end | |
def attributes | |
self.class.attributes_with_parents | |
end | |
def self.attributes_with_parents(obj=self) | |
return self.attributes if @touched | |
attributes = self.attributes | |
if obj.superclass | |
if obj.superclass.respond_to? :attributes | |
attributes << obj.superclass.attributes_with_parents | |
end | |
end | |
@touched = true | |
attributes.flatten | |
end | |
end | |
class Base < VeryBase | |
attr_accessor :name | |
end | |
class Test < Base | |
attr_accessor :size | |
end | |
class Test2 < Test | |
attr_accessor :size2 | |
end | |
class Test3 < Test | |
attr_accessor :size3 | |
end | |
my_obj = Test2.new | |
puts "have object #{my_obj}" | |
my_obj_parents = my_obj.attributes_with_parents | |
puts "have attributes #{my_obj_parents.inspect}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment