Hi everyone!
As I was making changes for the framework ticket #1919 (Asset root path should be configurable), I started feeling
some anxiety with the loose path strings here and there. I started to think that perhaps it would be better if they were wrapped
with some type and functionality for consistency, easy usage and also "findability".
Finally the question from Tim Nelson, that should the configured root path be an empty string instead of "/", triggered me to
make this simple builder for paths that would be convenient and consistent to use, easy to read and find from code and also
nice to vend from the DI system in applications. At least I have many this kind of paths in use in my lift apps, so some sort of
handle for the app developer would be good too.
So, basically this is only for static paths, I know there's all kinds of general URL and URI builders, but I had something more
simple yet specialized in my mind.
Some examples from console:
Initializing an empty path (not that useful though :)
scala> LPath()
res0: LPath.LPath =
Defining a path starting from root (the case in the ticket):
scala> LPath().fromRoot
res1: LPath.LPath = /
scala> LPath().fromRoot.withFolders("myassets" :: "mysubdir" :: Nil).withFolder("anothersubdir")
res3: LPath.LPath = /myassets/mysubdir/anothersubdir/
Complete path for some file resource perhaps:
scala> LPath().fromRoot.withFolders(List("myassets", "subdir")).withFile("abc.jpg")
res4: LPath.LPath = /myassets/subdir/abc.jpg
So the LPath object will store the path components, which here are about root, folder objects and the file object. toString method will
render the path then how it was used as a string before. Joining of LPaths is supported with .join method or ++ operator, as long as
one doesn't try to join a path with root component to another path, or any path to a path with file component, as they would be
ambiguous situations.
Now, defining for example the configured asset path and then having a component to use it as a base, would go like this:
In configuration we'd have it like this, instead of plain string object:
scala> val assetRoot = LPath().fromRoot.withFolder("assets")
assetRoot: LPath.LPath = /assets/
Then usage by joining paths together with ++ operator:
scala> assetRoot ++ LPath().withFile("myimage.jpg")
res5: LPath.LPath = /assets/myimage.jpg
Of course mostly with simple cases like this, assetRoot could be used directly:
scala> assetRoot.withFile("myimage.png")
res6: LPath.LPath = /assets/myimage.png
Implementation is following build pattern, utilizing immutable case classes. This construct would remove the possibility for
confusion about which parts of string joining the "/" chars would be etc.
What do you think, could this be added to the framework and then use it in for example the #1919 ticket and possibly find out
other uses as well?
Br,
Henrik.