Created
April 7, 2011 14:32
-
-
Save loz/907868 to your computer and use it in GitHub Desktop.
Extend Exercise
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 AccessorWrapper | |
def self.included(base) | |
base.extend ClassMethods | |
end | |
def self.sneaky_logger(klass, accessor) | |
@logged_stuff ||= {} | |
@logged_stuff[klass] ||= [] | |
@logged_stuff[klass] << accessor | |
end | |
def self.logged_stuff | |
@logged_stuff | |
end | |
module ClassMethods | |
#Your Method Here.. | |
def my_accessor(name) | |
@my_attributes ||= [] | |
@my_attributes << name | |
AccessorWrapper.sneaky_logger(self, name) | |
attr_accessor name | |
end | |
def my_attributes | |
@my_attributes | |
end | |
end | |
end | |
class MyClass | |
include AccessorWrapper | |
my_accessor :field1 | |
my_accessor :field2 | |
end | |
class MyOtherClass | |
include AccessorWrapper | |
my_accessor :some_field | |
end | |
m = MyClass.new | |
m.field1 = 99 | |
p MyClass.my_attributes # => [:field1, :field2] | |
#Move into a module and find a way to get them to work in the class | |
#Bonus: Can you find all attribtues in all classes that use my_accessor? | |
p AccessorWrapper.logged_stuff |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment