class Pinion::Asset

Attributes

cached_assets[R]
watch_directories[R]
checksum[R]

Asset methods

extension[R]

Asset methods

length[R]

Asset methods

mtime[R]

Asset methods

Public Class Methods

[](to_path) click to toggle source

Look up an asset by its path. It may be returned from cache.

# File lib/pinion/asset.rb, line 72
def self.[](to_path)
  asset = @cached_assets[to_path]
  if asset
    return asset if static
    mtime = asset.mtime
    latest = asset.latest_mtime
    if latest > mtime
      asset.invalidate
      return self[to_path]
    end
  else
    begin
      asset = find_uncached_asset(to_path)
    rescue Error => error
      STDERR.puts "Warning: #{error.message}"
      return nil
    end
    @cached_assets[to_path] = asset
  end
  asset
end
find_file(path) click to toggle source

Find a particular file in the watched directories.

# File lib/pinion/asset.rb, line 53
def self.find_file(path)
  return @cached_files[path] if (static && @cached_files.include?(path))
  result = nil
  @watch_directories.each do |directory|
    filename = File.join(directory, path)
    if File.file? filename
      result = filename
      break
    end
  end
  @cached_files[path] = result if static
  result
end
find_source_file_and_conversion(to_path) click to toggle source
# File lib/pinion/asset.rb, line 103
def self.find_source_file_and_conversion(to_path)
  path, dot, suffix = to_path.rpartition(".")
  conversions = Conversion.conversions_for(suffix.to_sym)
  raise Error, "No conversion for for #{to_path}" if conversions.empty?
  conversions.each do |conversion|
    filename = "#{path}.#{conversion.from_type}"
    from_path = find_file(filename)
    return [from_path, conversion] if from_path
  end
  raise Error, "No source file found for #{to_path}"
end
find_uncached_asset(to_path) click to toggle source
# File lib/pinion/asset.rb, line 94
def self.find_uncached_asset(to_path)
  real_file = find_file(to_path)
  return StaticAsset.new(to_path, real_file) if real_file
  from_path, conversion = find_source_file_and_conversion(to_path)
  # If we reach this point we've found the asset we're going to compile
  # TODO: log at info: compiling asset ...
  CompiledAsset.new from_path, conversion
end
new() click to toggle source
# File lib/pinion/asset.rb, line 27
def initialize() raise "subclass me" end
static() click to toggle source

In production mode, assume that the files won’t change on the filesystem. This means we can always serve them from cache (if cached).

# File lib/pinion/asset.rb, line 43
def self.static() Pinion.environment == "production" end
watch_path(path) click to toggle source

Add a path to the set of asset paths.

# File lib/pinion/asset.rb, line 50
def self.watch_path(path) @watch_directories << File.join(".", path) end

Public Instance Methods

content_type() click to toggle source
# File lib/pinion/asset.rb, line 39
def content_type() Rack::Mime::MIME_TYPES[".#{@extension}"] || "application/octet-stream" end
contents() click to toggle source
# File lib/pinion/asset.rb, line 28
def contents() raise "Implement me" end
each() { |contents end| ... } click to toggle source

Allow the Asset to be served as a rack response body

# File lib/pinion/asset.rb, line 37
  def each() yield contents end

  def content_type() Rack::Mime::MIME_TYPES[".#{@extension}"] || "application/octet-stream" end

  # In production mode, assume that the files won't change on the filesystem. This means we can always serve
  # them from cache (if cached).
  def self.static() Pinion.environment == "production" end

  #
  # File watcher class methods
  #

  # Add a path to the set of asset paths.
  def self.watch_path(path) @watch_directories << File.join(".", path) end

  # Find a particular file in the watched directories.
  def self.find_file(path)
    return @cached_files[path] if (static && @cached_files.include?(path))
    result = nil
    @watch_directories.each do |directory|
      filename = File.join(directory, path)
      if File.file? filename
        result = filename
        break
      end
    end
    @cached_files[path] = result if static
    result
  end

  #
  # Asset search methods
  #

  # Look up an asset by its path. It may be returned from cache.
  def self.[](to_path)
    asset = @cached_assets[to_path]
    if asset
      return asset if static
      mtime = asset.mtime
      latest = asset.latest_mtime
      if latest > mtime
        asset.invalidate
        return self[to_path]
      end
    else
      begin
        asset = find_uncached_asset(to_path)
      rescue Error => error
        STDERR.puts "Warning: #{error.message}"
        return nil
      end
      @cached_assets[to_path] = asset
    end
    asset
  end

  def self.find_uncached_asset(to_path)
    real_file = find_file(to_path)
    return StaticAsset.new(to_path, real_file) if real_file
    from_path, conversion = find_source_file_and_conversion(to_path)
    # If we reach this point we've found the asset we're going to compile
    # TODO: log at info: compiling asset ...
    CompiledAsset.new from_path, conversion
  end

  def self.find_source_file_and_conversion(to_path)
    path, dot, suffix = to_path.rpartition(".")
    conversions = Conversion.conversions_for(suffix.to_sym)
    raise Error, "No conversion for for #{to_path}" if conversions.empty?
    conversions.each do |conversion|
      filename = "#{path}.#{conversion.from_type}"
      from_path = find_file(filename)
      return [from_path, conversion] if from_path
    end
    raise Error, "No source file found for #{to_path}"
  end
end
invalidate() click to toggle source

Invalidate this asset (and possibly others it depends on)

# File lib/pinion/asset.rb, line 34
def invalidate() raise "Implement me" end
latest_mtime() click to toggle source

The latest mtime of this asset. For compiled assets, this the latest mtime of all files of this type.

# File lib/pinion/asset.rb, line 31
def latest_mtime() raise "Implement me" end