Refactoring scenario with Ludo
TL;DR
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 ]) do: [ :each | (RBPullUpInstanceVariableRefactoring model: namespace variable: each class: LudoGame superclass) primitiveExecute ]. namespace
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. (RBPullUpMethodRefactoring model: namespace pullUp: methodsToPushUp from: LudoGame) primitiveExecute. namespace
And perform it:
(RBPullUpMethodRefactoring pullUp: methodsToPushUp from: LudoGame) execute
Now all the slots and methods have been pushed up except the initialize
method.
Let's clean up this mess.
Smalltalk removeClassNamed: 'LudoGame'; removeClassNamed: 'Dummy'
Next:
Splitting up the BoardElement