Sha256: 9d0e1d6dd015247479d5a0efdc6a3282215291edbbf078b0944dde7861123d63

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

require 'open-uri'
require 'v8'
module JstGenerator
  class Engine
    attr_reader :files
    def initialize(opts = {})
      @dir_glob = opts.fetch(:dir_glob) { "**/*.hb" }
      @jst_path = opts.fetch(:jst_path) { "jst.js" }
      @files = Dir[@dir_glob]
    end


    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

    def create_jst_file(contents = "")
      File.open(@jst_path, "w") { |f| f.puts contents }
    end

    def load_source
      @cxt = V8::Context.new
      base_dir = File.dirname(__FILE__)
      path = "#{base_dir}/../"
      @cxt.load("#{path}/js/#{filename}.js")
    end

    def process_template(path)
      contents = IO.read(path).gsub("\n", "")
      template_name = path.gsub(base_path, "").split(".").first
      "window.JST[\"#{template_name}\"] = #{method}(\"#{contents}\");"
    end

    def jst_heading
      "window.JST = {};"
    end

    def base_path
      @dir_glob.split("*").first
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jstgenerator-0.1.0 lib/jstgenerator/engine.rb
jstgenerator-0.0.1 lib/jstgenerator/engine.rb