Class: Webgen::Path
Overview
A path object provides information about a specific path as well as methods for accessing its content.
A webgen source class needs to derive a specialized path class from this class and implement an approriate #changed? method that returns true
if the path’s content has changed since the last webgen run.
Direct Known Subclasses
Defined Under Namespace
Classes: SourceIO
Instance Attribute Summary collapse
-
#basename ⇒ Object
The basename part of the path.
-
#cnbase ⇒ Object
The canonical name without the extension.
-
#directory ⇒ Object
The directory part of the path.
-
#ext ⇒ Object
The extension.
-
#meta_info ⇒ Object
Extracted meta information for the path.
-
#path ⇒ Object
The full path.
-
#source_path ⇒ Object
The source path that lead to the creation of this path.
Class Method Summary collapse
-
.lcn(cn, lang) ⇒ Object
Utility method for creating the lcn from
cn
and the languagelang
. -
.match(path, pattern) ⇒ Object
Return
true
if the givenpath
matches the givenpattern
(trailing slashes of directories are not respected).
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Implemented sothat a Path looks like a String when used as key in a hash.
-
#==(other) ⇒ Object
(also: #eql?)
Compare this object to another Path or a String.
-
#changed? ⇒ Boolean
Has the content of this path changed since the last webgen run? This default implementation always returns
true
, a specialized sub class needs to override this behaviour!. -
#cn ⇒ Object
The canonical name created from the filename (created from cnbase and extension).
-
#dup ⇒ Object
Duplicate the path object.
-
#hash ⇒ Object
Implemented sothat a Path looks like a String when used as key in a hash.
-
#initialize(path, source_path = path, &ioblock) ⇒ Path
constructor
Create a new Path object for
path
. -
#inspect ⇒ Object
:nodoc:.
-
#io ⇒ Object
The SourceIO object associated with the path.
-
#lcn ⇒ Object
The localized canonical name created from the filename.
-
#mount_at(mp, prefix = nil) ⇒ Object
Mount this path at the mount point
mp
optionally strippingprefix
from the path and return the new path object. -
#to_s ⇒ Object
(also: #to_str)
:nodoc:.
Constructor Details
#initialize(path, source_path = path, &ioblock) ⇒ Path
Create a new Path object for path
. The optional source_path
parameter specifies the path that lead to the creation of this path. The optional block needs to return an IO object for the content of the path.
76 77 78 79 80 81 |
# File 'lib/webgen/path.rb', line 76 def initialize(path, source_path = path, &ioblock) = {} @io = block_given? ? SourceIO.new(&ioblock) : nil @source_path = source_path analyse(path) end |
Instance Attribute Details
#basename ⇒ Object
The basename part of the path.
59 60 61 |
# File 'lib/webgen/path.rb', line 59 def basename @basename end |
#cnbase ⇒ Object
The canonical name without the extension.
65 66 67 |
# File 'lib/webgen/path.rb', line 65 def cnbase @cnbase end |
#directory ⇒ Object
The directory part of the path.
62 63 64 |
# File 'lib/webgen/path.rb', line 62 def directory @directory end |
#ext ⇒ Object
The extension.
68 69 70 |
# File 'lib/webgen/path.rb', line 68 def ext @ext end |
#meta_info ⇒ Object
Extracted meta information for the path.
71 72 73 |
# File 'lib/webgen/path.rb', line 71 def end |
#path ⇒ Object
The full path.
53 54 55 |
# File 'lib/webgen/path.rb', line 53 def path @path end |
#source_path ⇒ Object
The source path that lead to the creation of this path.
56 57 58 |
# File 'lib/webgen/path.rb', line 56 def source_path @source_path end |
Class Method Details
.lcn(cn, lang) ⇒ Object
Utility method for creating the lcn from cn
and the language lang
.
127 128 129 130 131 132 133 |
# File 'lib/webgen/path.rb', line 127 def self.lcn(cn, lang) if lang.nil? cn else cn.split('.').insert(1, lang.to_s).join('.') end end |
.match(path, pattern) ⇒ Object
Return true
if the given path
matches the given pattern
(trailing slashes of directories are not respected). For information on which patterns are supported, have a look at the documentation of File.fnmatch.
43 44 45 46 47 |
# File 'lib/webgen/path.rb', line 43 def self.match(path, pattern) path = path.to_s.chomp('/') unless path == '/' pattern = pattern.to_s.chomp('/') unless pattern == '/' File.fnmatch(pattern, path, File::FNM_DOTMATCH|File::FNM_CASEFOLD|File::FNM_PATHNAME) end |
Instance Method Details
#<=>(other) ⇒ Object
Implemented sothat a Path looks like a String when used as key in a hash.
153 154 155 |
# File 'lib/webgen/path.rb', line 153 def <=>(other) @path <=> other.to_str end |
#==(other) ⇒ Object Also known as: eql?
Compare this object to another Path or a String.
141 142 143 144 145 146 147 148 149 |
# File 'lib/webgen/path.rb', line 141 def ==(other) if other.kind_of?(Path) other.path == @path elsif other.kind_of?(String) other == @path else false end end |
#changed? ⇒ Boolean
Has the content of this path changed since the last webgen run? This default implementation always returns true
, a specialized sub class needs to override this behaviour!
101 102 103 |
# File 'lib/webgen/path.rb', line 101 def changed? true end |
#cn ⇒ Object
The canonical name created from the filename (created from cnbase and extension).
122 123 124 |
# File 'lib/webgen/path.rb', line 122 def cn @cnbase + (@ext.length > 0 ? '.' + @ext : '') end |
#dup ⇒ Object
Duplicate the path object.
106 107 108 109 110 |
# File 'lib/webgen/path.rb', line 106 def dup temp = super temp. = .dup temp end |
#hash ⇒ Object
Implemented sothat a Path looks like a String when used as key in a hash.
158 159 160 |
# File 'lib/webgen/path.rb', line 158 def hash @path.hash end |
#inspect ⇒ Object
:nodoc:
167 168 169 |
# File 'lib/webgen/path.rb', line 167 def inspect #:nodoc: "#<Path: #{@path}>" end |
#io ⇒ Object
The SourceIO object associated with the path.
113 114 115 116 117 118 119 |
# File 'lib/webgen/path.rb', line 113 def io if @io @io else raise "No IO object defined for the path #{self}" end end |
#lcn ⇒ Object
The localized canonical name created from the filename.
136 137 138 |
# File 'lib/webgen/path.rb', line 136 def lcn self.class.lcn(cn, ['lang']) end |
#mount_at(mp, prefix = nil) ⇒ Object
Mount this path at the mount point mp
optionally stripping prefix
from the path and return the new path object.
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/webgen/path.rb', line 85 def mount_at(mp, prefix = nil) temp = dup temp.path = temp.path.sub(/^#{Regexp.escape(prefix.chomp("/"))}/, '') if prefix #" reanalyse = (@path == '/' || temp.path == '/') temp.path = File.join(mp, temp.path) temp.source_path = temp.path if @path == @source_path if reanalyse temp.send(:analyse, temp.path) else temp.directory = File.join(File.dirname(temp.path), '/') end temp end |
#to_s ⇒ Object Also known as: to_str
:nodoc:
162 163 164 |
# File 'lib/webgen/path.rb', line 162 def to_s #:nodoc: @path.dup end |