Prev | Current Page 259 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

You do a similar thing when you define a class method with def
self.foo. The difference is that the eigenclass lets you work on the class with any class methods that would
normally be available, like include, attr_accessor, and alias_method.We??™ll have more on eigenclasses
later in this chapter.
CHAPTER 5 ?–  BONUS FEATURES 111
Now we have a method that??™s in the right place but doesn??™t actually do anything. This is
great, because we have a place to start building our functionality. Something to note is that the
new version of find expects to be passed a block, which is what the &blk parameter becomes.
This block contains code which will eventually get parsed into a format suitable for ActiveRecord:
:Base#find. Since the original find doesn??™t take a block at all, we can use its presence to determine
whether or not the user wants to use the extension we??™re adding with RQuery:
class ActiveRecord::Base
class << self
def find_with_rquery(*args, &blk)
if blk
conditions = RQuery::Conditions.new(&blk)
amount = args.


Pages:
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271