Skip to content

Latest commit

 

History

History
117 lines (77 loc) · 3.3 KB

README.md

File metadata and controls

117 lines (77 loc) · 3.3 KB

Hooks provides a way for Scala programs to support plugins and optional features.

Warning

Under development The Hooks library is still being developed. Details will change before it's ready for release.

Extend your code

Annotate your code with special hooks:

val nameFilter = FilterHook[String]()
val userSavedAction = ActionHook[User]()

Call into that hook in your program code:

val displayName = nameFilter(user.name)
userSavedAction(user)

Plugins can attach behaviours to that hook to modify it:

nameFilter.hook { name => name.toUpperCase }
userSavedAction.hook { user => sendNotificationEmail(user) }

Optional features

Features group these modifications:

object Uppercase extends Feature("Uppercase") {
  def init() {
    nameFilter.hook { name => name.toUpperCase }
  }
}

Select which features you want to use when running code:

val features = List(Uppercase, AnotherFeature)
val displayName = FeatureRepository.using(features) {
  nameFilter(user.name)
}

Features can depend on each other and they'll be kept together:

object Uppercase extends Feature("Uppercase",
                                 depend = List(AnotherFeature)) {

You can control the order you want features to be initialised:

object Uppercase extends Feature("Uppercase",
                                 depend = List(AnotherFeature)
                                 before = List(AnotherFeature)) {

Plugins

Load plugins from a directory:

val folder = new File("homedir/plugins")
val classpath = List(new File("myapplication.jar"))
new PluginLoader(folder, classpath, ".jar").hookAll()

Using Hooks with SBT

You can add the Hooks library to your SBT project by putting this into your build file:

libraryDependencies += "cc.minotaur" %% "hooks" % "0.1"

Note that at present Hooks is compatible with Scala versions 2.9.0 and 2.9.1.

Read more