spells.LocationAwareConfigModule.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spells_2.11 Show documentation
Show all versions of spells_2.11 Show documentation
This is a small scala "util" library, which will hopefully grow over time.
The newest version!
package spells
private[spells] trait LocationAwareConfigModule {
this: AnsiModule =>
import scala.collection.JavaConverters._
import com.typesafe.config.Config
import com.typesafe.config.ConfigFactory._
spellsConfig.checkValid(defaultReference(), "spells")
final lazy val spellsConfig: Config = loadSpellsConfig
protected def loadSpellsConfig: Config =
if (userConfig.exists)
load(parseFile(userConfig)).withFallback(load)
else load
private final def userConfig = new java.io.File(userConfigLocation)
private final def userConfigLocation = s"""${System.getProperty("user.home")}/.spells.conf"""
private[spells] implicit final def locationAwarePropertyToBoolean(property: LocationAwarePropertyModule#LocationAwareProperty[Boolean]): Boolean =
locationAwarePropertyTo(property, spellsConfig getBoolean property.location)
private[spells] implicit final def locationAwarePropertyToInt(property: LocationAwarePropertyModule#LocationAwareProperty[Int]): Int =
locationAwarePropertyTo(property, spellsConfig getInt property.location)
private[spells] implicit final def locationAwarePropertyToString(property: LocationAwarePropertyModule#LocationAwareProperty[List[String]]): List[String] =
locationAwarePropertyTo(property, spellsConfig.getStringList(property.location).asScala.toList)
// Unresolved compiler bug related to implicit resolution: https://issues.scala-lang.org/browse/SI-5643
private[spells] implicit final def locationAwarePropertyToAnsiStyle(property: LocationAwarePropertyModule#LocationAwareProperty[AnsiModule#AnsiStyle]): AnsiModule#AnsiStyle =
locationAwarePropertyTo(property, spellsConfig getString property.location toAnsiStyle)
private[spells] def locationAwarePropertyTo[T](property: LocationAwarePropertyModule#LocationAwareProperty[T], value: T): T = {
require(property isValid value, property validationErrorMessage value)
value
}
}