Class: Minit

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

Instance Method Summary collapse

Instance Method Details

#compress?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/minit.rb', line 32

def compress?
  !%w(development test).include?(Rails.env)
end

#include_javascripts(create = false) ⇒ Object



40
41
42
# File 'lib/minit.rb', line 40

def include_javascripts create = false
  package :javascripts, %w(lib/**/*.js plugins/**/*.js **/*.js), create
end

#include_stylesheets(create = false) ⇒ Object



36
37
38
# File 'lib/minit.rb', line 36

def include_stylesheets create = false
  package :stylesheets, %w(reset.css default.css application.css lib/**/*.css **/*.css), create
end

#package(type, globs, create) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/minit.rb', line 5

def package type, globs, create
  compressor = {:javascripts => JSMin, :stylesheets => CSSMin}[type]
  extension = {:javascripts => 'js', :stylesheets => 'css'}[type]
  include_method = {:javascripts => 'javascript_include_tag', :stylesheets => 'stylesheet_link_tag'}[type]
  raise "package type unknown. Only :javascripts or :stylesheets supported." unless compressor && extension && include_method
  package_name = "packaged.#{extension}"
  return send(include_method, File.join('/assets', package_name)) if compress? && !create

  files = []
  includes = []
  asset_path = File.join(Rails.root, 'public', type.to_s, '/')

  file = File.open(File.join(Rails.root, 'public', 'assets', package_name), 'w') if (compress? && create)
  globs.each do |glob|
    Dir.glob(File.join(asset_path, glob)).each do |path|
      next if files.include?(path)
      files << path
      if compress? && create
        file.puts compressor.minify(File.read(path))
      elsif !create
        includes << path.gsub(asset_path, '')
      end
    end
  end
  file.close if file
end