Class: Webby::Resource
- Inherits:
-
Object
- Object
- Webby::Resource
- Defined in:
- lib/webby/resource.rb
Overview
A Webby::Resource is any file that can be found in the content directory or in the layout directory. This class contains information about the resources available to Webby. This information includes the resource type (static, page, layout), if the resource is dirty (it needs to be rendered), the output location of the rendered resource, etc.
A resource is a “layout” if the resource is found in the layout directory. Static and page resources are found in the content directory.
A resource is considered static only if it *does not* contain a YAML meta-data header at the top of the file. These resources will be copied as-is from the content directory to the output directory.
If a resouce does have meta-data, then it will be processed (i.e. rendered/filtered) by Webby, and the rendered results will be written to the output directory.
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
The directory of the resource excluding the content directory.
-
#ext ⇒ Object
readonly
Extesion of the resource file.
-
#filename ⇒ Object
readonly
The resource filename excluding path and extension.
-
#mtime ⇒ Object
readonly
Resource file modification time.
-
#path ⇒ Object
readonly
The full path to the resource file.
Class Method Summary collapse
-
.clear ⇒ Object
Clear the contents of the
layouts
and thepages
hash objects. -
.layouts ⇒ Object
Returns the layouts hash object.
-
.pages ⇒ Object
Returns the pages hash object.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
call-seq: resource <=> other => -1, 0, +1, or nil.
-
#destination ⇒ Object
call-seq: destination => string.
-
#dirty? ⇒ Boolean
call-seq: dirty? => true or false.
-
#equal?(other) ⇒ Boolean
(also: #==, #eql?)
call-seq: equal?( other ) => true or false.
-
#extension ⇒ Object
call-seq: extension => string.
-
#initialize(fn) ⇒ Resource
constructor
call-seq: Resource.new( filename ) => resource.
-
#is_layout? ⇒ Boolean
call-seq: is_layout? => true or false.
-
#is_page? ⇒ Boolean
call-seq: is_page? => true or false.
-
#is_static? ⇒ Boolean
call-seq: is_static? => true or false.
-
#method_missing(name, *a, &b) ⇒ Object
call-seq: method_missing( symbol [, *args, &block] ) => result.
-
#render ⇒ Object
call-seq: render => string.
Constructor Details
#initialize(fn) ⇒ Resource
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/webby/resource.rb', line 62 def initialize( fn ) @path = fn.sub(%r/\A(?:\.\/|\/)/o, '').freeze @dir = ::File.dirname(@path).sub(%r/\A[^\/]+\/?/o, '') @filename = ::File.basename(@path).sub(%r/\.\w+\z/o, '') @ext = ::File.extname(@path).sub(%r/\A\.?/o, '') @mtime = ::File.mtime @path @rendering = false # deal with the meta-data @mdata = ::Webby::File.(@path) @have_mdata = !@mdata.nil? @mdata ||= {} @mdata = ::Webby.page_defaults.merge(@mdata) if is_page? @mdata.sanitize! self.class.pages << self if is_page? or is_static? self.class.layouts << self if is_layout? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *a, &b) ⇒ Object
call-seq:
method_missing( symbol [, *args, &block] ) => result
Invoked by Ruby when a message is sent to the resource that it cannot handle. The default behavior is to convert symbol to a string and search for that string in the resource’s meta-data. If found, the meta-data item is returned; otherwise, nil
is returned.
243 244 245 |
# File 'lib/webby/resource.rb', line 243 def method_missing( name, *a, &b ) @mdata[name.to_s] end |
Instance Attribute Details
#dir ⇒ Object (readonly)
The directory of the resource excluding the content directory
46 47 48 |
# File 'lib/webby/resource.rb', line 46 def dir @dir end |
#ext ⇒ Object (readonly)
Extesion of the resource file
52 53 54 |
# File 'lib/webby/resource.rb', line 52 def ext @ext end |
#filename ⇒ Object (readonly)
The resource filename excluding path and extension
49 50 51 |
# File 'lib/webby/resource.rb', line 49 def filename @filename end |
#mtime ⇒ Object (readonly)
Resource file modification time
55 56 57 |
# File 'lib/webby/resource.rb', line 55 def mtime @mtime end |
#path ⇒ Object (readonly)
The full path to the resource file
43 44 45 |
# File 'lib/webby/resource.rb', line 43 def path @path end |
Class Method Details
.clear ⇒ Object
Clear the contents of the layouts
and the pages
hash objects.
36 37 38 39 |
# File 'lib/webby/resource.rb', line 36 def clear self.pages.clear self.layouts.clear end |
Instance Method Details
#<=>(other) ⇒ Object
call-seq:
resource <=> other => -1, 0, +1, or nil
Resource comparison operates on the full path of the resource objects and uses the standard String comparison operator. Returns nil
if other is not a Resource instance.
103 104 105 106 |
# File 'lib/webby/resource.rb', line 103 def <=>( other ) return unless self.class == other.class @path <=> other.path end |
#destination ⇒ Object
call-seq:
destination => string
Returns the path in the output directory where the results of rendering this resource should be stored. This path is used to determine if the resource is dirty and in need of rendering.
The destination for any resource can be overridden by explicitly setting the ‘destination’ propery in the resource’s meta-data.
140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/webby/resource.rb', line 140 def destination return @dest if defined? @dest return @dest = ::Webby.config['output_dir'] if is_layout? @dest = if @mdata.has_key? 'destination' then @mdata['destination'] else File.join(dir, filename) end @dest = File.join(::Webby.config['output_dir'], @dest) @dest << '.' @dest << extension @dest end |
#dirty? ⇒ Boolean
call-seq:
dirty? => true or false
Returns true
if this resource is newer than its corresponding output product. The resource needs to be rendered (if a page or layout) or copied (if a static file) to the output directory.
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/webby/resource.rb', line 212 def dirty? return @mdata['dirty'] if @mdata.has_key? 'dirty' # if the destination file does not exist, then we are dirty return @mdata['dirty'] = true unless test ?e, destination # if this file's mtime is larger than the destination file's # mtime, then we are dirty @mdata['dirty'] = @mtime > File.mtime(destination) return @mdata['dirty'] if is_static? or @mdata['dirty'] # check to see if the layout is dirty, and it it is then we # are dirty, too if @mdata.has_key? 'layout' lyt = self.class.layouts.find_by_name @mdata['layout'] break if lyt.nil? return @mdata['dirty'] = true if lyt.dirty? end # if we got here, then we are not dirty @mdata['dirty'] = false end |
#equal?(other) ⇒ Boolean Also known as: ==, eql?
call-seq:
equal?( other ) => true or false
Returns true
if the path of this resource is equivalent to the path of the other resource. Returns false
if this is not the case.
89 90 91 92 |
# File 'lib/webby/resource.rb', line 89 def equal?( other ) return false unless self.class == other.class @path == other.path end |
#extension ⇒ Object
call-seq:
extension => string
Returns the extension that will be appended to the output destination filename. The extension is determined by looking at the following:
-
this resource’s meta-data for an ‘extension’ property
-
the meta-data of this resource’s layout for an ‘extension’ propery
-
the extension of this resource file
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/webby/resource.rb', line 118 def extension return @mdata['extension'] if @mdata.has_key? 'extension' if @mdata.has_key? 'layout' lyt = self.class.layouts.find_by_name @mdata['layout'] break if lyt.nil? return lyt.extension end @ext end |
#is_layout? ⇒ Boolean
call-seq:
is_layout? => true or false
Returns true
if this resource is a layout.
182 183 184 185 |
# File 'lib/webby/resource.rb', line 182 def is_layout? @is_layout ||= !(%r/\A(?:\.\/|\/)?#{::Webby.config['layout_dir']}\//o =~ @path).nil? end |
#is_page? ⇒ Boolean
call-seq:
is_page? => true or false
Returns true
if this resource is a page suitable for rendering.
201 202 203 |
# File 'lib/webby/resource.rb', line 201 def is_page? @have_mdata and !is_layout? end |
#is_static? ⇒ Boolean
call-seq:
is_static? => true or false
Returns true
if this resource is a static file.
192 193 194 |
# File 'lib/webby/resource.rb', line 192 def is_static? !@have_mdata end |
#render ⇒ Object
call-seq:
render => string
Creates a new Webby::Renderer instance and uses that instance to render the resource contents using the configured filter(s). The filter(s) to use is defined in the resource’s meta-data as the ‘filter’ key.
Note, this only renders this resource. The returned string does not include any layout rendering.
163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/webby/resource.rb', line 163 def render raise Error, "page '#@path' is in a rendering loop" if @rendering @rendering = true content = Renderer.new(self).render_page @rendering = false return content rescue @rendering = false raise end |