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.

Association.png

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.

Integer.png

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

Float.png

How does this work?

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

Object>>gtInspectorVariableValuePairs
     "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;
          yourself

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:

gtInspectorVariableValuePairs
     "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);
          yourself 

That’s it.

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