class Fuse::Document::Asset

Constants

TYPES

Attributes

path[R]

Public Class Methods

[](dir) click to toggle source
# File lib/fuse/document/asset.rb, line 7
def [](dir)
  assets = Fuse::Document::AssetCollection.new
  Dir[File.join dir, '**/*.*'].each do |full_path|
    asset = self.for(full_path[dir.length..-1], dir)
    assets << asset if asset
  end
  assets
end
const_missing(name) click to toggle source
# File lib/fuse/document/asset_types.rb, line 12
def self.const_missing(name)
  (@anonymous_subclasses ||= {})[name] ||= Class.new(self)
end
for(path, root = Dir.pwd) click to toggle source
# File lib/fuse/document/asset.rb, line 16
def for(path, root = Dir.pwd)
  full_path = File.join root, path
  return unless File.exists? full_path
  type = TYPES[(File.extname(path)[1..-1] || '').to_sym]
  (@cache ||= {})[File.expand_path full_path] ||= type.new(path, root) if type
end
new(path, root) click to toggle source
# File lib/fuse/document/asset.rb, line 27
def initialize(path, root)
  @root = root
  @path = path
end

Public Instance Methods

call(env) click to toggle source
# File lib/fuse/document/asset.rb, line 52
def call(env)
  if filter?
    body = filter
    [200, {'Content-Type' => type, 'Content-Length' => body.length.to_s}, [body]]
  else
    Rack::File.new(@root).call(env)
  end
end
extension() click to toggle source
# File lib/fuse/document/asset.rb, line 72
def extension
  @extension ||= File.extname(path).downcase[1..-1]
end
filter?() click to toggle source
# File lib/fuse/document/asset.rb, line 48
def filter?
  respond_to? :filter
end
filtered() click to toggle source
# File lib/fuse/document/asset.rb, line 44
def filtered
  filter? ? filter : raw
end
full_path() click to toggle source
# File lib/fuse/document/asset.rb, line 32
def full_path
  @full_path ||= File.join @root, @path
end
raw() click to toggle source
# File lib/fuse/document/asset.rb, line 40
def raw
  File.open(full_path, 'rb') { |f| f.read }
end
relative_path() click to toggle source
# File lib/fuse/document/asset.rb, line 36
def relative_path
  @relative_path ||= path.sub(%r`^[\\/]`, '')
end
to_datauri(compressed = false) click to toggle source
# File lib/fuse/document/asset.rb, line 65
def to_datauri(compressed = false)
  'data:%s;base64,%s' % [
      type,
      Base64.strict_encode64(compressed && respond_to?(:compress) ? compress : raw)
  ]
end
type() click to toggle source
# File lib/fuse/document/asset.rb, line 61
def type
  @type ||= Rack::Mime.mime_type('.' + extension)
end