Sha256: 747ce1399b9e162013e386453218b207ed7c897923ed49d8b9faea9a20ca1b8f

Contents?: true

Size: 1.71 KB

Versions: 5

Compression:

Stored size: 1.71 KB

Contents

require 'rack/utils'
require 'rack/mime'
require 'dassets/source_proxy'

module Dassets; end
class Dassets::AssetFile

  attr_reader :digest_path, :dirname, :extname, :basename, :source_proxy

  def initialize(digest_path)
    @digest_path  = digest_path
    @dirname      = File.dirname(@digest_path)
    @extname      = File.extname(@digest_path)
    @basename     = File.basename(@digest_path, @extname)
    @source_proxy = Dassets::SourceProxy.new(@digest_path, {
      :content_cache     => Dassets.config.content_cache,
      :fingerprint_cache => Dassets.config.fingerprint_cache
    })
  end

  def digest!
    return if !self.exists?
    Dassets.config.file_store.save(self.url){ self.content }
  end

  def url
    path_basename = "#{@basename}-#{self.fingerprint}#{@extname}"
    path = File.join(@dirname, path_basename).sub(/^\.\//, '').sub(/^\//, '')
    "#{dassets_base_url}/#{path}"
  end

  alias_method :href, :url

  def fingerprint
    return nil if !self.exists?
    @source_proxy.fingerprint
  end

  def content
    return nil if !self.exists?
    @source_proxy.content
  end

  def mtime
    return nil if !self.exists?
    @source_proxy.mtime.httpdate
  end

  def size
    return nil if !self.exists?
    Rack::Utils.bytesize(self.content)
  end

  def mime_type
    return nil if !self.exists?
    Rack::Mime.mime_type(@extname)
  end

  def response_headers
    @source_proxy.response_headers
  end

  def exists?
    @source_proxy.exists?
  end

  def ==(other_asset_file)
    other_asset_file.kind_of?(Dassets::AssetFile) &&
    self.digest_path == other_asset_file.digest_path &&
    self.fingerprint == other_asset_file.fingerprint
  end

  private

  def dassets_base_url
    Dassets.config.base_url.to_s
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dassets-0.14.5 lib/dassets/asset_file.rb
dassets-0.14.4 lib/dassets/asset_file.rb
dassets-0.14.3 lib/dassets/asset_file.rb
dassets-0.14.2 lib/dassets/asset_file.rb
dassets-0.14.1 lib/dassets/asset_file.rb