Class: Pakunok::HamlJsTemplate
- Inherits:
-
Tilt::Template
- Object
- Tilt::Template
- Pakunok::HamlJsTemplate
- Defined in:
- lib/pakunok/haml_js_template.rb
Class Attribute Summary collapse
-
.custom_escape ⇒ Object
Returns the value of attribute custom_escape.
-
.name_prefix ⇒ Object
Returns the value of attribute name_prefix.
-
.root_variable ⇒ Object
Returns the value of attribute root_variable.
Class Method Summary collapse
Instance Method Summary collapse
- #client_name ⇒ Object
- #compile_to_function ⇒ Object
- #evaluate(scope, locals, &block) ⇒ Object
- #initialize_engine ⇒ Object
- #js_custom_escape ⇒ Object
- #js_string(str) ⇒ Object
- #prepare ⇒ Object
Class Attribute Details
.custom_escape ⇒ Object
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_prefix ⇒ Object
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_variable ⇒ Object
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
8 9 10 |
# File 'lib/pakunok/haml_js_template.rb', line 8 def self.engine_initialized? defined? ::ExecJS end |
.haml_source ⇒ Object
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.('../../../vendor/assets/javascripts/pakunok/haml.js', __FILE__) end |
Instance Method Details
#client_name ⇒ Object
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_function ⇒ Object
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_engine ⇒ Object
12 13 14 |
# File 'lib/pakunok/haml_js_template.rb', line 12 def initialize_engine require_template_library 'execjs' end |
#js_custom_escape ⇒ Object
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 |
#prepare ⇒ Object
17 18 |
# File 'lib/pakunok/haml_js_template.rb', line 17 def prepare end |