     else
       true
 
-  isPartial = (filepath) ->
-    isPartialFilename _.last filepath.split '/'
 
-  isPartialFilename = (filename) ->
-    regex = /^_/
-    regex.test(filename)
+########################################################
+# TODO extract these classes
+########################################################
+class TemplateBuilder
+  constructor: (window, options = {}) ->
+    @window = window
+    @rootPath = options?.rootPath
+
+  keyForFilePath: (filepath) ->
+    filepath
+      .replace(new RegExp('\\\\', 'g'), '/') #replace backslashes
+      .replace(/\.\w+$/, '') #remove extension
+      .replace(@rootPath, '')
+
+class EmberBuilder extends TemplateBuilder
+  build: (src, filepath) ->
+    key = JSON.stringify @keyForFilePath(filepath)
+    compiled = @window.Emblem.precompile(@window.Ember.Handlebars, src)
+    template = "Ember.Handlebars.template(#{compiled})"
+    "Ember.TEMPLATES[#{key}] = #{template};"
+
+class VanillaBuilder extends TemplateBuilder
+  build: (src, filepath) ->
+    key = @keyForFilePath(filepath)
+    content = @window.Emblem.precompile @window.Handlebars, src
+
+    """
+    var templates = Handlebars.templates = Handlebars.templates || {};
+    templates['#{key}'] = Handlebars.template(#{content});
+    """



  ########################################################
  # Vanilla Compilation -
  # Compiles the file at provided filepath and returns the output
  ########################################################
  compileVanilla = (src, window, key) ->
    content = window.Emblem.precompile window.Handlebars, src

    """
    var templates = Handlebars.templates = Handlebars.templates || {};
    templates['#{key}'] = Handlebars.template(#{content});
    """

  ########################################################
  # Ember Compilation
  ########################################################
  compileEmber = (src, window, key) ->
    key = JSON.stringify(key)
    compiled = window.Emblem.precompile(window.Ember.Handlebars, src)
    template = "Ember.Handlebars.template(#{compiled})"
    "Ember.TEMPLATES[#{key}] = #{template};"

  ########################################################
  # Key for given filepath
  ########################################################
  keyForFilePath = (filepath, root) ->
    key = filepath
      .replace(new RegExp('\\\\', 'g'), '/') #replace backslashes
      .replace(/\.\w+$/, '') #remove extension
      .replace(root, '')

  ########################################################
  # Filters out missing files
  ########################################################


    window.run grunt.file.read options.paths.emblem, 'utf8'
    if options.paths.ember
      window.run grunt.file.read options.paths.ember, 'utf8'
      templateBuilder = new EmberBuilder(window, rootPath: options.root)
    else
      templateBuilder = new VanillaBuilder(window, rootPath: options.root)


    grunt.verbose.writeflags(options, 'Options')
