Class: Pinion::CompiledAsset

Inherits:
Asset
  • Object
show all
Defined in:
lib/pinion/compiled_asset.rb

Instance Attribute Summary collapse

Attributes inherited from Asset

#checksum, #extension, #length, #mtime

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Asset

[], #content_type, #each, find_file, find_source_file_and_conversion, find_uncached_asset, static, watch_path

Constructor Details

#initialize(uncompiled_path, conversion) ⇒ CompiledAsset



9
10
11
12
13
14
15
16
17
# File 'lib/pinion/compiled_asset.rb', line 9

def initialize(uncompiled_path, conversion)
  @from_type = conversion.from_type
  @to_type = conversion.to_type
  @compiled_contents = conversion.convert(File.read(uncompiled_path))
  @length = Rack::Utils.bytesize(@compiled_contents)
  @mtime = latest_mtime
  @extension = @to_type.to_s
  @checksum = Digest::MD5.hexdigest(@compiled_contents)
end

Instance Attribute Details

#from_typeObject (readonly)

Returns the value of attribute from_type.



7
8
9
# File 'lib/pinion/compiled_asset.rb', line 7

def from_type
  @from_type
end

Class Method Details

.glob(pattern, &block) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/pinion/compiled_asset.rb', line 32

def self.glob(pattern, &block)
  enumerator = Enumerator.new do |yielder|
    Asset.watch_directories.each do |directory|
      Dir.glob(File.join(directory, pattern)) { |filename| yielder.yield filename }
    end
  end
  enumerator.each(&block)
end

.sanitize_for_glob(pattern) ⇒ Object



30
# File 'lib/pinion/compiled_asset.rb', line 30

def self.sanitize_for_glob(pattern) pattern.gsub(/[\*\?\[\]\{\}]/) { |match| "\\#{match}" } end

Instance Method Details

#contentsObject



19
# File 'lib/pinion/compiled_asset.rb', line 19

def contents() @compiled_contents end

#invalidateObject



26
27
28
# File 'lib/pinion/compiled_asset.rb', line 26

def invalidate
  Asset.cached_assets.delete_if { |_, asset| asset.is_a?(CompiledAsset) && asset.from_type == @from_type }
end

#latest_mtimeObject



21
22
23
24
# File 'lib/pinion/compiled_asset.rb', line 21

def latest_mtime
  pattern = "**/*#{self.class.sanitize_for_glob(".#{@from_type}")}"
  self.class.glob(pattern).reduce(Time.at(0)) { |latest, path| [latest, File.stat(path).mtime].max }
end