Skip to content

Commit

Permalink
Implement #140
Browse files Browse the repository at this point in the history
  • Loading branch information
labordep committed Oct 1, 2024
1 parent fb486a3 commit d6a62b1
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
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.
]
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
]
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 d6a62b1

Please sign in to comment.