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.

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.



76
77
78
79
80
81
# File 'lib/webgen/path.rb', line 76

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

Instance Attribute Details

#basenameObject

The basename part of the path.



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

def basename
  @basename
end

#cnbaseObject

The canonical name without the extension.



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

def cnbase
  @cnbase
end

#directoryObject

The directory part of the path.



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

def directory
  @directory
end

#extObject

The extension.



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

def ext
  @ext
end

#meta_infoObject

Extracted meta information for the path.



71
72
73
# File 'lib/webgen/path.rb', line 71

def meta_info
  @meta_info
end

#pathObject

The full path.



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

def path
  @path
end

#source_pathObject

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!

Returns:

  • (Boolean)


101
102
103
# File 'lib/webgen/path.rb', line 101

def changed?
  true
end

#cnObject

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

#dupObject

Duplicate the path object.



106
107
108
109
110
# File 'lib/webgen/path.rb', line 106

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.



158
159
160
# File 'lib/webgen/path.rb', line 158

def hash
  @path.hash
end

#inspectObject

:nodoc:



167
168
169
# File 'lib/webgen/path.rb', line 167

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

#ioObject

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

#lcnObject

The localized canonical name created from the filename.



136
137
138
# File 'lib/webgen/path.rb', line 136

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.



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

:nodoc:



162
163
164
# File 'lib/webgen/path.rb', line 162

def to_s #:nodoc:
  @path.dup
end