Skip to content

Instantly share code, notes, and snippets.

@frgomes
Last active September 25, 2015 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frgomes/78f84f048d3137ebe224 to your computer and use it in GitHub Desktop.
Save frgomes/78f84f048d3137ebe224 to your computer and use it in GitHub Desktop.
Scala - Extracts a custom configuration object from a generic com.typesafe.config.Config
package util.implicits
object RichConfig extends RichConfig
trait RichConfig {
import com.typesafe.config.Config
import net.ceedubs.ficus.Ficus._
import util.types.Database
implicit class RichConfig(config: Config) {
def database: Database = {
val name = config.as[String]("db.name")
val driver = config.as[String]("db.driver")
val url = config.as[String]("db.url")
val username = config.as[String]("db.username")
val password = config.as[String]("db.password")
Database(name, driver, url, username, password)
}
}
}
@frgomes
Copy link
Author

frgomes commented Sep 24, 2015

This example shows how a custom configuration object, in this case Database can be built and returned inline when a Database is needed but a com.typesafe.config.Config is passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment