Class: TDiary::Rack::Assets::Precompile

Inherits:
Object
  • Object
show all
Defined in:
lib/tdiary/rack/assets/precompile.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, environment = nil) ⇒ Precompile

Returns a new instance of Precompile.



9
10
11
12
# File 'lib/tdiary/rack/assets/precompile.rb', line 9

def initialize(app, environment = nil)
  @app = app
  @environment = environment
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tdiary/rack/assets/precompile.rb', line 14

def call( env )
  @environment.each_file do |script|
    next unless script.to_s =~ /\.coffee\z/
    js_path = Pathname.new(script.to_s.gsub(/\.coffee\z/, '.js'))

    if !FileTest.exist?(js_path) || FileUtils.uptodate?(script, [js_path])
      File.open(js_path, 'w') do |js|
        js.write CoffeeScript.compile(File.read(script))
      end
    end
  end if @environment
  @app.call( env )
end