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, &ioblock) ⇒ Path

Create a new Path object for path. The optional block needs to return an IO object for the content of the path.



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

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

Instance Attribute Details

#basenameObject

The basename part of the path.



44
45
46
# File 'lib/webgen/path.rb', line 44

def basename
  @basename
end

#cnbaseObject

The canonical name without the extension.



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

def cnbase
  @cnbase
end

#directoryObject

The directory part of the path.



47
48
49
# File 'lib/webgen/path.rb', line 47

def directory
  @directory
end

#extObject

The extension.



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

def ext
  @ext
end

#meta_infoObject

Extracted meta information for the path.



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

def meta_info
  @meta_info
end

#pathObject

The full source path.



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

def path
  @path
end

Class Method Details

.lcn(cn, lang) ⇒ Object

Utility method for creating the lcn from cn and the language lang.



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

def self.lcn(cn, lang)
  if lang.nil?
    cn
  else
    cn.split('.').insert(1, lang.to_s).join('.')
  end
end

Instance Method Details

#<=>(other) ⇒ Object

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



141
142
143
# File 'lib/webgen/path.rb', line 141

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

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

Compare this object to another Path or a String.



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

def ==(other)
  if other.kind_of?(Path)
    other.path == @path
  elsif other.kind_of?(String)
    other == @path
  else
    false
  end
end

#=~(pattern) ⇒ Object

Return true if the localized path matches the given pattern. For information on which patterns are supported, have a look at the documentation of File.fnmatch.



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

def =~(pattern)
  File.fnmatch(pattern, File.join(@directory, lcn), File::FNM_DOTMATCH|File::FNM_CASEFOLD|File::FNM_PATHNAME)
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)


83
84
85
# File 'lib/webgen/path.rb', line 83

def changed?
  true
end

#cnObject

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



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

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

#dupObject

Duplicate the path object.



88
89
90
91
92
# File 'lib/webgen/path.rb', line 88

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.



146
147
148
# File 'lib/webgen/path.rb', line 146

def hash
  @path.hash
end

#inspectObject

:nodoc:



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

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

#ioObject

The IO object associated with the path.



95
96
97
98
99
100
101
# File 'lib/webgen/path.rb', line 95

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.



118
119
120
# File 'lib/webgen/path.rb', line 118

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.



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/webgen/path.rb', line 68

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)
  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:



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

def to_s #:nodoc:
  @path.dup
end