wrapping class method

  • Thread starter Ball, Donald A Jr (Library)
  • Start date
B

Ball, Donald A Jr (Library)

Hey guys, I'm having trouble figuring out how to wrap a class method. I
want to add a logger.debug to ActiveRecord::Base.establish_connection,
just trying to figure out how the rails database connection lifecycle is
supposed to work. I tried this naive approach:

module ActiveRecord

class Base

alias self.old_establish_connection self.establish_connection

def self.establish_connection(arg)
logger.debug("Establishing connection")
self.old_establish_connection(arg)
end

end

end

of course, ruby doesn't like this, the self.old_establish_connection bit
throws a SyntaxError. How ought I go about doing this?

- donald
 
E

Erik Veenstra

Hey guys, I'm having trouble figuring out how to wrap a class

Wrapping methods? That good old story? Have a look at [1]... ;]

gegroet,
Erik V. - http://www.erikveen.dds.nl/

[1] http://www.erikveen.dds.nl/monitorfunctions/index.html

----------------------------------------------------------------

$ cat init.rb
require "ev/metameta" # This is where I store my
meta-meta-programming stuff.

require "rubygems"
require "active_record"

module ActiveRecord
class Base
metaclass.pre_condition:)establish_connection) do |obj,
method_name, args, block|
puts "Establishing connection"
end
end
end

load "script/server"

----------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,214
Latest member
JFrancisDavis

Latest Threads

Top