Skip to content

Instantly share code, notes, and snippets.

@rummelonp
Created August 20, 2014 18:02
Show Gist options
  • Save rummelonp/88e6d1b0b85fdb5a7b8d to your computer and use it in GitHub Desktop.
Save rummelonp/88e6d1b0b85fdb5a7b8d to your computer and use it in GitHub Desktop.
Rails でめっちゃ雑に TypedCoffeeScript 使う
require 'open3'
module TypedCoffeeScript
class Error < StandardError
end
class NotInstalled < Error
end
class Template < Tilt::Template
self.default_mime_type = 'application/javascript'
def self.engine_initialized?
_, _, status = Open3.capture3('which tcoffee')
status.success?
end
def initialize_engine
raise NotInstalled, 'TypedCoffeeScript not installed'
end
def prepare
end
def evaluate(scope, locals, &block)
out, err, status = Open3.capture3("tcoffee -c #{file}", chdir: Dir.tmpdir)
if status.success?
filename = out.chomp.split(' ').last
IO.read(File.join(Dir.tmpdir, filename))
else
raise Error, remove_ansi_escape_sequence(err)
end
end
def allow_script?
false
end
private
def remove_ansi_escape_sequence(text)
text.gsub(/\x1B\[[0-9;]*[mK]/, '')
end
end
end
Rails.configuration.after_initialize do
Sprockets::Rails::Helper.assets.register_engine('.tcoffee', TypedCoffeeScript::Template)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment