Class: RIO::RL::WithPath

Inherits:
Base show all
Includes:
Error::NotImplemented, PathUtil
Defined in:
lib/rio/rl/withpath.rb,
lib/rio/rl/withpath.rb

Direct Known Subclasses

URIBase

Constant Summary collapse

SCHEME =
URI::REGEXP::PATTERN::SCHEME
HOST =
URI::REGEXP::PATTERN::HOST

Instance Attribute Summary

Attributes inherited from Base

#fs

Instance Method Summary collapse

Methods included from Error::NotImplemented

#nodef

Methods inherited from Base

#==, #===, #=~, #callstr, #close, #escape, #fs2url, #initialize, #initialize_copy, is_riorl?, #length, parse, #rl, split_riorl, subscheme, #to_rl, #to_s, #unescape, #url, #url2fs

Constructor Details

This class inherits a constructor from RIO::RL::Base

Instance Method Details

#_build(*args) ⇒ Object



278
# File 'lib/rio/rl/withpath.rb', line 278

def _build(*args) RIO::RL::Builder.build(*args) end

#_partsObject

returns an array of parts of a RL. ‘a/b/c’ => [‘a’,‘b’,‘c’] For absolute paths the first component is the pathroot ‘/topdir/dir/file’ => [‘/’,‘topdir’,‘dir’,‘file’] ‘host/dir/file’ => [‘host/’,‘dir’,‘file’] ‘//host/a/b’ => [‘file://host/’,‘a’,‘b’] each element of the array is an RL whose base is set such that the correct absolute path would be returned returns an array of RLs



190
191
192
193
194
195
196
# File 'lib/rio/rl/withpath.rb', line 190

