Created
December 3, 2009 04:12
-
-
Save ljuti/247881 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
module AttrLogger | |
def self.included(base) | |
base.extend ClassLevelMethods | |
end | |
module ClassLevelMethods | |
def attr_logger(*args) | |
args.each { |attribute| | |
define_method(attribute) do | |
value = instance_variable_get("@#{attribute}") | |
STDERR.puts "#{Time.now} - GET - #{value}" | |
return value | |
end | |
define_method("#{attribute}=") do |val| | |
instance_variable_set("@#{attribute}", val) | |
STDERR.puts "#{Time.now} - SET - #{val}" | |
end | |
} | |
end | |
end | |
end | |
class Person | |
include AttrLogger | |
attr_logger :first_name | |
attr_logger :last_name | |
def initialize | |
@first_name = "John" | |
@last_name = "Doe" | |
end | |
end | |
john = Person.new | |
first = john.first_name | |
last = john.last_name | |
p first | |
p last | |
first = john.first_name = "Jonathan" | |
last = john.last_name = "Smith" | |
p first | |
p last |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment