Prev | Current Page 263 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

We can define a whole bunch of methods that, instead of returning true or false
like a normal comparison, will actually record the whole comparison into the object they were
called on. Once we have that, we can turn that object into a format suitable for handing to
find_without_rquery.
The only annoying part is that we??™re basically going to be writing the same method over
and over, so we can loop over a list of method names and use define_method to keep our code
straightforward. Keep in mind that using define_method is almost exactly like using def,
except you use a symbol for the name of the function instead of just typing it. This means our
comparison functions will be named according to the symbols :==, :<, :>, :<=, :>=, and :=~.
Since the body of each comparison will be the same, we can iterate over an array containing
the operators and use define_method, like so:
CHAPTER 5 ?–  BONUS FEATURES 113
module RQuery
class Column
attr_reader :name, :operator, :operand
def initialize(name)
@name = name
end
[:==, :<, :>, :<=, :>=, :=~].


Pages:
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275