Class: Path

Inherits:
String show all
Defined in:
lib/RubyExt/Path.rb

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

#+(o) ⇒ Object



70
71
72
# File 'lib/RubyExt/Path.rb', line 70

def + o
  add(o)
end

#absolute?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


16
# File 'lib/RubyExt/Path.rb', line 16

def empty?; self == '' || self == '/' end

#firstObject



44
45
46
# File 'lib/RubyExt/Path.rb', line 44

def first;
  Path.new(scan(/^\/?[^\/]*/)[0])
end

#first_nameObject



57
58
59
60
# File 'lib/RubyExt/Path.rb', line 57

def first_name
  list = scan(/[^\/]+/)
  return list.size > 0 ? list[0] : nil
end

#lastObject



48
49
50
# File 'lib/RubyExt/Path.rb', line 48

def last;
  Path.new(sub(/[^\/].+\//, ""))
end

#last_nameObject



52
53
54
55
# File 'lib/RubyExt/Path.rb', line 52

def last_name
  list = scan(/[^\/]+$/)
  return list.size > 0 ? list[0] : nil
end

#nextObject



39
40
41
42
# File 'lib/RubyExt/Path.rb', line 39

def next;
  p = sub(/[^\/]+\/?/, "")
  return p.empty? ? nil : p
end

#previousObject



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

Returns:

  • (Boolean)


12
13
14
# File 'lib/RubyExt/Path.rb', line 12

def relative?;
  !absolute?
end

#simple?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/RubyExt/Path.rb', line 18

def simple?;
  self =~ /^\/?[^\/]*$/ ? true : false
end

#sizeObject



84
# File 'lib/RubyExt/Path.rb', line 84

def size() to_a.size end

#string_eachObject



78
# File 'lib/RubyExt/Path.rb', line 78

alias_method :string_each, :each

#string_plusObject



69
# File 'lib/RubyExt/Path.rb', line 69

alias_method :string_plus, :+

#string_sizeObject



83
# File 'lib/RubyExt/Path.rb', line 83

alias_method :string_size, :size

#to_aObject



86
# File 'lib/RubyExt/Path.rb', line 86

def to_a; @elements ||= to_relative.split('/') end

#to_absoluteObject



74
# File 'lib/RubyExt/Path.rb', line 74

def to_absolute; Path.new(absolute? ? self : "/#{self}") end

#to_relativeObject



76
# File 'lib/RubyExt/Path.rb', line 76

def to_relative; Path.new sub(/^\//, "") end

#to_sObject



88
89
90
# File 'lib/RubyExt/Path.rb', line 88

def to_s
  return String.new(self)
end