Class: Rack::Pack::Package

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

Direct Known Subclasses

Javascript, Stylesheet

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output_file, source_files) ⇒ Package

Returns a new instance of Package.



24
25
26
27
28
29
30
31
# File 'lib/rack/pack/package.rb', line 24

def initialize(output_file, source_files)
  @file = to_pathname(output_file)
  @from = if source_files.is_a?(Array)
    source_files.map { |file| to_pathname(file) }
  else
    source_files.to_s
  end
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



22
23
24
# File 'lib/rack/pack/package.rb', line 22

def file
  @file
end

Class Method Details

.[](file) ⇒ Object



16
17
18
19
# File 'lib/rack/pack/package.rb', line 16

def [](file)
  ext = ::File.extname(file.to_s).sub(/^\./, '').downcase
  mappings[ext] || self
end

.mappingsObject



7
8
9
# File 'lib/rack/pack/package.rb', line 7

def mappings
  @package_mappings ||= {}
end

.register(ext, package_class) ⇒ Object



11
12
13
14
# File 'lib/rack/pack/package.rb', line 11

def register(ext, package_class)
  ext = ext.to_s.sub(/^\./, '').downcase
  mappings[ext] = package_class
end

Instance Method Details

#compileObject



40
41
42
43
44
# File 'lib/rack/pack/package.rb', line 40

def compile
  compiled = source_files.map(&:read).join
  compiled = compress(compiled) if compress?
  compiled.strip
end

#compress(source) ⇒ Object



46
47
48
# File 'lib/rack/pack/package.rb', line 46

def compress(source)
  source
end

#compress?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/rack/pack/package.rb', line 59

def compress?
  Pack.production? || Pack.options && Pack.options[:always_compress]
end

#source_filesObject



55
56
57
# File 'lib/rack/pack/package.rb', line 55

def source_files
  @source_files ||= @from.is_a?(Array) ? @from : Pathname.glob(@from)
end

#stale?Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/rack/pack/package.rb', line 50

def stale?
  @source_files = nil
  source_files? && (file_missing? || source_files_newer?)
end

#updateObject



33
34
35
36
37
38
# File 'lib/rack/pack/package.rb', line 33

def update
  file.dirname.mkpath
  file.open('w') do |file|
    file << compile
  end
end