Class: Rack::Sprocketize::Sprocket

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_file, config) ⇒ Sprocket

Returns a new instance of Sprocket.



9
10
11
12
13
14
# File 'lib/rack/sprocketize/sprocket.rb', line 9

def initialize(source_file, config)
  @source_file = source_file
  @config      = config
  @output_path = source_file.sub /^#{Regexp.escape(config.source_path)}/, config.output_path
  @secretary   = Sprockets::Secretary.new config.sprockets.merge(:source_files => [ @source_file ])
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/rack/sprocketize/sprocket.rb', line 7

def config
  @config
end

#output_pathObject

Returns the value of attribute output_path.



7
8
9
# File 'lib/rack/sprocketize/sprocket.rb', line 7

def output_path
  @output_path
end

#secretaryObject

Returns the value of attribute secretary.



7
8
9
# File 'lib/rack/sprocketize/sprocket.rb', line 7

def secretary
  @secretary
end

#source_fileObject

Returns the value of attribute source_file.



7
8
9
# File 'lib/rack/sprocketize/sprocket.rb', line 7

def source_file
  @source_file
end

Instance Method Details

#already_compressed?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/rack/sprocketize/sprocket.rb', line 47

def already_compressed?
  ::File.basename(@source_file) =~ /(-|\.)min\.js/
end

#compress(output) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rack/sprocketize/sprocket.rb', line 16

def compress(output)
  if defined?(JSMin)
    JSMin.minify(output)
  elsif defined?(Packr)
    Packr.pack output, compression_options(:shrink_vars => true)
  elsif defined?(YUI) and defined?(YUI::JavaScriptCompressor)
    YUI::JavaScriptCompressor.new(compression_options(:munge => true)).compress(output)
  elsif defined?(Closure) and defined?(Closure::Compiler)
    Closure::Compiler.new(compression_options).compile(output)
  else
    output
  end
end

#concatObject



30
31
32
# File 'lib/rack/sprocketize/sprocket.rb', line 30

def concat
  secretary.concatenation.to_s
end

#sprocketizeObject



38
39
40
41
42
43
44
45
# File 'lib/rack/sprocketize/sprocket.rb', line 38

def sprocketize
  FileUtils.mkdir_p ::File.dirname(output_path)
  ::File.open(output_path, 'w') do |file|
    output = concat
    output = compress(output) if config.always_compress? && !already_compressed?
    file.write(output.strip)
  end
end

#stale?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/rack/sprocketize/sprocket.rb', line 34

def stale?
  !::File.exist?(output_path) || secretary.source_last_modified > ::File.mtime(output_path)
end