We can work around these problems, but
this method should still be considered very experimental.
The Og library has less baggage than ActiveRecord, and doesn??™t try to do as much,
so the code is a bit more readable. It uses ???magic??? just as much as ActiveRecord
does, but in a different way. Rather than defining its own set of property accessors
(as DataMapper does), Og overrides the standard Ruby attr_accessor method to
define properties. It uses ObjectSpace to find objects that represent model classes
(based on their use of the new attr_accessor or inclusion of the Og::Model module).
This means that the models can look almost exactly like plain-old Ruby objects. The
only difference is that the types of the properties must be explicitly specified, so that
they can be mapped to SQL types:
class Person
attr_accessor :first_name, String
attr_accessor :last_name, String
attr_accessor :dob, Date
end
276 | Chapter 9: Incorporating and Extending Rails
This approach leads to very readable code, but it has a couple of drawbacks:
??? All models must be loaded before Og is started, because Og only traverses
ObjectSpace when it is first loaded.
??? Og??™s attr_accessor method will mark a class as an Og model. However, if the
attr_accessor method is not used first thing in a class declaration, the Og methods
will not be pulled in. Consider this example:
class Person
is Og::Model
has_many :friends, Friend
end
The is method is a Ruby Facets alias for include that more closely reflects the
semantics of multiple inheritance.
Pages:
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420