[PEAK] adapt based on inheritance

Phillip J. Eby pje at telecommunity.com
Wed Jun 9 15:41:45 EDT 2004


At 03:12 PM 6/9/04 -0400, Stephen Haberman wrote:
>Hi,
>
>I'd like to do something like:
>
>for f in modelElement.mdl_features:
>     f = adapt(f, IMyFeatureInterface)
>
>And then have model.Attribute features get adapted to MyAttributeFeature and
>model.Collection features get adapted to MyCollectionFeature.
>
>I was initially thinking asAdaptorForTypes would work, but the type of both
>model.Attribute and model.Collection features is
>peak.model.features.FeatureClass.
>
>Is there a convenient way to adapt based on the base classes of the target
>object? I was rather surprised I couldn't, though perhaps this is a misuse
>of the adaption concept?

What you want is 'declareAdapter(adapterfactory, provides=[IWhatever], 
forObjects=[feature]).  That is, you want to declare adapters for the 
specific object(s), not their classes.  Inheritance will then do what you 
want here.

Keep in mind, however, that the use of Collection as a base class for 
multi-valued features is quite optional.  Inheriting from Attribute and 
setting the upperBound to a number greater than 1 suffices to make the 
feature multi-valued, and this approach is used in e.g. code generated by 
the 'mof2py' script.




More information about the PEAK mailing list