Class Juicer::Asset::Path
In: lib/juicer/asset/path.rb
Parent: Object

Assets are files used by CSS and JavaScript files. The Asset class provides tools for manipulating asset paths, such as rebasing, adding cache busters, and cycling asset hosts.

Asset::Path objects are most commonly created by Juicer::Asset::PathResolver#resolve which resolves include paths to file names. It is possible, however, to use the asset class directly:

  Dir.pwd
  #=> "/home/christian/projects/mysite/design/css"

  asset = Juicer::Asset::Path.new "../images/logo.png"
  asset.path
  #=> "../images/logo.png"

  asset.rebase("~/projects/mysite/design").path
  #=> "images/logo.png"

  asset.filename
  #=> "/home/christian/projects/mysite/design/images/logo.png"

  asset.path(:cache_buster_type => :soft)
  #=> "../images/logo.png?jcb=1234567890"

  asset.path(:cache_buster_type => :soft, :cache_buster => nil)
  #=> "../images/logo.png?1234567890"

  asset.path(:cache_buster => "bustIT")
  #=> "../images/logo.png?bustIT=1234567890"

  asset = Juicer::Asset::Path.new "../images/logo.png", :document_root
  #=> "/home/christian/projects/mysite"

  asset.absolute_path(:cache_buster_type => :hard)
  #=> "/images/logo-jcb1234567890.png"

  asset.absolute_path(:host => "http://localhost")
  #=> "http://localhost/images/logo.png"

  asset.absolute_path(:host => "http://localhost", :cache_buster_type => :hard)
  #=> "http://localhost/images/logo-jcb1234567890.png"
Author:Christian Johansen (christian@cjohansen.no)
Copyright:Copyright (c) 2009 Christian Johansen
License:BSD

Methods

Attributes

base  [R]  Base directory to resolve relative path from, see Juicer::Asset::Path#initialize
document_root  [R]  Directory served as root through a web server, see Juicer::Asset::Path#initialize
hosts  [R]  Hosts served from :document_root, see Juicer::Asset::Path#initialize

Public Class methods

Assures that a host has scheme/protocol and no trailing slash

Accepts a single host, or an array of hosts and returns an array of hosts that include scheme/protocol, and don‘t have trailing slash.

Initialize asset at path. Accepts an optional hash of options:

:base
Base context from which asset is required. Given a path of ../images/logo.png and a :base of /project/design/css, the asset file will be assumed to live in /project/design/images/logo.png Defaults to the current directory.
:hosts
Array of host names that are served from :document_root. May also include scheme/protocol. If not, http is assumed.
:document_root
The root directory for absolute URLs (ie, the server‘s document root). This option is needed when resolving absolute URLs that include a hostname as well as when generating absolute paths.

Public Instance methods

Returns absolute path calculated using the document_root. Optionally accepts a hash of options:

:host
Return fully qualified URL with this host name. May include scheme/protocol. Default scheme is http.
:cache_buster
The parameter name for the cache buster.
:cache_buster_type
The kind of cache buster to add, :soft or :hard.

A cache buster will be added if either (or both) of the :cache_buster or :cache_buster_type options are provided. The default cache buster type is :soft.

Raises an ArgumentException if no document_root has been set.

Returns basename of filename on disk

Returns basename of filename on disk

Returns true if file exists on disk

Return filename on disk. Requires the document_root to be set if original path was an absolute one.

If asset path includes scheme/protocol and host, it can only be resolved if a match is found in hosts. Otherwise, an exeception is raised.

Returns the original path.

Accepts an optional hash of options for cache busters:

:cache_buster
The parameter name for the cache buster.
:cache_buster_type
The kind of cache buster to add, :soft or :hard.

A cache buster will be added if either (or both) of the :cache_buster or :cache_buster_type options are provided. The default cache buster type is :soft.

Rebase path and return a new Asset::Path object.

  asset = Juicer::Asset::Path.new "../images/logo.png", :base => "/var/www/public/stylesheets"
  asset2 = asset.rebase("/var/www/public")
  asset2.relative_path #=> "images/logo.png"

Return path relative to base

Accepts an optional hash of options for cache busters:

:cache_buster
The parameter name for the cache buster.
:cache_buster_type
The kind of cache buster to add, :soft or :hard.

A cache buster will be added if either (or both) of the :cache_buster or :cache_buster_type options are provided. The default cache buster type is :soft.

[Validate]