Skip to content

Instantly share code, notes, and snippets.

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 ljuti/247881 to your computer and use it in GitHub Desktop.
Save ljuti/247881 to your computer and use it in GitHub Desktop.
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