Class: SwissKnife::Assets
- Inherits:
-
Object
- Object
- SwissKnife::Assets
- Defined in:
- lib/swiss_knife/assets.rb
Constant Summary collapse
- EXTENSIONS =
{ :javascripts => "js", :stylesheets => "css" }
Class Attribute Summary collapse
-
.configuration ⇒ Object
Returns the value of attribute configuration.
Class Method Summary collapse
- .compress_css(source) ⇒ Object
-
.compress_js(source) ⇒ Object
Taken from github.com/sbecker/asset_packager/.
- .config ⇒ Object
- .config? ⇒ Boolean
- .config_file ⇒ Object
- .export(type) ⇒ Object
- .merge? ⇒ Boolean
- .public_dir ⇒ Object
Class Attribute Details
.configuration ⇒ Object
Returns the value of attribute configuration.
4 5 6 |
# File 'lib/swiss_knife/assets.rb', line 4 def configuration @configuration end |
Class Method Details
.compress_css(source) ⇒ Object
52 53 54 |
# File 'lib/swiss_knife/assets.rb', line 52 def self.compress_css(source) source end |
.compress_js(source) ⇒ Object
Taken from github.com/sbecker/asset_packager/
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/swiss_knife/assets.rb', line 30 def self.compress_js(source) jsmin_path = File.dirname(__FILE__) tmp_path = Rails.root.join("tmp/js") # write out to a temp file File.open("#{tmp_path}_uncompressed.js", "w") {|f| f.write(source) } # compress file with JSMin library `ruby #{jsmin_path}/jsmin.rb <#{tmp_path}_uncompressed.js >#{tmp_path}_compressed.js \n` # read it back in and trim it result = "" File.open("#{tmp_path}_compressed.js", "r") { |f| result += f.read.strip } result << "\n;" # delete temp files if they exist File.delete("#{tmp_path}_uncompressed.js") if File.exists?("#{tmp_path}_uncompressed.js") File.delete("#{tmp_path}_compressed.js") if File.exists?("#{tmp_path}_compressed.js") result end |
.config ⇒ Object
13 14 15 |
# File 'lib/swiss_knife/assets.rb', line 13 def self.config @config ||= YAML.load_file(config_file) end |
.config? ⇒ Boolean
21 22 23 |
# File 'lib/swiss_knife/assets.rb', line 21 def self.config? File.file?(config_file) && config end |
.config_file ⇒ Object
9 10 11 |
# File 'lib/swiss_knife/assets.rb', line 9 def self.config_file Rails.root.join("config/assets.yml") end |
.export(type) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/swiss_knife/assets.rb', line 56 def self.export(type) assets_dir = public_dir.join(type.to_s) ext = EXTENSIONS[type] group = config[type.to_s] return unless group group.each do |name, files| File.open(assets_dir.join("#{name}_packaged.#{ext}"), "w+") do |f| files.each do |file| file << ".#{ext}" unless file =~ /\.#{ext}$/i f << send("compress_#{ext}", File.read(assets_dir.join(file))) end end end end |
.merge? ⇒ Boolean
25 26 27 |
# File 'lib/swiss_knife/assets.rb', line 25 def self.merge? config? && Rails.env.production? end |
.public_dir ⇒ Object
17 18 19 |
# File 'lib/swiss_knife/assets.rb', line 17 def self.public_dir Rails.root.join("public") end |