class Sinatra::Exstatic::Asset

Encapsulates an asset, be it a stylesheet, an image…

Constants

URI_PATTERN

We only need to check for a scheme/protocol to know it's not a file.

Attributes

fullpath[R]

Public Class Methods

new( filename, asset_dir=nil, timestamp_format=nil ) click to toggle source

@param [String] filename Either the file name (and path relative to the public folder) or the external HTTP link. @param [String] asset_dir The asset directory. When used with Sinatra this will default to the directory defined by the `public_folder` setting.

Calls superclass method
# File lib/sinatra/exstatic_assets.rb, line 67
def initialize( filename, asset_dir=nil, timestamp_format=nil ) # TODO failure strategy
  if asset_dir.nil?
    # TODO should this strip the leading slash?
    filename, asset_dir = [File.basename(filename), File.dirname(filename)]
  end
  # TODO fail if asset_dir.nil?
  super filename
  @timestamp_format = timestamp_format
  @fullpath = File.join( asset_dir, filename ) unless is_uri?
end

Public Instance Methods

exists?() click to toggle source

Convenience, mainly for my convenience in stubbing during tests. @return [TrueClass]

# File lib/sinatra/exstatic_assets.rb, line 115
def exists?
  File.exists? fullpath
end
is_uri?() click to toggle source

Tests whether the asset is a file or an HTTP link by checking the scheme portion. @note

A url will match:
http://example.com
//example.com
but www.example.com or example.com will be treated as a file.

@return [TrueClass]

# File lib/sinatra/exstatic_assets.rb, line 108
def is_uri?
  self =~ URI_PATTERN ? true : false
end
mtime_int() click to toggle source

Convenience, mainly for my convenience in stubbing during tests. @return [Int]

# File lib/sinatra/exstatic_assets.rb, line 122
def mtime_int
  File.mtime( fullpath ).to_i
end
querystring() click to toggle source

Takes the timestamp and returns it as a querystring. @return [String] `?ts=TIMESTAMP`

# File lib/sinatra/exstatic_assets.rb, line 87
def querystring
  timestamp ? "?ts=#{timestamp}" : nil
end
timestamp() click to toggle source

If the asset is a local file this gets the timestamp. @return [Integer]

# File lib/sinatra/exstatic_assets.rb, line 81
def timestamp
  @timestamp ||= !is_uri? && exists? && send(@timestamp_format)
end