Class: Ro::Path

Inherits:
String show all
Includes:
Klass
Defined in:
lib/ro/path.rb

Direct Known Subclasses

Root

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Klass

included

Methods inherited from String

#html_safe

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

.absoluteObject



16
17
18
# File 'lib/ro/path.rb', line 16

def absolute(...)
  new(...).absolute
end

.absolute?(arg, *args) ⇒ Boolean

Returns:

  • (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 expand(arg, *args)
  new(Pathname.new(clean(arg, *args).expand_path))
end

.relativeObject



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

def relative(...)
  new(...).relative
end

.relative?(arg, *args) ⇒ Boolean

Returns:

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

#absoluteObject



65
66
67
# File 'lib/ro/path.rb', line 65

def absolute
  Path.new('/' + self)
end

#baseObject



150
151
152
153
# File 'lib/ro/path.rb', line 150

def base
  base, ext = basename.split('.', 2)
  base
end

#basenameObject 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

Returns:

  • (Boolean)


174
175
176
177
178
# File 'lib/ro/path.rb', line 174

def child?(other)
  parent = expand
  child = Path.for(other).expand
  (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

Returns:

  • (Boolean)


190
191
192
# File 'lib/ro/path.rb', line 190

def directory?
  test(?d, self)
end

#extensionObject 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

Returns:

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

#keyObject



83
84
85
# File 'lib/ro/path.rb', line 83

def key
  parts
end

#klassObject



41
42
43
# File 'lib/ro/path.rb', line 41

def klass
  self.class
end

#parentObject 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

Returns:

  • (Boolean)


180
181
182
183
184
# File 'lib/ro/path.rb', line 180

def parent?(other)
  child = expand
  parent = Path.for(other).expand
  (parent.size < child.size && child.start_with?(parent))
end

#partsObject



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

#pnObject



37
38
39
# File 'lib/ro/path.rb', line 37

def pn
  Pathname.new(self)
end

#relativeObject



69
70
71
# File 'lib/ro/path.rb', line 69

def relative
  Path.new(absolute.gsub(%r{^/+}, ''))
end

#relative_fromObject



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).expand_path
  b = Pathname.new(other).expand_path
  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

Returns:

  • (Boolean)


170
171
172
# File 'lib/ro/path.rb', line 170

def sibling?(other)
  expand.dirname == Path.for(other).expand.dirname
end

#sort_keyObject



217
218
219
# File 'lib/ro/path.rb', line 217

def sort_key
  [dirname.to_s, basename.to_s]
end

#statObject



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?

Returns:

  • (Boolean)


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