Created
June 20, 2012 01:25
-
-
Save soheilhy/2957565 to your computer and use it in GitHub Desktop.
Cake Pattern Imitated
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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