class HamlCoffeeAssets::ActionView::TemplateHandler

Constants

DEPENDENCY_PATTERN

Public Class Methods

call(template, _body = nil) click to toggle source
# File lib/haml_coffee_assets/action_view/template_handler.rb, line 6
def self.call(template, _body = nil)
  new(template).render
end
new(template, partial = false, dependencies = []) click to toggle source
# File lib/haml_coffee_assets/action_view/template_handler.rb, line 14
def initialize(template, partial = false, dependencies = [])
  @template     = template
  @partial      = partial
  @dependencies = dependencies
end
stale?(last_compile_time) click to toggle source
# File lib/haml_coffee_assets/action_view/template_handler.rb, line 10
def self.stale?(last_compile_time)
  GlobalContext.mtime > last_compile_time
end

Public Instance Methods

render() click to toggle source
# File lib/haml_coffee_assets/action_view/template_handler.rb, line 20
def render
  "ExecJS.compile(#{ compilation_string }).eval(#{ evaluation_string }).html_safe"
end

Protected Instance Methods

compilation_string() click to toggle source
# File lib/haml_coffee_assets/action_view/template_handler.rb, line 26
def compilation_string
  string = ''

  unless @partial
    string << preamble
    string << helpers
  end

  string << compiled_template

  if @partial
    string
  else
    string.inspect
  end
end

Private Instance Methods

compiled_template() click to toggle source
# File lib/haml_coffee_assets/action_view/template_handler.rb, line 58
def compiled_template
  compiled = ::HamlCoffeeAssets::Compiler.compile(
    logical_path,
    @template.source
  )

  include_dependencies(compiled)
end
evaluation_string() click to toggle source
# File lib/haml_coffee_assets/action_view/template_handler.rb, line 45
def evaluation_string
  string = "JST['#{ logical_path }'](\#{local_assigns.to_json})"
  string.inspect.sub(/\\#/, '#')
end
helpers() click to toggle source
# File lib/haml_coffee_assets/action_view/template_handler.rb, line 54
def helpers
  GlobalContext.to_s
end
include_dependencies(compiled) click to toggle source
# File lib/haml_coffee_assets/action_view/template_handler.rb, line 67
def include_dependencies(compiled)
  compiled.dup.scan(DEPENDENCY_PATTERN) do |match|
    match.compact!

    path = match[0]

    if path == logical_path || @dependencies.include?(path)
      next
    else
      @dependencies << path
    end

    partial = ::ActionView::Template.new(
      partial_source(path),
      path,
      self.class,
      virtual_path: partial_path(path),
      locals: []
    )

    compiled << self.class.new(
      partial,
      true,
      @dependencies
    ).compilation_string
  end

  compiled
end
logical_path() click to toggle source
# File lib/haml_coffee_assets/action_view/template_handler.rb, line 97
def logical_path
  return @logical_path if defined?(@logical_path)

  path = @template.virtual_path.split('/')
  path.last.sub!(/^_/, '')
  @logical_path = path.join('/')
end
partial_path(path) click to toggle source
# File lib/haml_coffee_assets/action_view/template_handler.rb, line 112
def partial_path(path)
  parts     = path.split('/')
  parts[-1] = "_#{ parts[-1] }"
  parts.join('/')
end
partial_source(path) click to toggle source
# File lib/haml_coffee_assets/action_view/template_handler.rb, line 105
def partial_source(path)
  ::Rails.root.join(
    ::HamlCoffeeAssets.config.templates_path,
    partial_path(path) + '.hamlc'
  ).read
end
preamble() click to toggle source
# File lib/haml_coffee_assets/action_view/template_handler.rb, line 50
def preamble
  "var window = { JST: {} }, JST = window.JST;\n"
end