Skip to content

Commit

Permalink
Merge pull request #195 from OpenSmock/140-Component-name-generation-…
Browse files Browse the repository at this point in the history
…to-quickly-start-a-lot-of-components

Implement #140
  • Loading branch information
labordep authored Oct 1, 2024
2 parents fb486a3 + 42df89e commit 08e965b
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Molecule-Tests/MolComponentAnnouncementTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ MolComponentAnnouncementTest >> testAnnounceInstanciated [

| announcement component component2 |
announcement := nil.
SystemAnnouncer uniqueInstance when: MolComponentInstanciated do:[ :a | announcement := a ].
SystemAnnouncer uniqueInstance when: MolComponentInstantiated do:[ :a | announcement := a ].

"test by standard instanciation"
MolMyUserComponentImpl deploy.
Expand Down
29 changes: 29 additions & 0 deletions src/Molecule-Tests/MolComponentImplTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,21 @@ MolComponentImplTest >> testStart2 [
self assert: component componentName equals: #compA.
]

{ #category : #'tests - component creation' }
MolComponentImplTest >> testStartWithGeneratedName [

| instance instance2 |
instance := MolCompleteComponentImpl startWithGeneratedName.
self assert: instance componentName notNil.
self assert: instance componentName isSymbol.

instance2 := MolCompleteComponentImpl startWithGeneratedName.
self assert: instance2 componentName notNil.
self assert: instance2 componentName isSymbol.
self deny: instance componentName equals: instance2 componentName.

]

{ #category : #'tests - component creation' }
MolComponentImplTest >> testStartWithName [

Expand Down Expand Up @@ -706,6 +721,7 @@ MolComponentImplTest >> testStartWithWrongNames [

{ #category : #'tests - component creation' }
MolComponentImplTest >> testStop [

MolCompleteComponentImpl start.
MolCompleteComponentImpl stop.
self assert: (MolUtils instanceOf: MolCompleteComponentImpl) equals: nil.
Expand All @@ -715,3 +731,16 @@ MolComponentImplTest >> testStop [
self assert: (MolUtils instanceOf: MolCompleteComponentImpl named: #compA) equals: nil.

]

{ #category : #'tests - component creation' }
MolComponentImplTest >> testStopInstance [

| instance |
instance := MolCompleteComponentImpl start.
instance stop.
self assert: (MolUtils instanceOf: MolCompleteComponentImpl) equals: nil.

instance := MolCompleteComponentImpl start: #test.
instance stop.
self assert: (MolUtils instanceOf: MolCompleteComponentImpl) equals: nil.
]
4 changes: 2 additions & 2 deletions src/Molecule-Tests/MolComponentManagerTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ MolComponentManagerTest >> testCleanUp [
MolComponentManagerTest >> testCleanUpAnnouncements [
| announcement list selection |
announcement := nil.
SystemAnnouncer uniqueInstance when: MolComponentInstanciated do:[ :a | announcement := a ].
SystemAnnouncer uniqueInstance when: MolComponentInstantiated do:[ :a | announcement := a ].
SystemAnnouncer uniqueInstance when: MolComponentPassivated do:[ :a | announcement := a ].
SystemAnnouncer uniqueInstance when: MolComponentInstanciated do:[ :a | announcement := a ].
SystemAnnouncer uniqueInstance when: MolComponentInstantiated do:[ :a | announcement := a ].
list := MolAnnouncement withAllSubclasses.
selection := SystemAnnouncer uniqueInstance subscriptions subscriptions select: [ :e | list includes: e announcementClass] .
self assert: (selection size > 0).
Expand Down
13 changes: 13 additions & 0 deletions src/Molecule/MolComponentImpl.trait.st
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ MolComponentImpl classSide >> start: aComponentName [
^ MolUtils startComponent: self named: aComponentName
]

{ #category : #'start & stop' }
MolComponentImpl classSide >> startWithGeneratedName [

^ self start: (MolUtils generateComponentNameForType: self componentType)
]

{ #category : #'start & stop' }
MolComponentImpl classSide >> stop [

Expand Down Expand Up @@ -449,3 +455,10 @@ MolComponentImpl >> servicesProviders [
ifNotNil: [ :e | e servicesProviders ]
ifNil: [ MolComponentConnector defaultServicesProviders ]
]

{ #category : #stop }
MolComponentImpl >> stop [
"Stop me, whatever my name is"

self class stop: self componentName
]
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Announcement when a component is instanciated by the ComponentManager.
"
Class {
#name : #MolComponentInstanciated,
#name : #MolComponentInstantiated,
#superclass : #MolComponentAnnouncement,
#category : #'Molecule-Announcements'
}
2 changes: 1 addition & 1 deletion src/Molecule/MolHomeServices.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ MolHomeServices >> instantiateComponent: aComponentClass named: aName [
connector := MolComponentConnector new.
component componentConnector: connector.
component componentInitialize.
self announcer announce: (MolComponentInstanciated new
self announcer announce: (MolComponentInstantiated new
component: component; componentName: aName; yourself).

MolUtils log:
Expand Down
29 changes: 28 additions & 1 deletion src/Molecule/MolUtils.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ MolUtils class >> createAllComponents [
components keysDo: [ :aClass | homeServices activateComponent: aClass ]
]

{ #category : #accessing }
{ #category : #name }
MolUtils class >> defaultComponentName [
"Used when a component instanciation is not nammed"

Expand Down Expand Up @@ -131,6 +131,33 @@ MolUtils class >> deployAndInitializeComponents: aComponentClassList [
^ components
]

{ #category : #private }
MolUtils class >> doRandomComponentNameForType: aComponentType [

^ (aComponentType printString, (Random new next * 10000000) rounded printString) asSymbol

]

{ #category : #name }
MolUtils class >> generateComponentNameForType: aComponentType [

| instances componentName usedNames found |
aComponentType ifNil: [ ^ nil ].
componentName := self doRandomComponentNameForType: aComponentType.
found := false.

"check if the name is not used"
[ found ] whileFalse: [
instances := self allComponentInstancesOfType: aComponentType.
usedNames := instances collect: [ :c | c componentName ].
componentName := self doRandomComponentNameForType: aComponentType.
found := usedNames isEmpty
ifTrue: [ true ]
ifFalse: [ (usedNames includes: componentName) not ] ].

^ componentName
]

{ #category : #accessing }
MolUtils class >> instanceKindOf: aClass [

Expand Down

0 comments on commit 08e965b

Please sign in to comment.