Sha256: 7b3be398ac1c2b01d518f85332f841f6d76e21ae94b9a4c2ccea0767796e1b45

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

require 'digest/sha2'

module Barista
  class Compiler
    
    class << self; attr_accessor :bin_path; end
    self.bin_path ||= "coffee"
    
    def self.compile(content)
      new(content).to_js
    end
    
    def initialize(content)
      @compiled = false
      @content  = content
    end
    
    def compile!
      # Compiler code thanks to bistro_car.
      tf = temp_file_for_content
      @compiled_content = invoke_coffee(temp_file_for_content.path)
      @compiled = true
    ensure
      tf.unlink rescue nil
    end
    
    def to_js
      compile! unless @compiled
      @compiled_content.to_s
    end
    
    protected
    
    def coffee_options
      "-p"
    end
    
    def temp_file_for_content
      tf = Tempfile.new("barista-#{content_hash}.coffee")
      tf.write @content
      tf.close
      tf
    end
    
    def invoke_coffee(path)
      command = "#{self.class.bin_path} #{coffee_options} '#{path}'"
      %x(#{command})
    end
    
    def content_hash
      @content_hash ||= Digest::SHA256.hexdigest(@content)
    end
    
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
barista-0.1.1 lib/barista/compiler.rb
barista-0.1.0 lib/barista/compiler.rb