Class: JS2::Rack

Inherits:
Object
  • Object
show all
Defined in:
lib/js2/rack.rb

Overview

this is a hack for now until I can get v8 stable

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Rack

Returns a new instance of Rack.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/js2/rack.rb', line 11

def initialize(app)
  @app = app

  default = {
    'source_dir'  => "#{get_root}/app/js2",
    'target_dir'  => "#{get_root}/public/javascripts",
    'bin'         => (`which js2`.chomp rescue nil),
    'copy_js2'    => true
  }


  config = YAML.load_file(get_root + '/config/js2.yml') rescue default

  @source_dir = config['source_dir'] || './app/js2'
  @target_dir = config['target_dir'] || './public/javascripts'
  @bin        = config['bin'] || 'js2'
  @copy_js2   = config.has_key?('copy_js2') ? config['copy_js2'] : true

  @valid = @source_dir && @target_dir && !@bin.blank?

  unless @valid
    puts "JS2 is not configured properly"
  end
end

Instance Method Details

#call(env) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/js2/rack.rb', line 36

def call(env)
  if @copy_js2
    to_file = "#{get_root}/public/javascripts/js2.js"
    unless File.exists?(to_file) 
      from_file = JS2::ROOT + '/js2/browser.js'
      puts "--- #{get_root}"
      puts "#{from_file} #{to_file}"
      File.open(to_file, 'w') { |f| f << File.read(from_file) }
    end
  end

  `#{@bin} compile -f=browser -m #{@source_dir} #{@target_dir}` if @valid
  @app.call(env)
end

#get_rootObject



6
7
8
9
# File 'lib/js2/rack.rb', line 6

def get_root 
  @root ||= defined?(Rails) ? Rails.root : File.expand_path(Dir.getwd)
  return @root
end