Class: Path
- Inherits:
-
Object
- Object
- Path
- Defined in:
- lib/path2.rb,
lib/path2/version.rb
Constant Summary collapse
- VERSION =
"0.2.3"
Instance Method Summary collapse
- #basename ⇒ Object
- #current ⇒ Object
- #directory? ⇒ Boolean
- #dirname ⇒ Object
- #entries ⇒ Object
- #exists?(path = nil) ⇒ Boolean
- #expand_path! ⇒ Object
- #file? ⇒ Boolean
- #find(arg) ⇒ Object
- #grep(pattern) ⇒ Object
- #ignore(path) ⇒ Object
-
#initialize(*args) ⇒ Path
constructor
A new instance of Path.
- #inspect ⇒ Object
- #join(path) ⇒ Object
- #push(*paths) ⇒ Object (also: #<<)
- #recursive! ⇒ Object
- #reject(pattern) ⇒ Object
- #reload ⇒ Object
- #reload! ⇒ Object
- #size ⇒ Object
- #split ⇒ Object
- #stat ⇒ Object
- #to_s ⇒ Object
- #tree(path = nil) ⇒ Object
- #walk(path) ⇒ Object
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. @options[:recursive] ||= false @options[:expand_path] ||= false @root = (args.shift || Dir.pwd) @entries = tree args.each &method(:<<) end |
Instance Method Details
#basename ⇒ Object
44 45 46 |
# File 'lib/path2.rb', line 44 def basename File.basename(current) end |
#current ⇒ Object
52 53 54 |
# File 'lib/path2.rb', line 52 def current File.(@root) end |
#directory? ⇒ Boolean
36 37 38 |
# File 'lib/path2.rb', line 36 def directory? File.directory?(current) end |
#dirname ⇒ Object
40 41 42 |
# File 'lib/path2.rb', line 40 def dirname directory? ? current : File.dirname(current) end |
#entries ⇒ Object
86 87 88 |
# File 'lib/path2.rb', line 86 def entries @entries ||= [] end |
#exists?(path = nil) ⇒ 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 @options[:expand_path] = true reload! end |
#file? ⇒ 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 |
#inspect ⇒ Object
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 |
#reload! ⇒ Object
60 61 62 |
# File 'lib/path2.rb', line 60 def reload! @entries = tree end |
#size ⇒ Object
24 25 26 |
# File 'lib/path2.rb', line 24 def size directory? ? entries.size : File.size(current) end |
#split ⇒ Object
48 49 50 |
# File 'lib/path2.rb', line 48 def split File.split(current) end |
#stat ⇒ Object
32 33 34 |
# File 'lib/path2.rb', line 32 def stat File.stat(current) end |
#to_s ⇒ Object
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 |