Class: Ro::Path
Direct Known Subclasses
Class Method Summary collapse
- .absolute ⇒ Object
- .absolute?(arg, *args) ⇒ Boolean
- .clean(arg, *args) ⇒ Object
- .expand(arg, *args) ⇒ Object
- .relative ⇒ Object
- .relative?(arg, *args) ⇒ Boolean
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #absolute ⇒ Object
- #base ⇒ Object
- #basename ⇒ Object (also: #name)
- #binwrite(data) ⇒ Object
- #child?(other) ⇒ Boolean
- #detect(&block) ⇒ Object
- #directory? ⇒ Boolean
- #extension ⇒ Object (also: #ext)
- #file? ⇒ Boolean
- #files(arg = '**/**', *args, **kws, &block) ⇒ Object
- #glob(arg = '**/**', *args, **kws, &block) ⇒ Object (also: #ls)
-
#initialize(arg, *args) ⇒ Path
constructor
A new instance of Path.
- #join(arg, *args) ⇒ Object
- #key ⇒ Object
- #klass ⇒ Object
- #parent ⇒ Object (also: #dirname)
- #parent?(other) ⇒ Boolean
- #parts ⇒ Object
- #pn ⇒ Object
- #relative ⇒ Object
- #relative_from ⇒ Object
- #relative_to(other) ⇒ Object
- #relative_to!(other) ⇒ Object
- #select(&block) ⇒ Object
- #sibling?(other) ⇒ Boolean
- #sort_key ⇒ Object
- #stat ⇒ Object
- #subdirectories(&block) ⇒ Object (also: #subdirs)
- #subdirectory?(subdirectory) ⇒ Boolean (also: #subdir?)
- #subdirectory_for(subdirectory) ⇒ Object (also: #subdir_for)
Methods included from Klass
Methods inherited from String
Constructor Details
#initialize(arg, *args) ⇒ Path
Returns a new instance of Path.
33 34 35 |
# File 'lib/ro/path.rb', line 33 def initialize(arg, *args) super Path.clean(arg, *args) end |
Class Method Details
.absolute ⇒ Object
16 17 18 |
# File 'lib/ro/path.rb', line 16 def absolute(...) new(...).absolute end |
.absolute?(arg, *args) ⇒ Boolean
20 21 22 |
# File 'lib/ro/path.rb', line 20 def absolute?(arg, *args) Path.for(arg, *args).absolute? end |
.clean(arg, *args) ⇒ Object
8 9 10 |
# File 'lib/ro/path.rb', line 8 def clean(arg, *args) Pathname.new([arg, *args].join('/')).cleanpath.to_s end |
.expand(arg, *args) ⇒ Object
12 13 14 |
# File 'lib/ro/path.rb', line 12 def (arg, *args) new(Pathname.new(clean(arg, *args).)) end |
.relative ⇒ Object
24 25 26 |
# File 'lib/ro/path.rb', line 24 def relative(...) new(...).relative end |
.relative?(arg, *args) ⇒ Boolean
28 29 30 |
# File 'lib/ro/path.rb', line 28 def relative?(arg, *args) Path.for(arg, *args).relative? end |
Instance Method Details
#<=>(other) ⇒ Object
221 222 223 |
# File 'lib/ro/path.rb', line 221 def <=>(other) sort_key <=> other.sort_key end |
#absolute ⇒ Object
65 66 67 |
# File 'lib/ro/path.rb', line 65 def absolute Path.new('/' + self) end |
#base ⇒ Object
150 151 152 153 |
# File 'lib/ro/path.rb', line 150 def base base, ext = basename.split('.', 2) base end |
#basename ⇒ Object Also known as: name
145 146 147 |
# File 'lib/ro/path.rb', line 145 def basename Path.for(File.basename(self)) end |
#binwrite(data) ⇒ Object
165 166 167 168 |
# File 'lib/ro/path.rb', line 165 def binwrite(data) FileUtils.mkdir_p(dirname) IO.binwrite(self, data) end |
#child?(other) ⇒ Boolean
174 175 176 177 178 |
# File 'lib/ro/path.rb', line 174 def child?(other) parent = child = Path.for(other). (parent.size < child.size && child.start_with?(parent)) end |
#detect(&block) ⇒ Object
136 137 138 |
# File 'lib/ro/path.rb', line 136 def detect(&block) glob.detect(&block) end |
#directory? ⇒ Boolean
190 191 192 |
# File 'lib/ro/path.rb', line 190 def directory? test(?d, self) end |
#extension ⇒ Object Also known as: ext
155 156 157 158 |
# File 'lib/ro/path.rb', line 155 def extension base, ext = basename.split('.', 2) ext end |
#file? ⇒ Boolean
186 187 188 |
# File 'lib/ro/path.rb', line 186 def file? test(?f, self) end |
#files(arg = '**/**', *args, **kws, &block) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ro/path.rb', line 118 def files(arg = '**/**', *args, **kws, &block) glob = Path.for(self, arg, *args, **kws) accum = [] Dir.glob(glob) do |entry| next unless test(?f, entry) path = Path.new(entry) block ? block.call(path) : accum.push(path) end accum end |
#glob(arg = '**/**', *args, **kws, &block) ⇒ Object Also known as: ls
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/ro/path.rb', line 104 def glob(arg = '**/**', *args, **kws, &block) glob = Path.for(self, arg, *args, **kws) accum = [] Dir.glob(glob) do |entry| path = Path.new(entry) block ? block.call(path) : accum.push(path) end accum end |
#join(arg, *args) ⇒ Object
161 162 163 |
# File 'lib/ro/path.rb', line 161 def join(arg, *args) Path.for(self, Path.clean(arg, *args)) end |
#key ⇒ Object
83 84 85 |
# File 'lib/ro/path.rb', line 83 def key parts end |
#klass ⇒ Object
41 42 43 |
# File 'lib/ro/path.rb', line 41 def klass self.class end |
#parent ⇒ Object Also known as: dirname
140 141 142 |
# File 'lib/ro/path.rb', line 140 def parent Path.for(File.dirname(self)) end |
#parent?(other) ⇒ Boolean
180 181 182 183 184 |
# File 'lib/ro/path.rb', line 180 def parent?(other) child = parent = Path.for(other). (parent.size < child.size && child.start_with?(parent)) end |
#parts ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/ro/path.rb', line 73 def parts parts = scan(%r`[^/]+`) if absolute? %w[ / ] + parts else parts end end |
#pn ⇒ Object
37 38 39 |
# File 'lib/ro/path.rb', line 37 def pn Pathname.new(self) end |
#relative ⇒ Object
69 70 71 |
# File 'lib/ro/path.rb', line 69 def relative Path.new(absolute.gsub(%r{^/+}, '')) end |
#relative_from ⇒ Object
93 94 95 |
# File 'lib/ro/path.rb', line 93 def relative_from(...) relative_to(...) end |
#relative_to(other) ⇒ Object
87 88 89 90 91 |
# File 'lib/ro/path.rb', line 87 def relative_to(other) a = Pathname.new(self). b = Pathname.new(other). Path.for(a.relative_path_from(b)) end |
#relative_to!(other) ⇒ Object
97 98 99 100 101 102 |
# File 'lib/ro/path.rb', line 97 def relative_to!(other) a = Pathname.new(self).realpath b = Pathname.new(other).realpath Path.for(a.relative_path_from(b)) end |
#select(&block) ⇒ Object
132 133 134 |
# File 'lib/ro/path.rb', line 132 def select(&block) glob.select(&block) end |
#sibling?(other) ⇒ Boolean
170 171 172 |
# File 'lib/ro/path.rb', line 170 def sibling?(other) .dirname == Path.for(other)..dirname end |
#sort_key ⇒ Object
217 218 219 |
# File 'lib/ro/path.rb', line 217 def sort_key [dirname.to_s, basename.to_s] end |
#stat ⇒ Object
225 226 227 |
# File 'lib/ro/path.rb', line 225 def stat File.stat(self) end |
#subdirectories(&block) ⇒ Object Also known as: subdirs
194 195 196 197 198 199 200 201 202 203 |
# File 'lib/ro/path.rb', line 194 def subdirectories(&block) accum = [] glob('*') do |entry| next unless entry.directory? block ? block.call(entry) : accum.push(entry) end block ? self : accum end |
#subdirectory?(subdirectory) ⇒ Boolean Also known as: subdir?
211 212 213 214 |
# File 'lib/ro/path.rb', line 211 def subdirectory?(subdirectory) subdirectory = join(Path.relative(subdirectory)) subdirectory.exist? end |
#subdirectory_for(subdirectory) ⇒ Object Also known as: subdir_for
206 207 208 |
# File 'lib/ro/path.rb', line 206 def subdirectory_for(subdirectory) join(Path.relative(subdirectory)) end |