[Dbix-class] Class::Accessor::Grouped - how to use it?
joel at fysh.org
Mon Nov 10 13:11:10 GMT 2008
2008/11/10 Joel Bernstein <joel at fysh.org>:
> 2008/11/10 q <quarky at quantentunnel.de>:
>> it's probably an awful question, but I could not figure out the rationale
>> behind the module Class::Accessor::Grouped. The concept of accessors I know
>> of, but I do not understand what this "group" thing is all about. Does
>> anybody know where I can find more information regarding this? Or maybe a
>> short explanation?
> I think the Class::Accessor::Grouped POD makes it quite clear. If you
> use the "simple" group you get behaviour like Class::Accessor, where
> an accessor call hits get_simple($property) and a mutator call hits
> set_simple($property, $value). However you also have other option
> groups: "inherited", which gives behaviour similar to
> Class::Data::Accessor, and "component_class" which is specifically an
> accessor for package-names (set it, and the package is loaded before
> the property set).
OK, it looks at though I'm not actually correct.
I've hit on a sort of side detail of CAG, rather than its main
purpose. The main idea is that you can separate attributes into
groups, where each group has a similar set of behaviour required to
happen on get/set action. In OO terms, these are "attribute
metaclasses". Defining a group "foo" implies that the class will
implement a method "get_foo" and a method "set_foo", which will
encapsulate the group-specific behaviours. Each time a property in the
"foo" group is accessed, the "get_foo" method will be called with the
property name, and so on.
If you skim the DBIC source you'll see how DBIC uses the 3 builtin
groups, but also defines a couple of others ("column" and
"inflated_column"). The point is, we're able to hook arbitrary
behaviour onto the get or set actions for those attributes, and we're
able to do this in such a way as to abstract the logic away from each
individual accessor, so that it has only to be written once for each
group of similar properties.
Hope this clarifies, and I'm sure the maintainer will gratefully
accept a documentation patch from you, in the hope that others don't
suffer the same confusion as you did.
More information about the DBIx-Class