Extending the variables shown in GTInspector

The GTInspector offers a moldable infrastructure that makes it easy for you to define specific representations for your objects.

The most basic way to extend the inspector is to extend the State presentation. By default, the State presentation shows the instance variables of the inspected object. For example, the picture below shows the inspector displaying the key and value instance variables of an association object.


However, sometimes you want it to show something else. For example, for an integer, you might want it to show the representation of the integer in various bases.


Or for a Float, you might want it to show the exponent and significand.


How does this work?

Quite simple. All you have to do is override gtInspectorVariableValuePairs in your class. The generic implementation in Object looks like:

     "This is a helper method that returns a collection of variable names -> value for the current object.
     Subclasses can override it to specialize what appears in the variables presentation"
     | bindings |
     bindings := self class allInstVarNames collect: [ :iv | iv -> (self instVarNamed: iv) ].
     ^ Set new
          addAll: bindings;

It simply collects all variable names and associated values. To customize it, you need to add your own items to the result. For example, in the case of Integer, it looks like:

     "We override the default variables to add representations for various bases"
     ^ super gtInspectorVariableValuePairs
          add: 'hex' -> self printStringHex;
          add: 'octal' -> (self printStringBase: 8);
          add: 'binary' -> (self printStringBase: 2);

That’s it.

Posted by Tudor Girba at 21 December 2013, 11:42 pm with tags tooling, moose, pharo link