Refactoring scenario with Ludo


We illustrate how we can construct a refactoring scenario with a series of code snippets.

Refactoring scenario

In this scenario, we create a Dummy class and a LudoGame subclass, and script a refactoring to push methods and slots from the subclass to the superclass.

Object subclass: #Dummy
	instanceVariableNames: ''
	classVariableNames: ''
	package: 'GToolkit-Demo-Ludo-Model'
Dummy subclass: #LudoGame
	instanceVariableNames: 'board players winner needToRollDie lastDieRolled'
	classVariableNames: ''
	package: 'GToolkit-Demo-Ludo-Model'

We compile a couple of dummy methods.

LudoGame compile: 'doit ^ 42'
LudoGame compile: 'initialize super initialize'

We create a refactoring instance to pull all the slots of the LudoGame class to its superclass. NB: refactorings are always performed within a given namespace.

namespace := RBNamespace new.
(LudoGame slots collect: [ :each | each name asSymbol ])
		[ :each | 
			model: namespace
			variable: each
			class: LudoGame superclass) primitiveExecute ].

And here we actually execute the refactoring:

(LudoGame slots collect: [ :each | each name asSymbol ])
	do: [ :each | (RBPullUpInstanceVariableRefactoring variable: each class: LudoGame superclass) execute ]

We select the methods to push up:

methodsToPushUp := LudoGame selectors reject: [:each | each = #initialize]

Build the refactoring:

namespace := RBNamespace new.
	model: namespace
	pullUp: methodsToPushUp
	from: LudoGame) primitiveExecute.

And perform it:

(RBPullUpMethodRefactoring pullUp: methodsToPushUp from: LudoGame)

Now all the slots and methods have been pushed up except the initialize method.

Let's clean up this mess.

	removeClassNamed: 'LudoGame';
	removeClassNamed: 'Dummy'


Splitting up the BoardElement