Class: Path

Inherits:
String show all
Defined in:
lib/camino.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from String

#to_path

Constructor Details

#initialize(string = Path.root) ⇒ Path

Returns a new instance of Path.



3
4
5
# File 'lib/camino.rb', line 3

def initialize(string = Path.root)
  super(string)
end

Class Method Details

.homeObject



72
73
74
# File 'lib/camino.rb', line 72

def self.home
  Path.new(ENV['HOME'])
end

.pwdObject



80
81
82
# File 'lib/camino.rb', line 80

def self.pwd
  Path.new(Dir.pwd)
end

.relativeObject



65
66
67
68
69
70
# File 'lib/camino.rb', line 65

def self.relative
  path = get_path_from_caller(caller)
  dir = File.dirname(path)
  raise "Path doesn't seem to exist:#{last_call.inspect}" if !File.exist?(dir)
  Path.new(dir)
end

.rootObject



76
77
78
# File 'lib/camino.rb', line 76

def self.root
  Path.new("/")
end

Instance Method Details

#+(obj) ⇒ Object



11
12
13
# File 'lib/camino.rb', line 11

def +(obj)
  Path.new(self.to_s + obj.to_s)
end

#/(obj) ⇒ Object



7
8
9
# File 'lib/camino.rb', line 7

def /(obj)
  Path.new(File.join(self, obj.to_s))
end

#directoryObject



45
46
47
# File 'lib/camino.rb', line 45

def directory
  File.dirname(self)
end

#directory?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/camino.rb', line 21

def directory?
  File.directory?(self)
end

#exist?Boolean Also known as: exists?

Returns:

  • (Boolean)


15
16
17
# File 'lib/camino.rb', line 15

def exist?
  File.exists?(self)
end

#extensionObject



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

def extension
  File.extname(self)[1..-1]
end

#fileObject



49
50
51
# File 'lib/camino.rb', line 49

def file
  File.basename(self)
end

#file?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/camino.rb', line 25

def file?
  exist? && !directory?
end

#filenameObject



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

def filename
  File.basename(self, File.extname(self))
end

#files(pattern = "*") ⇒ Object



29
30
31
32
33
34
35
# File 'lib/camino.rb', line 29

def files(pattern = "*")
  if directory?
    Dir.glob(File.join(self, pattern)).collect{|p|Path.new(p)}
  else
    raise "Not a directory:(#{self})"
  end
end

#inspectObject



61
62
63
# File 'lib/camino.rb', line 61

def inspect
  "#<Path:#{(object_id * 2).to_s(16)}:#{self}>"
end

#readObject



37
38
39
40
41
42
43
# File 'lib/camino.rb', line 37

def read
  if file?
    File.open(self).read
  else
    raise "Not a file:(#{self})"
  end
end