Class: Nanoc2::Asset

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc2/base/asset.rb

Overview

A Nanoc2::Asset represents an asset in a nanoc site. It has a file object (File instance) and attributes, as well as a path. It can also store the modification time to speed up compilation.

Each asset has a list of asset representations or reps (Nanoc2::AssetRep); compiling an asset actually compiles all of its assets.

Constant Summary collapse

DEFAULTS =

Defaults values for assets.

{
  :extension  => 'dat',
  :binary     => true,
  :filters    => []
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, attributes, path, mtime = nil) ⇒ Asset

Creates a new asset.

file

An instance of File representing the uncompiled asset.

attributes

A hash containing this asset’s attributes.

path

This asset’s path.

mtime

The time when this asset was last modified.



45
46
47
48
49
50
51
# File 'lib/nanoc2/base/asset.rb', line 45

def initialize(file, attributes, path, mtime=nil)
  # Set primary attributes
  @file           = file
  @attributes     = attributes.clean
  @path           = path.cleaned_path
  @mtime          = mtime
end

Instance Attribute Details

#attributesObject

A hash containing this asset’s attributes.



25
26
27
# File 'lib/nanoc2/base/asset.rb', line 25

def attributes
  @attributes
end

#fileObject (readonly)

This assets’s file.



22
23
24
# File 'lib/nanoc2/base/asset.rb', line 22

def file
  @file
end

#mtimeObject (readonly)

The time when this asset was last modified.



31
32
33
# File 'lib/nanoc2/base/asset.rb', line 31

def mtime
  @mtime
end

#pathObject (readonly)

This asset’s path.



28
29
30
# File 'lib/nanoc2/base/asset.rb', line 28

def path
  @path
end

#repsObject (readonly)

This asset’s list of asset representations.



34
35
36
# File 'lib/nanoc2/base/asset.rb', line 34

def reps
  @reps
end

#siteObject

The Nanoc2::Site this asset belongs to.



19
20
21
# File 'lib/nanoc2/base/asset.rb', line 19

def site
  @site
end

Instance Method Details

#attribute_named(name) ⇒ Object

Returns the attribute with the given name.



81
82
83
84
85
# File 'lib/nanoc2/base/asset.rb', line 81

def attribute_named(name)
  return @attributes[name] if @attributes.has_key?(name)
  return @site.asset_defaults.attributes[name] if @site.asset_defaults.attributes.has_key?(name)
  return DEFAULTS[name]
end

#build_repsObject

Builds the individual asset representations (Nanoc2::AssetRep) for this asset.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/nanoc2/base/asset.rb', line 55

def build_reps
  # Get list of rep names
  rep_names_default = (@site.asset_defaults.attributes[:reps] || {}).keys
  rep_names_this    = (@attributes[:reps] || {}).keys + [ :default ]
  rep_names         = rep_names_default | rep_names_this

  # Get list of reps
  reps = rep_names.inject({}) do |memo, rep_name|
    rep = (@attributes[:reps] || {})[rep_name]
    is_bad = (@attributes[:reps] || {}).has_key?(rep_name) && rep.nil?
    is_bad ? memo : memo.merge(rep_name => rep || {})
  end

  # Build reps
  @reps = []
  reps.each_pair do |name, attrs|
    @reps << AssetRep.new(self, attrs, name)
  end
end

#deleteObject

Deletes the asset. Tells the site’s data source to delete the asset.



105
106
107
108
109
# File 'lib/nanoc2/base/asset.rb', line 105

def delete
  @site.data_source.loading do
    @site.data_source.delete_asset(self)
  end
end

#inspectObject



111
112
113
# File 'lib/nanoc2/base/asset.rb', line 111

def inspect
  "<#{self.class} path=#{self.path}>"
end

#move_to(new_path) ⇒ Object

Moves the asset to a new path. Tells the site’s data source to move the asset.



98
99
100
101
102
# File 'lib/nanoc2/base/asset.rb', line 98

def move_to(new_path)
  @site.data_source.loading do
    @site.data_source.move_asset(self, new_path)
  end
end

#saveObject

Saves the asset in the database, creating it if it doesn’t exist yet or updating it if it already exists. Tells the site’s data source to save the asset.



90
91
92
93
94
# File 'lib/nanoc2/base/asset.rb', line 90

def save
  @site.data_source.loading do
    @site.data_source.save_asset(self)
  end
end

#to_proxyObject

Returns a proxy (Nanoc2::AssetProxy) for this asset.



76
77
78
# File 'lib/nanoc2/base/asset.rb', line 76

def to_proxy
  @proxy ||= AssetProxy.new(self)
end