Class: Rack::Sprocketize::Sprocket
- Inherits:
-
Object
- Object
- Rack::Sprocketize::Sprocket
- Defined in:
- lib/rack/sprocketize/sprocket.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#output_path ⇒ Object
Returns the value of attribute output_path.
-
#secretary ⇒ Object
Returns the value of attribute secretary.
-
#source_file ⇒ Object
Returns the value of attribute source_file.
Instance Method Summary collapse
- #already_compressed? ⇒ Boolean
- #compress(output) ⇒ Object
- #concat ⇒ Object
-
#initialize(source_file, config) ⇒ Sprocket
constructor
A new instance of Sprocket.
- #sprocketize ⇒ Object
- #stale? ⇒ Boolean
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
#config ⇒ Object
Returns the value of attribute config.
7 8 9 |
# File 'lib/rack/sprocketize/sprocket.rb', line 7 def config @config end |
#output_path ⇒ Object
Returns the value of attribute output_path.
7 8 9 |
# File 'lib/rack/sprocketize/sprocket.rb', line 7 def output_path @output_path end |
#secretary ⇒ Object
Returns the value of attribute secretary.
7 8 9 |
# File 'lib/rack/sprocketize/sprocket.rb', line 7 def secretary @secretary end |
#source_file ⇒ Object
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
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, (:shrink_vars => true) elsif defined?(YUI) and defined?(YUI::JavaScriptCompressor) YUI::JavaScriptCompressor.new((:munge => true)).compress(output) elsif defined?(Closure) and defined?(Closure::Compiler) Closure::Compiler.new().compile(output) else output end end |
#concat ⇒ Object
30 31 32 |
# File 'lib/rack/sprocketize/sprocket.rb', line 30 def concat secretary.concatenation.to_s end |
#sprocketize ⇒ Object
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
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 |