class Pinion::CompiledAsset

Attributes

from_type[R]

Public Class Methods

glob(pattern, &block) click to toggle source
# 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
new(uncompiled_path, conversion) click to toggle source
# 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
sanitize_for_glob(pattern) click to toggle source
# File lib/pinion/compiled_asset.rb, line 30
def self.sanitize_for_glob(pattern) pattern.gsub(/[\*\?\[\]\{\}]/) { |match| "\\#{match}" } end

Public Instance Methods

contents() click to toggle source
# File lib/pinion/compiled_asset.rb, line 19
def contents() @compiled_contents end
invalidate() click to toggle source
# 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_mtime() click to toggle source
# 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