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
45
# File 'lib/rack/pack/package.rb', line 40

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

#compress(source) ⇒ Object



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

def compress(source)
  source
end

#compress?Boolean

Returns:

  • (Boolean)


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

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

#source_filesObject



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

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

#stale?Boolean

Returns:

  • (Boolean)


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

def stale?
  @source_files = nil
  source_files? && (file_missing? || source_files_added_or_removed? || 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