class JstGenerator::Engine
Attributes
files[R]
Public Class Methods
new(opts = {})
click to toggle source
# File lib/jstgenerator/engine.rb, line 6 def initialize(opts = {}) @dir_glob = opts.fetch(:dir_glob) { "**/*.hb" } @jst_path = opts.fetch(:jst_path) { "jst.js" } @files = Dir[@dir_glob] end
Public Instance Methods
generate()
click to toggle source
# File lib/jstgenerator/engine.rb, line 13 def generate @jst_contents = [jst_heading] load_source @files.each do |file| content = process_template(file) @jst_contents.push(content) end create_jst_file(@jst_contents.join("\n")) end
Private Instance Methods
base_path()
click to toggle source
# File lib/jstgenerator/engine.rb, line 47 def base_path @dir_glob.split("*").first end
create_jst_file(contents = "")
click to toggle source
# File lib/jstgenerator/engine.rb, line 25 def create_jst_file(contents = "") File.open(@jst_path, "w") { |f| f.puts contents } end
jst_heading()
click to toggle source
# File lib/jstgenerator/engine.rb, line 43 def jst_heading "window.JST = {};" end
load_source()
click to toggle source
# File lib/jstgenerator/engine.rb, line 29 def load_source @cxt = V8::Context.new base_dir = File.dirname(__FILE__) path = "#{base_dir}/../" @cxt.load("#{path}/js/#{filename}.js") end
process_template(path)
click to toggle source
# File lib/jstgenerator/engine.rb, line 36 def process_template(path) contents = IO.read(path).gsub("\n", "") contents.gsub!('"', '\"') template_name = path.gsub(base_path, "").split(".").first "window.JST[\"#{template_name}\"] = #{method}(\"#{contents}\");" end