Skip to content

Commit

Permalink
better API: use fromX:ifNone: instead of returning nil
Browse files Browse the repository at this point in the history
  • Loading branch information
demarey committed Jun 3, 2020
1 parent f610484 commit 42cfb31
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
14 changes: 9 additions & 5 deletions src/FileSystem-Core/FileLocator.class.st
Expand Up @@ -245,9 +245,9 @@ FileLocator class >> flushCaches [
]

{ #category : #'instance creation' }
FileLocator class >> fromPath: aPath [
FileLocator class >> fromPath: aPath ifNone: notFoundBlock [
"Returns a file locator if aPath is a reference to a supported origin or is a child of an origin.
Else returns nil.
If no locator matches, return the result of the evaluation of notFoundBlock.
Should not be called direcly. Prefer the use of Path or String>>#asFileLocatorOrReference "

| locators locatorsPaths |
Expand All @@ -260,12 +260,16 @@ FileLocator class >> fromPath: aPath [
do: [ :locator |
((locatorsPaths at: locator) = aPath or: [ (locatorsPaths at: locator) containsPath: aPath ])
ifTrue: [ ^ locator resolve: (aPath relativeToPath: (locatorsPaths at: locator)) ] ].
^ nil
^ notFoundBlock value
]

{ #category : #'instance creation' }
FileLocator class >> fromString: aString [
^ self fromPath: aString asPath
FileLocator class >> fromString: aString ifNone: notFoundBlock [
"Returns a file locator if aString converted as path is a reference to a supported origin or is a child of an origin.
If no locator matches, return the result of the evaluation of notFoundBlock.
Should not be called direcly. Prefer the use of Path or String>>#asFileLocatorOrReference "

^ self fromPath: aString asPath ifNone: notFoundBlock
]

{ #category : #origins }
Expand Down
3 changes: 1 addition & 2 deletions src/FileSystem-Core/Path.extension.st
Expand Up @@ -3,8 +3,7 @@ Extension { #name : #Path }
{ #category : #'*FileSystem-Core' }
Path >> asFileLocatorOrReference [

^ (FileLocator fromPath: self)
ifNil: [ self asFileReference ]
^ FileLocator fromPath: self ifNone: [ self asFileReference ]
]

{ #category : #'*FileSystem-Core' }
Expand Down
12 changes: 6 additions & 6 deletions src/FileSystem-Tests-Core/FileLocatorTest.class.st
Expand Up @@ -35,7 +35,7 @@ FileLocatorTest >> testCanCreateLocatorFromStringWhenDeepChild [
| path |
path := (FileLocator home / 'foo' / 'bar' / 'bee' / 'see') fullName.

locator := FileLocator fromString: path.
locator := FileLocator fromString: path ifNone: [ nil ].

self
assert: locator origin
Expand All @@ -50,7 +50,7 @@ FileLocatorTest >> testCanCreateLocatorFromStringWhenDirectChild [
| path |
path := (FileLocator home / 'foo') fullName.

locator := FileLocator fromString: path.
locator := FileLocator fromString: path ifNone: [ nil ].

self
assert: locator origin
Expand All @@ -65,7 +65,7 @@ FileLocatorTest >> testCanCreateLocatorFromStringWhenSamePath [
| path |
path := FileLocator home fullName.

locator := FileLocator fromString: path.
locator := FileLocator fromString: path ifNone: [ nil ].

self
assert: locator
Expand Down Expand Up @@ -132,11 +132,11 @@ FileLocatorTest >> testFileSystem [
]

{ #category : #'creation tests' }
FileLocatorTest >> testFromPathReturnsNilIfPathNotPartOfAnOrigin [
FileLocatorTest >> testFromPathReturnsBlockValueIfPathNotPartOfAnOrigin [
| path |
path := '/foo' asFileReference path.

locator := FileLocator fromPath: path.
locator := FileLocator fromPath: path ifNone: [ nil ].

self
assert: locator
Expand All @@ -148,7 +148,7 @@ FileLocatorTest >> testGetRightLocatorWhenCreatingFromString [
| path |
path := (FileLocator documents / 'foo') fullName.

locator := FileLocator fromString: path.
locator := FileLocator fromString: path ifNone: [ nil ].

self
assert: locator origin
Expand Down

0 comments on commit 42cfb31

Please sign in to comment.