Skip to content

Instantly share code, notes, and snippets.

@soheilhy
Created June 20, 2012 01:25
Show Gist options
  • Save soheilhy/2957565 to your computer and use it in GitHub Desktop.
Save soheilhy/2957565 to your computer and use it in GitHub Desktop.
Cake Pattern Imitated
// You create the trait.
trait JavaScriptCompilerComponent {
def compiler: JavaScriptCompiler
trait JavaScriptCompiler {
def compile(files: Seq[Source]): String
}
}
// You implement it.
trait DummyJavaScriptParserComponent extends JavaScriptCompilerComponent {
override val compiler = new DummyJavaScriptCompiler
class DummyJavaScriptCompiler extends JavaScriptCompiler {
override def compile(files: Seq[Source]) = {
...
}
}
}
trait PageRenderer {
def render: Elem
}
// You add the dependecy.
class MyPageRenderer extends PageRenderer { this: JavaScriptCompilerComponent =>
val jsFiles = Source.fromFile("test.js") :: Nil
override def render =
<html>
<script>
{compiler.compile(jsFiles)}
</script>
</html>
}
// And you inject it.
val page = new MyPageRenderer with DummyJavaScriptParserComponent
...
page.render
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment