Class: Path

Inherits:
Object
  • Object
show all
Defined in:
lib/path2.rb,
lib/path2/version.rb

Constant Summary collapse

VERSION =
"0.2.3"

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Path

Returns a new instance of Path.



5
6
7
8
9
10
11
12
# File 'lib/path2.rb', line 5

def initialize(*args)
  @options                = args.extract_options!
  @options[:recursive]    ||= false
  @options[:expand_path]  ||= false
  @root                   = (args.shift || Dir.pwd)
  @entries                = tree
  args.each &method(:<<)
end

Instance Method Details

#basenameObject



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

def basename
  File.basename(current)
end

#currentObject



52
53
54
# File 'lib/path2.rb', line 52

def current
  File.expand_path(@root)
end

#directory?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/path2.rb', line 36

def directory?
  File.directory?(current)
end

#dirnameObject



40
41
42
# File 'lib/path2.rb', line 40

def dirname
  directory? ? current : File.dirname(current)
end

#entriesObject



86
87
88
# File 'lib/path2.rb', line 86

def entries
  @entries ||= []
end

#exists?(path = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

def exists?(path = nil)
  File.exists?((path ? [current, path].join("/") : current))
end

#expand_path!Object



19
20
21
22
# File 'lib/path2.rb', line 19

def expand_path!
  @options[:expand_path] = true
  reload!
end

#file?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/path2.rb', line 28

def file?
  !directory?
end

#find(arg) ⇒ Object



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

def find(arg)
  grep(/#{arg}/)
end

#grep(pattern) ⇒ Object



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

def grep(pattern)
  entries.grep(pattern)
end

#ignore(path) ⇒ Object



97
98
99
# File 'lib/path2.rb', line 97

def ignore(path)
  reject(/#{path}/)
end

#inspectObject



110
111
112
# File 'lib/path2.rb', line 110

def inspect
  "#<Path @root=\"#{current}\" >"
end

#join(path) ⇒ Object



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

def join(path)
  Path.new([@root, path].join("/"), @options)
end

#push(*paths) ⇒ Object Also known as: <<



90
91
92
93
94
# File 'lib/path2.rb', line 90

def push(*paths)
  paths.each do |path|
    entries.concat(Path.new(path, @options).tree).uniq!
  end
end

#recursive!Object



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

def recursive!
  @options[:recursive] = true
  reload!
end

#reject(pattern) ⇒ Object



101
102
103
104
# File 'lib/path2.rb', line 101

def reject(pattern)
  @entries = (entries - grep(pattern))
  self
end

#reloadObject



64
65
66
# File 'lib/path2.rb', line 64

def reload
  Path.new(@root, @options)
end

#reload!Object



60
61
62
# File 'lib/path2.rb', line 60

def reload!
  @entries = tree
end

#sizeObject



24
25
26
# File 'lib/path2.rb', line 24

def size
  directory? ? entries.size : File.size(current)
end

#splitObject



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

def split
  File.split(current)
end

#statObject



32
33
34
# File 'lib/path2.rb', line 32

def stat
  File.stat(current)
end

#to_sObject



106
107
108
# File 'lib/path2.rb', line 106

def to_s
  current
end

#tree(path = nil) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/path2.rb', line 114

def tree(path = nil)
  tree = []
  Dir.foreach((path ||= (!@options[:expand_path] ? @root : dirname))) do |entry|
    next if ['..','.'].include?(entry)
    entry = File.join(path, entry)
    if @options[:recursive] && File.directory?(entry)
      tree << tree(entry)
    else
      tree << entry
    end
  end
  tree.flatten
end

#walk(path) ⇒ Object



80
81
82
83
84
# File 'lib/path2.rb', line 80

def walk(path)
  yield join(path)
ensure
  self
end