Class: Webgen::Path

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/webgen/path.rb

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

Source::FileSystem::Path

Defined Under Namespace

Classes: SourceIO

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.



74
75
76
77
78
79
# File 'lib/webgen/path.rb', line 74

def initialize(path, source_path = path, &ioblock)
  @meta_info = {}
  @io = SourceIO.new(&ioblock) if block_given?
  @source_path = source_path
  analyse(path)
end

Instance Attribute Details

#basenameObject

The basename part of the path.



57
58
59
# File 'lib/webgen/path.rb', line 57

def basename
  @basename
end

#cnbaseObject

The canonical name without the extension.



63
64
65
# File 'lib/webgen/path.rb', line 63

def cnbase
  @cnbase
end

#directoryObject

The directory part of the path.



60
61
62
# File 'lib/webgen/path.rb', line 60

def directory
  @directory
end

#extObject

The extension.



66
67
68
# File 'lib/webgen/path.rb', line 66

def ext
  @ext
end

#meta_infoObject

Extracted meta information for the path.



69
70
71
# File 'lib/webgen/path.rb', line 69

def meta_info
  @meta_info
end

#pathObject

The full path.



51
52
53
# File 'lib/webgen/path.rb', line 51

def path
  @path
end

#source_pathObject

The source path that lead to the creation of this path.



54
55
56
# File 'lib/webgen/path.rb', line 54

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.



125
126
127
128
129
130
131
# File 'lib/webgen/path.rb', line 125

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.



41
42
43
44
45
# File 'lib/webgen/path.rb', line 41

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.



151
152
153
# File 'lib/webgen/path.rb', line 151

def <=>(other)
  @path <=> other.to_str
end

#==(other) ⇒ Object Also known as: eql?

Compare this object to another Path or a String.



139
140
141
142
143
144
145
146
147
# File 'lib/webgen/path.rb', line 139

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!



99
100
101
# File 'lib/webgen/path.rb', line 99

def changed?
  true
end

#cnObject

The canonical name created from the filename (created from cnbase and extension).



120
121
122
# File 'lib/webgen/path.rb', line 120

def cn
  @cnbase + (@ext.length > 0 ? '.' + @ext : '')
end

#dupObject

Duplicate the path object.



104
105
106
107
108
# File 'lib/webgen/path.rb', line 104

def dup
  temp = super
  temp.meta_info = @meta_info.dup
  temp
end

#hashObject

Implemented sothat a Path looks like a String when used as key in a hash.



156
157
158
# File 'lib/webgen/path.rb', line 156

def hash
  @path.hash
end

#inspectObject

:nodoc:



165
166
167
# File 'lib/webgen/path.rb', line 165

def inspect #:nodoc:
  "#<Path: #{@path}>"
end

#ioObject

The IO object associated with the path.



111
112
113
114
115
116
117
# File 'lib/webgen/path.rb', line 111

def io
  if @io
    @io
  else
    raise "No IO object defined for the path #{self}"
  end
end

#lcnObject

The localized canonical name created from the filename.



134
135
136
# File 'lib/webgen/path.rb', line 134

def lcn
  self.class.lcn(cn, @meta_info['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.



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/webgen/path.rb', line 83

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_sObject Also known as: to_str

:nodoc:



160
161
162
# File 'lib/webgen/path.rb', line 160

def to_s #:nodoc:
  @path.dup
end