def _parts()
  pr = self.pathroot
  ur = self.urlroot.sub(/#{pr}$/,'')
  up = self.urlpath.sub(/^#{pr}/,'')

  [ur,pr,up]
end

#_uri(arg) ⇒ Object



158
159
160
# File 'lib/rio/rl/withpath.rb', line 158

def _uri(arg)
  arg.kind_of?(::URI) ? arg.clone : ::URI.parse(arg.to_s)
end

#abs(thebase = nil) ⇒ Object

returns the absolute path. combines the urlpath with the argument, or the value returned by base() to create an absolute path. returns a RL



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/rio/rl/withpath.rb', line 165

def abs(thebase=nil) 
  thebase ||= self.base
  base_uri = _uri(thebase)
  path_uri = self.uri.clone
  #p "abs: base_uri=#{base_uri.inspect}"
  #p "abs: path_uri=#{path_uri.inspect}"
  if path_uri.scheme == 'file' and base_uri.scheme != 'file'
    abs_uri = base_uri.merge(path_uri.path)
  else
    abs_uri = base_uri.merge(path_uri)
  end
  #p "abs: abs_uri=#{abs_uri.inspect}"
  _build(abs_uri,{:fs => self.fs})
end

#base(thebase = nil) ⇒ Object

returns the base of a path. merging the value returned with this yields the absolute path



105
# File 'lib/rio/rl/withpath.rb', line 105

def base(thebase=nil) nodef(thebase) end

#basename(ext) ⇒ Object

returns the tail portion of the path minus the extension returns a RL



248
249
250
251
252
253
254
# File 'lib/rio/rl/withpath.rb', line 248

def basename(ext)
  #p callstr('basename',ext)
  base_rl = self.abs
  base_rl.fspath = fs.dirname(base_rl.fspath_no_slash)
  path_str = fs.basename(self.fspath_no_slash,ext)
  _build(path_str,{:base => base_rl.uri, :fs => self.fs})
end

#build_arg0_(path_str) ⇒ Object



255
256
257
# File 'lib/rio/rl/withpath.rb', line 255

def build_arg0_(path_str)
  path_str
end

#dirnameObject

returns the directory portion of the path like File#dirname returns a RL



234
235
236
237
238
239
240
241
242
243
244
# File 'lib/rio/rl/withpath.rb', line 234

def dirname() 
  new_rl = self.clone
  #p "dirname: fspath_no_slash(#{self.fspath_no_slash} dn(#{fs.dirname(self.fspath_no_slash)})"
  pth = self.fspath_no_slash
  if pth =~ %r{^//(#{HOST})(/.*)}
    new_rl.fspath = "//#{$1}#{fs.dirname($2)}"
  else
    new_rl.fspath = fs.dirname(pth)
  end
  new_rl
end

#filenameObject

returns the tail portion of the path returns a RL



261
262
263
# File 'lib/rio/rl/withpath.rb', line 261

def filename() 
  basename('')
end

#fspathObject

returns the path as the file system sees it. Spaces are spaces and not %20 etc. This is the path that would be passed to the fs object. For windows RLs this includes the ‘//host’ part and the ‘C:’ part returns a String



121
122
123
# File 'lib/rio/rl/withpath.rb', line 121

def fspath() 
  RL.url2fs(self.urlpath)
end

#fspath=(fpth) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/rio/rl/withpath.rb', line 125

def fspath=(fpth)
  #p "FSPATH= #{fpth} => #{RL.fs2url(fpth)}"
  case fpth
  when %r{^//(#{HOST})(/.*)?$}
    self.host = $1
    self.urlpath = RL.fs2url($2||'')
  else
    self.urlpath = RL.fs2url(fpth)
  end
end

#fspath_no_slashObject

The value of fspath() with any trailing slash removed returns a String



148
149
150
151
152
# File 'lib/rio/rl/withpath.rb', line 148

def fspath_no_slash() 
  pth = self.fspath
  #p "path_no_slash: #{is_root?(pth)} #{pth}"
  is_root?(pth) ? pth : pth.sub(/\/$/,'')
end

#hostObject

returns the host portion of the URI if their is one otherwise it returns nil returns a String



83
# File 'lib/rio/rl/withpath.rb', line 83

def host() nodef() end

#host=(arg) ⇒ Object



84
# File 'lib/rio/rl/withpath.rb', line 84

def host=(arg) nodef(arg) end

#is_root?(upth) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/rio/rl/withpath.rb', line 135

def is_root?(upth)
  upth =~ %r%^(/?[a-zA-Z]:)?/% or upth =~ %r%^//(#{HOST})%
end

#join(*args) ⇒ Object

changes this RLs path so that is consists of this RL’s path combined with those of its arguments.



218
219
220
221
222
223
# File 'lib/rio/rl/withpath.rb', line 218

def join(*args)
  return self if args.empty?
  #sa = args.map { |arg| ::URI.escape(arg.to_s,ESCAPE) }
  sa = args.map { |arg| arg.to_s }
  join_(sa)
end

#merge(other) ⇒ Object

calls URI#merge returns a RL



268
# File 'lib/rio/rl/withpath.rb', line 268

def merge(other) _build(self.uri.merge(other.uri)) end

#opaqueObject

returns the portion of the URL starting after the colon following the scheme, and ending before the query portion of the URL returns a String



90
# File 'lib/rio/rl/withpath.rb', line 90

def opaque() nodef() end

#openfs_Object

returns an appriate FS object for the scheme



56
# File 'lib/rio/rl/withpath.rb', line 56

def openfs_() nodef() end

#pathObject

For RLs that are on the file system this is fspath() For RLs that are remote (http,ftp) this is urlpath() For RLs that have no path this is nil returns a String



67
# File 'lib/rio/rl/withpath.rb', line 67

def path() nodef{} end

#path=(arg) ⇒ Object



68
# File 'lib/rio/rl/withpath.rb', line 68

def path=(arg) nodef(arg) end

#path_no_slashObject

The value of urlpath() with any trailing slash removed returns a String



141
142
143
144
145
# File 'lib/rio/rl/withpath.rb', line 141

def path_no_slash() 
  pth = self.urlpath
  #p "path_no_slash: #{is_root?(pth)} #{pth}"
  is_root?(pth) ? pth : pth.sub(/\/$/,'')
end

#pathdepthObject



153
154
155
156
# File 'lib/rio/rl/withpath.rb', line 153

def pathdepth()
  pth = self.path_no_slash
  is_root?(pth) ? 0 : pth.count('/')
end

#pathrootObject

returns the portion of the path that when prepended to the path would make it usable. For paths on the file system this would be ‘/’ For http and ftp paths it would be host/ For zipfile paths it would be ” For windows paths with a drive it would be ‘C:/’ For windows UNC paths it would be ‘//host/’ returns a String



100
# File 'lib/rio/rl/withpath.rb', line 100

def pathroot() nodef() end

#route_from(other) ⇒ Object

calls URI#route_from returns a RL



272
# File 'lib/rio/rl/withpath.rb', line 272

def route_from(other) _build(self.uri.route_from(other.uri),{:base => other.uri}) end

#route_to(other) ⇒ Object

calls URI#route_to returns a RL



276
# File 'lib/rio/rl/withpath.rb', line 276

def route_to(other) _build(self.uri.route_to(other.uri),{:base => self.uri}) end

#schemeObject

when the URL is legal it is the URI scheme otherwise it is one of Rio’s schemes returns a String



78
# File 'lib/rio/rl/withpath.rb', line 78

def scheme() nodef() end

#splitObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/rio/rl/withpath.rb', line 197

def split()
  if absolute?
    parts = self._parts
    sparts = []
    sparts << parts[0] + parts[1]
    sparts += parts[2].split('/')
  else
    sparts = self.urlpath.split('/')
  end
  rlparts = sparts.map { |str| self.class.new(str) }
  (1...sparts.length).each { |i|
    base_str = rlparts[i-1].abs.url
    base_str += '/' unless base_str[-1] == ?/
    rlparts[i].base = base_str
  }
  rlparts

end

#uriObject

returns A URI object representation of the RL if one exists otherwise it returns nil returns a URI



73
# File 'lib/rio/rl/withpath.rb', line 73

def uri() nodef() end

#uri_from_string_(str) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
# File 'lib/rio/rl/withpath.rb', line 280

def uri_from_string_(str)
  case str
  when %r%^file://(#{HOST})?(/.*)?$% then ::URI.parse(str)
  when %r/^[a-zA-Z]:/ then 
    ::URI.parse(str)
  when %r/^#{SCHEME}:/ then ::URI.parse(str)
  #when %r{^/} then ::URI.parse('file://'+str+( ( str[-1,0] == '/' ) ? "" : "/"))
  when %r{^/} then ::URI.parse('file://'+RL.fs2url(str))
  else ::URI.parse(str)
  end
end

#urlpathObject

returns the path portion of a URL. All spaces would be %20 returns a String



60
# File 'lib/rio/rl/withpath.rb', line 60

def urlpath() nodef() end

#urlpath=(arg) ⇒ Object



61
# File 'lib/rio/rl/withpath.rb', line 61

def urlpath=(arg) nodef(arg) end