Class: Pakunok::HamlJsTemplate

Inherits:
Tilt::Template
  • Object
show all
Defined in:
lib/pakunok/haml_js_template.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.custom_escapeObject

Returns the value of attribute custom_escape.



71
72
73
# File 'lib/pakunok/haml_js_template.rb', line 71

def custom_escape
  @custom_escape
end

.name_prefixObject

Returns the value of attribute name_prefix.



73
74
75
# File 'lib/pakunok/haml_js_template.rb', line 73

def name_prefix
  @name_prefix
end

.root_variableObject

Returns the value of attribute root_variable.



72
73
74
# File 'lib/pakunok/haml_js_template.rb', line 72

def root_variable
  @root_variable
end

Class Method Details

.engine_initialized?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/pakunok/haml_js_template.rb', line 8

def self.engine_initialized?
  defined? ::ExecJS
end

.haml_sourceObject



75
76
77
78
# File 'lib/pakunok/haml_js_template.rb', line 75

def haml_source
  # Haml source is an asset
  @haml_source ||= IO.read File.expand_path('../../../vendor/assets/javascripts/pakunok/haml.js', __FILE__) 
end

Instance Method Details

#client_nameObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/pakunok/haml_js_template.rb', line 31

def client_name
  prefix = self.class.name_prefix || if defined?(BackboneRails)
    'backbone/templates/'
  else
    'javascripts/'
  end
  path = eval_file
  name_start = path.rindex(prefix)
  path = file[(name_start + prefix.length)..-1] if name_start


  parts = path.split(/-|_/)
  res = parts[0] + (parts[1..-1] || []).map {|w| w.capitalize }.join
  res = res.gsub(/\/|\\/, '_')
  res.split('.').first.sub(/^_/, '') # chomp of the extensions
end

#compile_to_functionObject



49
50
51
52
53
54
55
# File 'lib/pakunok/haml_js_template.rb', line 49

def compile_to_function
  function = ExecJS.
    compile(self.class.haml_source).
    eval "Haml('#{js_string data}', {escapeHtmlByDefault: true, customEscape: #{js_custom_escape}}).toString()"
  # make sure function is annonymous
  function.sub /function \w+/, "function "
end

#evaluate(scope, locals, &block) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/pakunok/haml_js_template.rb', line 21

def evaluate(scope, locals, &block)
  ns = self.class.root_variable || 'JST'
  @output ||= <<-TEMPLATE
(function(scope){
  (scope.#{ns} || (scope.#{ns} = {}))['#{client_name}'] = #{compile_to_function};
})(this);
    TEMPLATE
end

#initialize_engineObject



12
13
14
# File 'lib/pakunok/haml_js_template.rb', line 12

def initialize_engine
  require_template_library 'execjs'
end

#js_custom_escapeObject



65
66
67
# File 'lib/pakunok/haml_js_template.rb', line 65

def js_custom_escape
  self.class.custom_escape ? "'#{js_string self.class.custom_escape}'" : 'null'
end

#js_string(str) ⇒ Object



58
59
60
61
62
# File 'lib/pakunok/haml_js_template.rb', line 58

def js_string str
  (str || '').
    gsub("'")  {|m| "\\'" }.
    gsub("\n") {|m| "\\n" }
end

#prepareObject



17
18
# File 'lib/pakunok/haml_js_template.rb', line 17

def prepare
end