Class: Webby::Resources::Resource
- Inherits:
-
Object
- Object
- Webby::Resources::Resource
- Defined in:
- lib/webby/resources/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.
Instance Attribute Summary collapse
-
#_meta_data ⇒ Object
readonly
:nodoc:.
-
#dir ⇒ Object
readonly
The directory of the resource excluding the content directory.
-
#ext ⇒ Object
readonly
Extesion of the resource file.
-
#mtime ⇒ Object
readonly
Resource file modification time.
-
#name ⇒ Object
readonly
The name of the file excluding the directory and extension.
-
#path ⇒ Object
readonly
The full path to the resource file.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
call-seq: resource <=> other => -1, 0, +1, or nil.
-
#[](key) ⇒ Object
call-seq: resource => value or nil.
-
#[]=(key, value) ⇒ Object
call-seq: resource = value.
-
#_read ⇒ Object
:stopdoc:.
- #_reset(meta_data = nil) ⇒ Object
-
#destination ⇒ Object
Returns the path in the output directory where the resource will be generated.
-
#directory ⇒ Object
The location of this resource in the directory structure.
-
#dirty? ⇒ Boolean
call-seq: dirty? => true or false.
-
#equal?(other) ⇒ Boolean
(also: #==, #eql?)
call-seq: equal?( other ) => true or false.
-
#extension ⇒ Object
The resource file extension.
-
#filename ⇒ Object
The resource filename excluding path and extension.
-
#initialize(fn) ⇒ Resource
constructor
call-seq: Resource.new( filename ) => resource.
-
#method_missing(name, *a, &b) ⇒ Object
call-seq: method_missing( symbol [, *args, &block] ) => result.
-
#url ⇒ Object
Returns a string suitable for use as a URL linking to this resource.
Constructor Details
#initialize(fn) ⇒ Resource
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/webby/resources/resource.rb', line 38 def initialize( fn ) @path = fn @dir = ::Webby::Resources.dirname(@path) @name = ::Webby::Resources.basename(@path) @ext = ::Webby::Resources.extname(@path) @mtime = ::File.mtime @path @_meta_data = {} self._reset 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.
102 103 104 |
# File 'lib/webby/resources/resource.rb', line 102 def method_missing( name, *a, &b ) [name.to_s] end |
Instance Attribute Details
#_meta_data ⇒ Object (readonly)
:nodoc:
31 32 33 |
# File 'lib/webby/resources/resource.rb', line 31 def @_meta_data end |
#dir ⇒ Object (readonly)
The directory of the resource excluding the content directory
23 24 25 |
# File 'lib/webby/resources/resource.rb', line 23 def dir @dir end |
#ext ⇒ Object (readonly)
Extesion of the resource file
26 27 28 |
# File 'lib/webby/resources/resource.rb', line 26 def ext @ext end |
#mtime ⇒ Object (readonly)
Resource file modification time
29 30 31 |
# File 'lib/webby/resources/resource.rb', line 29 def mtime @mtime end |
#name ⇒ Object (readonly)
The name of the file excluding the directory and extension
20 21 22 |
# File 'lib/webby/resources/resource.rb', line 20 def name @name end |
#path ⇒ Object (readonly)
The full path to the resource file
17 18 19 |
# File 'lib/webby/resources/resource.rb', line 17 def path @path 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.
69 70 71 72 |
# File 'lib/webby/resources/resource.rb', line 69 def <=>( other ) return unless other.kind_of? ::Webby::Resources::Resource self.destination <=> other.destination end |
#[](key) ⇒ Object
call-seq:
resource[key] => value or nil
Returns the value associated with the given meta-data key. Key is converted into a string.
80 81 82 |
# File 'lib/webby/resources/resource.rb', line 80 def []( key ) [key.to_s] end |
#[]=(key, value) ⇒ Object
call-seq:
resource[key] = value
Sets the given meta-data key to the value. Key is converted into a string.
90 91 92 |
# File 'lib/webby/resources/resource.rb', line 90 def []=( key, value ) [key.to_s] = value end |
#_read ⇒ Object
:stopdoc:
185 186 187 |
# File 'lib/webby/resources/resource.rb', line 185 def _read MetaFile.read(@path) end |
#_reset(meta_data = nil) ⇒ Object
189 190 191 192 193 |
# File 'lib/webby/resources/resource.rb', line 189 def _reset( = nil ) .replace() if .instance_of?(Hash) @url = nil @destination = nil end |
#destination ⇒ Object
Returns the path in the output directory where the resource will be generated. This path is used to determine if the resource is dirty and in need of generating.
166 167 168 169 170 171 172 173 174 175 |
# File 'lib/webby/resources/resource.rb', line 166 def destination return @destination unless @destination.nil? @destination = ::File.join(::Webby.site.output_dir, directory, filename) ext = extension unless ext.nil? or ext.empty? @destination << '.' << ext end @destination end |
#directory ⇒ Object
The location of this resource in the directory structure. This directory does not include the content folder or the output folder.
157 158 159 160 |
# File 'lib/webby/resources/resource.rb', line 157 def directory return ['directory'] if .has_key? 'directory' dir 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.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/webby/resources/resource.rb', line 113 def dirty? return ['dirty'] if .has_key? 'dirty' # if the destination file does not exist, then we are dirty return true unless test(?e, destination) # if this file's mtime is larger than the destination file's # mtime, then we are dirty dirty = @mtime > ::File.mtime(destination) return dirty if dirty # check to see if the layout is dirty, and if it is then we # are dirty, too if .has_key? 'layout' lyt = ::Webby::Resources.find_layout(['layout']) unless lyt.nil? return true if lyt.dirty? end end # if we got here, then we are not 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.
55 56 57 58 |
# File 'lib/webby/resources/resource.rb', line 55 def equal?( other ) return false unless other.kind_of? ::Webby::Resources::Resource (self.destination == other.destination) && (self.path == other.path) end |
#extension ⇒ Object
The resource file extension. This will either be the extension of the file or the ‘extension’ attribute from the meta-data if present.
149 150 151 152 |
# File 'lib/webby/resources/resource.rb', line 149 def extension return ['extension'] if .has_key? 'extension' ext end |
#filename ⇒ Object
The resource filename excluding path and extension. This will either be the name of the file or the ‘filename’ attribute from the meta-data if present.
141 142 143 144 |
# File 'lib/webby/resources/resource.rb', line 141 def filename return ['filename'] if .has_key? 'filename' name end |