Class: Path
Instance Method Summary
collapse
Methods inherited from String
#substitute, #to_iv, #to_reader, #to_writer
Constructor Details
#initialize(path = '') ⇒ Path
Returns a new instance of Path.
2
3
4
5
6
|
# File 'lib/RubyExt/Path.rb', line 2
def initialize path = ''
super path.chomp
raise "Invalid Path '#{path}' (ends with the '/' sign)" if (self =~ /\/$/ ) && !empty?
raise "Tnvalid Path '#{path}' (the '/' sign encounters multiple times in a row)" if self =~ /\/{2,}/
end
|
Instance Method Details
70
71
72
|
# File 'lib/RubyExt/Path.rb', line 70
def + o
add(o)
end
|
#absolute? ⇒ Boolean
8
9
10
|
# File 'lib/RubyExt/Path.rb', line 8
def absolute?;
(self =~ /^\//) ? true : false
end
|
#add(path) ⇒ Object
62
63
64
65
66
67
|
# File 'lib/RubyExt/Path.rb', line 62
def add path
path = Path.new(path) unless path.is_a? Path
return Path.new((absolute? ? "/" : "") + path.to_relative) if empty?
return Path.new(self) if path.empty?
return Path.new(self.string_plus((path.absolute? ? '' : '/')) + path)
end
|
#after(part) ⇒ Object
22
23
24
25
|
# File 'lib/RubyExt/Path.rb', line 22
def after part
raise "There is no Part '#{part}' in the Path '#{self}'" unless include? part
Path.new((absolute? ? '/' : '') + sub(/.*#{part}\/*/, ""))
end
|
#before(part) ⇒ Object
27
28
29
30
|
# File 'lib/RubyExt/Path.rb', line 27
def before part
raise "There is no Part '#{part}' in the Path '#{self}'" unless include? part
Path.new((absolute? ? '/' : '') + to_relative.sub(/\/*#{part}.*/, ""))
end
|
#each(&block) ⇒ Object
79
80
81
|
# File 'lib/RubyExt/Path.rb', line 79
def each &block
to_a.each(&block)
end
|
#empty? ⇒ Boolean
16
|
# File 'lib/RubyExt/Path.rb', line 16
def empty?; self == '' || self == '/' end
|
44
45
46
|
# File 'lib/RubyExt/Path.rb', line 44
def first;
Path.new(scan(/^\/?[^\/]*/)[0])
end
|
#first_name ⇒ Object
57
58
59
60
|
# File 'lib/RubyExt/Path.rb', line 57
def first_name
list = scan(/[^\/]+/)
return list.size > 0 ? list[0] : nil
end
|
48
49
50
|
# File 'lib/RubyExt/Path.rb', line 48
def last;
Path.new(sub(/[^\/].+\//, ""))
end
|
#last_name ⇒ Object
52
53
54
55
|
# File 'lib/RubyExt/Path.rb', line 52
def last_name
list = scan(/[^\/]+$/)
return list.size > 0 ? list[0] : nil
end
|
39
40
41
42
|
# File 'lib/RubyExt/Path.rb', line 39
def next;
p = sub(/[^\/]+\/?/, "")
return p.empty? ? nil : p
end
|
32
33
34
35
36
37
|
# File 'lib/RubyExt/Path.rb', line 32
def previous;
return nil if empty?
p = sub(/[^\/]+?$/, "" )
p = p[0..p.string_size-2] if p.string_size > 1
return p
end
|
#relative? ⇒ Boolean
12
13
14
|
# File 'lib/RubyExt/Path.rb', line 12
def relative?;
!absolute?
end
|
#simple? ⇒ Boolean
18
19
20
|
# File 'lib/RubyExt/Path.rb', line 18
def simple?;
self =~ /^\/?[^\/]*$/ ? true : false
end
|
84
|
# File 'lib/RubyExt/Path.rb', line 84
def size() to_a.size end
|
#string_each ⇒ Object
78
|
# File 'lib/RubyExt/Path.rb', line 78
alias_method :string_each, :each
|
#string_plus ⇒ Object
69
|
# File 'lib/RubyExt/Path.rb', line 69
alias_method :string_plus, :+
|
#string_size ⇒ Object
83
|
# File 'lib/RubyExt/Path.rb', line 83
alias_method :string_size, :size
|
86
|
# File 'lib/RubyExt/Path.rb', line 86
def to_a; @elements ||= to_relative.split('/') end
|
#to_absolute ⇒ Object
74
|
# File 'lib/RubyExt/Path.rb', line 74
def to_absolute; Path.new(absolute? ? self : "/#{self}") end
|
#to_relative ⇒ Object
76
|
# File 'lib/RubyExt/Path.rb', line 76
def to_relative; Path.new sub(/^\//, "") end
|
88
89
90
|
# File 'lib/RubyExt/Path.rb', line 88
def to_s
return String.new(self)
end
|