class Rapper::Definition

Basic definition abstraction to make working with the wacky YAML structure easier.

Constants

COMBINATION_PREFIX

Public Class Methods

new( path ) click to toggle source
# File lib/rapper/definition.rb, line 8
def initialize( path )
  @path = path
  @type = File.basename( path, ".yml" )
  @definition = YAML.load_file( @path )
  # Create asset destination folder, if needed
  Dir.mkdir( destination_root ) unless File.directory?( destination_root )
end

Public Instance Methods

asset_path( name, root=nil ) click to toggle source

@param [String] name The asset’s name.

@return [String] Path to the packaged asset file.

# File lib/rapper/definition.rb, line 89
def asset_path( name, root=nil )
  root ||= self.destination_root
  file_name = "#{name}.#{self.suffix}"
  File.join( root, file_name )
end
asset_tag_root() click to toggle source

@return [String] The public url root for packaged asset files.

# File lib/rapper/definition.rb, line 39
def asset_tag_root
  @definition["asset_tag_root"]
end
assets() click to toggle source

@return [YAML::Omap] Ordered mapping of definition keys to definition configuration (as a ‘YAML::Omap`).

# File lib/rapper/definition.rb, line 54
def assets
  @definition["assets"]
end
component_paths( name, root=nil ) click to toggle source

@param [String] name Name of the asset.

@return [Array<String>] Ordered list of the files that comprise the given

asset.
# File lib/rapper/definition.rb, line 99
def component_paths( name, root=nil )
  root ||= self.root
  spec = self.assets[name.to_s]
  
  if spec.nil?
    raise Rapper::Errors::InvalidAssetName,
      "'#{name}' is not a valid #{@type} asset. Make sure it is defined in the definition file."
  end
  
  ( spec["files"] || [] ).map do |file|
    if file =~ COMBINATION_PREFIX
      asset_name = file.sub( COMBINATION_PREFIX, "" )
      self.component_paths( asset_name, root )
    else
      file_name = "#{file}.#{self.suffix}"
      File.join( root, file_name )
    end
  end.flatten
end
component_tag_root() click to toggle source

@return [String] The public url root for the asset component files (used when bundling is off).

# File lib/rapper/definition.rb, line 34
def component_tag_root
  @definition["component_tag_root"]
end
destination_root() click to toggle source

@return [String] The root for packaged asset files. Defaults to root +

"/assets".
# File lib/rapper/definition.rb, line 27
def destination_root
  @default_destination_root ||= @definition["root"].gsub( /\/$/, '' ) + "/assets"
  @definition["destination_root"] || @default_destination_root
end
get_version( name ) click to toggle source
# File lib/rapper/definition.rb, line 67
def get_version( name )
  assets[name.to_s]["version"]
end
root() click to toggle source

@return [String] The root for asset component files.

# File lib/rapper/definition.rb, line 21
def root
  @definition["root"]
end
set_version( name, version ) click to toggle source

Update the version string for a specific asset.

@param [String] name Asset name.

@param [String] version New version string for the asset.

# File lib/rapper/definition.rb, line 63
def set_version( name, version )
  assets[name.to_s]["version"] = version
end
suffix() click to toggle source

@return [String] The suffix of files used in this definition.

# File lib/rapper/definition.rb, line 44
def suffix
  @definition["suffix"]
end
update() click to toggle source

Writes the in-memory definition out to disk.

# File lib/rapper/definition.rb, line 76
def update
  File.open( @path, "w" ) do |file|
    file.puts @definition.to_yaml
  end
end