Skip to content

Commit 08e965b

Browse files
authored
Merge pull request #195 from OpenSmock/140-Component-name-generation-to-quickly-start-a-lot-of-components
Implement #140
2 parents fb486a3 + 42df89e commit 08e965b

File tree

7 files changed

+75
-6
lines changed

7 files changed

+75
-6
lines changed

src/Molecule-Tests/MolComponentAnnouncementTest.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ MolComponentAnnouncementTest >> testAnnounceInstanciated [
5252

5353
| announcement component component2 |
5454
announcement := nil.
55-
SystemAnnouncer uniqueInstance when: MolComponentInstanciated do:[ :a | announcement := a ].
55+
SystemAnnouncer uniqueInstance when: MolComponentInstantiated do:[ :a | announcement := a ].
5656

5757
"test by standard instanciation"
5858
MolMyUserComponentImpl deploy.

src/Molecule-Tests/MolComponentImplTest.class.st

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,21 @@ MolComponentImplTest >> testStart2 [
658658
self assert: component componentName equals: #compA.
659659
]
660660

661+
{ #category : #'tests - component creation' }
662+
MolComponentImplTest >> testStartWithGeneratedName [
663+
664+
| instance instance2 |
665+
instance := MolCompleteComponentImpl startWithGeneratedName.
666+
self assert: instance componentName notNil.
667+
self assert: instance componentName isSymbol.
668+
669+
instance2 := MolCompleteComponentImpl startWithGeneratedName.
670+
self assert: instance2 componentName notNil.
671+
self assert: instance2 componentName isSymbol.
672+
self deny: instance componentName equals: instance2 componentName.
673+
674+
]
675+
661676
{ #category : #'tests - component creation' }
662677
MolComponentImplTest >> testStartWithName [
663678

@@ -706,6 +721,7 @@ MolComponentImplTest >> testStartWithWrongNames [
706721

707722
{ #category : #'tests - component creation' }
708723
MolComponentImplTest >> testStop [
724+
709725
MolCompleteComponentImpl start.
710726
MolCompleteComponentImpl stop.
711727
self assert: (MolUtils instanceOf: MolCompleteComponentImpl) equals: nil.
@@ -715,3 +731,16 @@ MolComponentImplTest >> testStop [
715731
self assert: (MolUtils instanceOf: MolCompleteComponentImpl named: #compA) equals: nil.
716732

717733
]
734+
735+
{ #category : #'tests - component creation' }
736+
MolComponentImplTest >> testStopInstance [
737+
738+
| instance |
739+
instance := MolCompleteComponentImpl start.
740+
instance stop.
741+
self assert: (MolUtils instanceOf: MolCompleteComponentImpl) equals: nil.
742+
743+
instance := MolCompleteComponentImpl start: #test.
744+
instance stop.
745+
self assert: (MolUtils instanceOf: MolCompleteComponentImpl) equals: nil.
746+
]

src/Molecule-Tests/MolComponentManagerTest.class.st

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ MolComponentManagerTest >> testCleanUp [
4242
MolComponentManagerTest >> testCleanUpAnnouncements [
4343
| announcement list selection |
4444
announcement := nil.
45-
SystemAnnouncer uniqueInstance when: MolComponentInstanciated do:[ :a | announcement := a ].
45+
SystemAnnouncer uniqueInstance when: MolComponentInstantiated do:[ :a | announcement := a ].
4646
SystemAnnouncer uniqueInstance when: MolComponentPassivated do:[ :a | announcement := a ].
47-
SystemAnnouncer uniqueInstance when: MolComponentInstanciated do:[ :a | announcement := a ].
47+
SystemAnnouncer uniqueInstance when: MolComponentInstantiated do:[ :a | announcement := a ].
4848
list := MolAnnouncement withAllSubclasses.
4949
selection := SystemAnnouncer uniqueInstance subscriptions subscriptions select: [ :e | list includes: e announcementClass] .
5050
self assert: (selection size > 0).

src/Molecule/MolComponentImpl.trait.st

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ MolComponentImpl classSide >> start: aComponentName [
178178
^ MolUtils startComponent: self named: aComponentName
179179
]
180180

181+
{ #category : #'start & stop' }
182+
MolComponentImpl classSide >> startWithGeneratedName [
183+
184+
^ self start: (MolUtils generateComponentNameForType: self componentType)
185+
]
186+
181187
{ #category : #'start & stop' }
182188
MolComponentImpl classSide >> stop [
183189

@@ -449,3 +455,10 @@ MolComponentImpl >> servicesProviders [
449455
ifNotNil: [ :e | e servicesProviders ]
450456
ifNil: [ MolComponentConnector defaultServicesProviders ]
451457
]
458+
459+
{ #category : #stop }
460+
MolComponentImpl >> stop [
461+
"Stop me, whatever my name is"
462+
463+
self class stop: self componentName
464+
]

src/Molecule/MolComponentInstanciated.class.st renamed to src/Molecule/MolComponentInstantiated.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Announcement when a component is instanciated by the ComponentManager.
33
"
44
Class {
5-
#name : #MolComponentInstanciated,
5+
#name : #MolComponentInstantiated,
66
#superclass : #MolComponentAnnouncement,
77
#category : #'Molecule-Announcements'
88
}

src/Molecule/MolHomeServices.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ MolHomeServices >> instantiateComponent: aComponentClass named: aName [
206206
connector := MolComponentConnector new.
207207
component componentConnector: connector.
208208
component componentInitialize.
209-
self announcer announce: (MolComponentInstanciated new
209+
self announcer announce: (MolComponentInstantiated new
210210
component: component; componentName: aName; yourself).
211211

212212
MolUtils log:

src/Molecule/MolUtils.class.st

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ MolUtils class >> createAllComponents [
9696
components keysDo: [ :aClass | homeServices activateComponent: aClass ]
9797
]
9898

99-
{ #category : #accessing }
99+
{ #category : #name }
100100
MolUtils class >> defaultComponentName [
101101
"Used when a component instanciation is not nammed"
102102

@@ -131,6 +131,33 @@ MolUtils class >> deployAndInitializeComponents: aComponentClassList [
131131
^ components
132132
]
133133

134+
{ #category : #private }
135+
MolUtils class >> doRandomComponentNameForType: aComponentType [
136+
137+
^ (aComponentType printString, (Random new next * 10000000) rounded printString) asSymbol
138+
139+
]
140+
141+
{ #category : #name }
142+
MolUtils class >> generateComponentNameForType: aComponentType [
143+
144+
| instances componentName usedNames found |
145+
aComponentType ifNil: [ ^ nil ].
146+
componentName := self doRandomComponentNameForType: aComponentType.
147+
found := false.
148+
149+
"check if the name is not used"
150+
[ found ] whileFalse: [
151+
instances := self allComponentInstancesOfType: aComponentType.
152+
usedNames := instances collect: [ :c | c componentName ].
153+
componentName := self doRandomComponentNameForType: aComponentType.
154+
found := usedNames isEmpty
155+
ifTrue: [ true ]
156+
ifFalse: [ (usedNames includes: componentName) not ] ].
157+
158+
^ componentName
159+
]
160+
134161
{ #category : #accessing }
135162
MolUtils class >> instanceKindOf: aClass [
136163

0 commit comments

Comments
 (0)