Class: Nanoc2::AssetRep
- Inherits:
-
Object
- Object
- Nanoc2::AssetRep
- Defined in:
- lib/nanoc2/base/asset_rep.rb
Overview
A Nanoc2::AssetRep is a single representation (rep) of an asset (Nanoc2::Asset). An asset can have multiple representations. A representation has its own attributes and its own output file. A single asset can therefore have multiple output files, each run through a different set of filters with a different layout.
An asset representation is observable. The following events will be notified:
-
:compilation_started
-
:compilation_ended
-
:filtering_started
-
:filtering_ended
The compilation-related events have one parameters (the page representation); the filtering-related events have two (the page representation, and a symbol containing the filter class name).
Instance Attribute Summary collapse
-
#asset ⇒ Object
readonly
The asset (Nanoc2::Asset) to which this representation belongs.
-
#attributes ⇒ Object
A hash containing this asset representation’s attributes.
-
#name ⇒ Object
readonly
This asset representation’s unique name.
Instance Method Summary collapse
-
#attribute_named(name) ⇒ Object
Returns the attribute with the given name.
-
#compile(even_when_not_outdated, from_scratch) ⇒ Object
Compiles the asset representation and writes the result to the disk.
-
#compiled? ⇒ Boolean
Returns true if this page rep has been compiled, false otherwise.
-
#created? ⇒ Boolean
Returns true if this asset rep’s output file was created during the last compilation session, or false if the output file did already exist.
-
#disk_path ⇒ Object
Returns the path to the output file, including the path to the output directory specified in the site configuration, and including the filename and extension.
-
#initialize(asset, attributes, name) ⇒ AssetRep
constructor
Creates a new asset representation for the given asset and with the given attributes.
-
#modified? ⇒ Boolean
Returns true if this asset rep’s output file was modified during the last compilation session, or false if the output file wasn’t changed.
-
#outdated? ⇒ Boolean
Returns true if this asset rep’s output file is outdated and must be regenerated, false otherwise.
-
#to_proxy ⇒ Object
Returns a proxy (Nanoc2::AssetRepProxy) for this asset representation.
-
#web_path ⇒ Object
Returns the path to the output file as it would be used in a web browser: starting with a slash (representing the web root), and only including the filename and extension if they cannot be ignored (i.e. they are not in the site configuration’s list of index files).
Constructor Details
#initialize(asset, attributes, name) ⇒ AssetRep
Creates a new asset representation for the given asset and with the given attributes.
asset
-
The asset (Nanoc2::Asset) to which the new representation will belong.
attributes
-
A hash containing the new asset representation’s attributes. This hash must have been run through Hash#clean before using it here.
name
-
The unique name for the new asset representation.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/nanoc2/base/asset_rep.rb', line 42 def initialize(asset, attributes, name) # Set primary attributes @asset = asset @attributes = attributes @name = name # Reset flags @compiled = false @modified = false @created = false # Reset stages @filtered = false end |
Instance Attribute Details
#asset ⇒ Object (readonly)
The asset (Nanoc2::Asset) to which this representation belongs.
23 24 25 |
# File 'lib/nanoc2/base/asset_rep.rb', line 23 def asset @asset end |
#attributes ⇒ Object
A hash containing this asset representation’s attributes.
26 27 28 |
# File 'lib/nanoc2/base/asset_rep.rb', line 26 def attributes @attributes end |
#name ⇒ Object (readonly)
This asset representation’s unique name.
29 30 31 |
# File 'lib/nanoc2/base/asset_rep.rb', line 29 def name @name end |
Instance Method Details
#attribute_named(name) ⇒ Object
Returns the attribute with the given name. This method will look in several places for the requested attribute:
-
This asset representation’s attributes;
-
The attributes of this asset representation’s asset;
-
The asset defaults’ representation corresponding to this asset representation;
-
The asset defaults in general;
-
The hardcoded asset defaults, if everything else fails.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/nanoc2/base/asset_rep.rb', line 131 def attribute_named(name) # Check in here return @attributes[name] if @attributes.has_key?(name) # Check in asset return @asset.attributes[name] if @asset.attributes.has_key?(name) # Check in asset defaults' asset rep asset_default_reps = @asset.site.asset_defaults.attributes[:reps] || {} asset_default_rep = asset_default_reps[@name] || {} return asset_default_rep[name] if asset_default_rep.has_key?(name) # Check in site defaults (global) asset_defaults_attrs = @asset.site.asset_defaults.attributes return asset_defaults_attrs[name] if asset_defaults_attrs.has_key?(name) # Check in hardcoded defaults return Nanoc2::Asset::DEFAULTS[name] end |
#compile(even_when_not_outdated, from_scratch) ⇒ Object
Compiles the asset representation and writes the result to the disk. This method should not be called directly; please use Nanoc2::Compiler#run instead, and pass this asset representation’s asset as its first argument.
The asset representation will only be compiled if it wasn’t compiled before yet. To force recompilation of the asset rep, forgetting any progress, set from_scratch
to true.
even_when_not_outdated
-
true if the asset rep should be compiled even if it is not outdated, false if not.
from_scratch
-
true if the asset rep should be filtered again even if it has already been filtered, false otherwise.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/nanoc2/base/asset_rep.rb', line 165 def compile(even_when_not_outdated, from_scratch) # Don't compile if already compiled return if @compiled and !from_scratch # Skip unless outdated unless outdated? or even_when_not_outdated Nanoc2::NotificationCenter.post(:compilation_started, self) Nanoc2::NotificationCenter.post(:compilation_ended, self) return end # Reset flags @compiled = false @modified = false @created = !File.file?(self.disk_path) # Forget progress if requested @filtered = false if from_scratch # Start @asset.site.compiler.stack.push(self) Nanoc2::NotificationCenter.post(:compilation_started, self) # Compile unless @filtered if attribute_named(:binary) == true compile_binary else compile_textual end end @compiled = true # Stop @asset.site.compiler.stack.pop Nanoc2::NotificationCenter.post(:compilation_ended, self) end |
#compiled? ⇒ Boolean
Returns true if this page rep has been compiled, false otherwise.
75 76 77 |
# File 'lib/nanoc2/base/asset_rep.rb', line 75 def compiled? @compiled end |
#created? ⇒ Boolean
Returns true if this asset rep’s output file was created during the last compilation session, or false if the output file did already exist.
64 65 66 |
# File 'lib/nanoc2/base/asset_rep.rb', line 64 def created? @created end |
#disk_path ⇒ Object
Returns the path to the output file, including the path to the output directory specified in the site configuration, and including the filename and extension.
82 83 84 |
# File 'lib/nanoc2/base/asset_rep.rb', line 82 def disk_path @disk_path ||= @asset.site.router.disk_path_for(self) end |
#modified? ⇒ Boolean
Returns true if this asset rep’s output file was modified during the last compilation session, or false if the output file wasn’t changed.
70 71 72 |
# File 'lib/nanoc2/base/asset_rep.rb', line 70 def modified? @modified end |
#outdated? ⇒ Boolean
Returns true if this asset rep’s output file is outdated and must be regenerated, false otherwise.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/nanoc2/base/asset_rep.rb', line 98 def outdated? # Outdated if we don't know return true if @asset.mtime.nil? # Outdated if compiled file doesn't exist return true if !File.file?(disk_path) # Get compiled mtime compiled_mtime = File.stat(disk_path).mtime # Outdated if file too old return true if @asset.mtime > compiled_mtime # Outdated if asset defaults outdated return true if @asset.site.asset_defaults.mtime.nil? return true if @asset.site.asset_defaults.mtime > compiled_mtime # Outdated if code outdated return true if @asset.site.code.mtime.nil? return true if @asset.site.code.mtime > compiled_mtime return false end |
#to_proxy ⇒ Object
Returns a proxy (Nanoc2::AssetRepProxy) for this asset representation.
58 59 60 |
# File 'lib/nanoc2/base/asset_rep.rb', line 58 def to_proxy @proxy ||= AssetRepProxy.new(self) end |
#web_path ⇒ Object
Returns the path to the output file as it would be used in a web browser: starting with a slash (representing the web root), and only including the filename and extension if they cannot be ignored (i.e. they are not in the site configuration’s list of index files).
90 91 92 93 94 |
# File 'lib/nanoc2/base/asset_rep.rb', line 90 def web_path compile(false, false) @web_path ||= @asset.site.router.web_path_for(self) end |