Class: Pinion::Asset

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

Direct Known Subclasses

Bundle, CompiledAsset, StaticAsset

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAsset



27
# File 'lib/pinion/asset.rb', line 27

def initialize() raise "subclass me" end

Class Attribute Details

.cached_assetsObject (readonly)

Returns the value of attribute cached_assets.



11
12
13
# File 'lib/pinion/asset.rb', line 11

def cached_assets
  @cached_assets
end

.watch_directoriesObject (readonly)

Returns the value of attribute watch_directories.



11
12
13
# File 'lib/pinion/asset.rb', line 11

def watch_directories
  @watch_directories
end

Instance Attribute Details

#checksumObject (readonly)

Asset methods



25
26
27
# File 'lib/pinion/asset.rb', line 25

def checksum
  @checksum
end

#extensionObject (readonly)

Asset methods



25
26
27
# File 'lib/pinion/asset.rb', line 25

def extension
  @extension
end

#lengthObject (readonly)

Asset methods



25
26
27
# File 'lib/pinion/asset.rb', line 25

def length
  @length
end

#mtimeObject (readonly)

Asset methods



25
26
27
# File 'lib/pinion/asset.rb', line 25

def mtime
  @mtime
end

Class Method Details

.[](to_path) ⇒ Object

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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) ⇒ Object

Find a particular file in the watched directories.



53
54
55
56
57
58
59
60
61
62
63
64
65
# 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) ⇒ Object

Raises:



103
104
105
106
107
108
109
110
111
112
113
# 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) ⇒ Object



94
95
96
97
98
99
100
101
# 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

.staticObject

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



43
# File 'lib/pinion/asset.rb', line 43

def self.static() Pinion.environment == "production" end

.watch_path(path) ⇒ Object

Add a path to the set of asset paths.



50
# File 'lib/pinion/asset.rb', line 50

def self.watch_path(path) @watch_directories << File.join(".", path) end

Instance Method Details

#content_typeObject



39
# File 'lib/pinion/asset.rb', line 39

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

#contentsObject



28
# File 'lib/pinion/asset.rb', line 28

def contents() raise "Implement me" end

#each {|contents| ... } ⇒ Object

Allow the Asset to be served as a rack response body

Yields:



37
# File 'lib/pinion/asset.rb', line 37

def each() yield contents end

#invalidateObject

Invalidate this asset (and possibly others it depends on)



34
# File 'lib/pinion/asset.rb', line 34

def invalidate() raise "Implement me" end

#latest_mtimeObject

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



31
# File 'lib/pinion/asset.rb', line 31

def latest_mtime() raise "Implement me" end