Phrogz
11/8/2007 9:39:00 PM
On Nov 8, 2:29 pm, Phrogz <phr...@mac.com> wrote:
> On Nov 8, 1:44 pm, Greg Weeks <gregwe...@shaw.ca> wrote:
>
> > I'm still curious about the most common practice, if there is one,
> > specifically about creating little "mini-languages" that can be used
> > inside class definitions. (Yes, I'm wondering about Rails.) My example
> > with explicit class methods now smells pretty bad to me, for example.
> > Could modules (using "define_method" and other magic) "extended" into
> > class objects be considered the norm? This is a more vague question,
> > but I'm putting it out FWIW.
>
> It's what Rails and its component libraries mostly do. For example:
Actually, I would like to change my answer:
The various Rails libraries generally define class methods as
'instance' methods in modules and extend classes with those modules to
add the class methods to various classes.
However, as you've no doubt seen in rails, most of the end user
functionality of the framework is achieved by subclassing (as with my
example based on ActiveRecord). The Rails programmer does not write:
class Person
include ActiveRecord::Validations
include ActiveRecord::Locking::Optimistic
include ActiveRecord::Locking::Pessimistic
include ActiveRecord::Callbacks
include ActiveRecord::Observing
include ActiveRecord::Timestamp
include ActiveRecord::Associations
include ActiveRecord::Aggregations
include ActiveRecord::Transactions
include ActiveRecord::Reflection
include ActiveRecord::Acts::Tree
include ActiveRecord::Acts::List
include ActiveRecord::Acts::NestedSet
include ActiveRecord::Calculations
include ActiveRecord::XmlSerialization
include ActiveRecord::AttributeMethods
# phew! time to actually code stuff up
end
So perhaps you don't care *how* the class methods were placed in the
parent class.
I would say:
* Define the class methods in the class itself if you only need them
for that class.
* Define the class methods in a common parent class if a strict
hierarchy suits your needs.
* Define the class methods in a module if you want to be able to pick
and choose which sets of class methods to bring into a particular
class.