Class: Handlebarsjs::HandlebarsSnapshot
- Inherits:
-
Object
- Object
- Handlebarsjs::HandlebarsSnapshot
- Includes:
- KLog::Logging
- Defined in:
- lib/handlebarsjs/handlebars_snapshot.rb
Overview
Wraps MiniRacer snapshot with specific emphasis on loading Handlebars helpers onto the MiniRacer context in the correct order. So that new contexts are preloaded with the handlebars library and the configured helpers.
Instance Attribute Summary collapse
-
#helpers ⇒ Object
readonly
Returns the value of attribute helpers.
-
#scripts ⇒ Object
readonly
Returns the value of attribute scripts.
Instance Method Summary collapse
- #add_callback(name, callback, safe, parameters) ⇒ Object
- #add_helper(name, helper) ⇒ Object
- #add_library(name, script: nil, path: nil) ⇒ Object
- #add_snippet(name, script: nil, path: nil) ⇒ Object
- #debug ⇒ Object
- #debug_register_scripts ⇒ Object
-
#dirty? ⇒ Boolean
not currently used.
-
#initialize ⇒ HandlebarsSnapshot
constructor
A new instance of HandlebarsSnapshot.
- #new_context ⇒ Object
- #register_helper(name) ⇒ Object
- #script ⇒ Object
- #snapshot ⇒ Object
Constructor Details
#initialize ⇒ HandlebarsSnapshot
Returns a new instance of HandlebarsSnapshot.
14 15 16 17 |
# File 'lib/handlebarsjs/handlebars_snapshot.rb', line 14 def initialize @scripts = [] @helpers = [] end |
Instance Attribute Details
#helpers ⇒ Object (readonly)
Returns the value of attribute helpers.
12 13 14 |
# File 'lib/handlebarsjs/handlebars_snapshot.rb', line 12 def helpers @helpers end |
#scripts ⇒ Object (readonly)
Returns the value of attribute scripts.
11 12 13 |
# File 'lib/handlebarsjs/handlebars_snapshot.rb', line 11 def scripts @scripts end |
Instance Method Details
#add_callback(name, callback, safe, parameters) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/handlebarsjs/handlebars_snapshot.rb', line 37 def add_callback(name, callback, safe, parameters) add_helper_entry( name: name, helper: nil, callback: callback, safe: safe, parameters: parameters ) end |
#add_helper(name, helper) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/handlebarsjs/handlebars_snapshot.rb', line 27 def add_helper(name, helper) add_helper_entry( name: name, helper: helper, callback: helper.to_proc, safe: helper.safe, parameters: helper.parameter_names ) end |
#add_library(name, script: nil, path: nil) ⇒ Object
19 20 21 |
# File 'lib/handlebarsjs/handlebars_snapshot.rb', line 19 def add_library(name, script: nil, path: nil) add_script(name, 'library', script: script, path: path) end |
#add_snippet(name, script: nil, path: nil) ⇒ Object
23 24 25 |
# File 'lib/handlebarsjs/handlebars_snapshot.rb', line 23 def add_snippet(name, script: nil, path: nil) add_script(name, 'snippet', script: script, path: path) end |
#debug ⇒ Object
75 76 77 78 |
# File 'lib/handlebarsjs/handlebars_snapshot.rb', line 75 def debug data = { scripts: scripts, helpers: helpers } log.structure(data) end |
#debug_register_scripts ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/handlebarsjs/handlebars_snapshot.rb', line 80 def debug_register_scripts register_scripts = helpers.map do |helper| # Handlebarsjs::Handlebars.register_safe_string_helper_script( # helper[:name] # ) # helper[:helper].build_register_helper_script( # helper[:name], # safe: helper[:safe] # ) end puts register_scripts.join("\n") end |
#dirty? ⇒ Boolean
not currently used
71 72 73 |
# File 'lib/handlebarsjs/handlebars_snapshot.rb', line 71 def dirty? @snapshot.nil? end |
#new_context ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/handlebarsjs/handlebars_snapshot.rb', line 59 def new_context context = MiniRacer::Context.new(snapshot: snapshot) helpers.each do |helper_entry| attach_ruby(context, helper_entry) eval_register_helper(context, helper_entry) end context end |
#register_helper(name) ⇒ Object
47 48 49 |
# File 'lib/handlebarsjs/handlebars_snapshot.rb', line 47 def register_helper(name) add_script(name, 'helper', script: "Handlebars.registerHelper('#{name}', ruby_#{name})") end |
#script ⇒ Object
51 52 53 |
# File 'lib/handlebarsjs/handlebars_snapshot.rb', line 51 def script scripts.map { |script| "// #{script[:type]} - #{script[:name]}\n#{script[:script]}" }.join("\n\n") end |
#snapshot ⇒ Object
55 56 57 |
# File 'lib/handlebarsjs/handlebars_snapshot.rb', line 55 def snapshot @snapshot ||= MiniRacer::Snapshot.new(script) end |