Class: Windirs::Path
- Inherits:
-
Object
- Object
- Windirs::Path
- Defined in:
- lib/windirs.rb
Instance Method Summary collapse
-
#cygwin(cygdrive_prefix = '/cygdrive/') ⇒ Object
Return a Cygwin style path String.
-
#initialize(path, cygdrive_prefix = '/cygdrive/') ⇒ Path
constructor
Create a new Path from
path
. -
#nix(prefix = '/') ⇒ Object
Return a *nix (Linux, Mac OS X, *BSD) style path String.
-
#rubywin ⇒ Object
Return a Ruby on Windows style path String.
-
#win_deref ⇒ Object
Return a new Windirs::Path with any Windows network mapped drives dereferenced to their UNC.
-
#windows ⇒ Object
Return a Windows style path String.
Constructor Details
#initialize(path, cygdrive_prefix = '/cygdrive/') ⇒ Path
Create a new Path from path
.
> winpath = Windirs::Path.new 'C:\Users\noah\Desktop'
> nixpath = Windirs::Path.new '/bin/ls'
> cygpath = Windirs::Path.new '/cygdrive/c/Users/noah/Desktop'
You probably don’t need to know the rest of this: cygdrive_prefix
defaults to ‘/cygdrive/’, and is used to extract drive letters from Cygwin-ish looking paths. If you know will not be using Cygwin paths, you may wish to assign this to ”. If your cygdrive path prefix is something else, you should assign this appropriately. See cygwin.com/cygwin-ug-net/using.html#cygdrive for details. Note that we are using a trailing slash, unlike the Cygwin documentation.
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/windirs.rb', line 54 def initialize path, cygdrive_prefix = '/cygdrive/' dirs = '(?<dirs>.*)$' windrive = '((?<drive>[A-Z]):)' cygdrive = "(#{cygdrive_prefix}(?<drive>[a-z]))" drive = "(#{windrive}|#{cygdrive})?" path_re = Regexp.new('^' + drive + dirs) if path_re =~ path m = Regexp.last_match @drive = m[:drive] @dirs = m[:dirs] end end |
Instance Method Details
#cygwin(cygdrive_prefix = '/cygdrive/') ⇒ Object
Return a Cygwin style path String. See Windirs.Path.initialize documentation for more info on cygdrive_prefix
.
> winpath.cygwin
=> "/cygdrive/c/Users/noah/Desktop"
76 77 78 |
# File 'lib/windirs.rb', line 76 def cygwin cygdrive_prefix = '/cygdrive/' fpath('/', cygdrive_prefix){|drive| "#{@drive.downcase}"} end |
#nix(prefix = '/') ⇒ Object
Return a *nix (Linux, Mac OS X, *BSD) style path String. prefix
will be used if a drive was detected in Path, but this is unlikely to be useful. You may want to assign it for certain Samba situations, but I have little opinion or knowledge on that.
> winpath.nix
=> "/c/Users/noah/Desktop"
90 91 92 |
# File 'lib/windirs.rb', line 90 def nix prefix = '/' fpath('/', prefix){|drive| "#{@drive.downcase}"} end |
#rubywin ⇒ Object
Return a Ruby on Windows style path String.
> winpath.rubywin
=> "C:/Users/noah/Desktop"
100 101 102 |
# File 'lib/windirs.rb', line 100 def rubywin fpath('/'){ |drive| "#{@drive.upcase}:"} end |
#win_deref ⇒ Object
Return a new Windirs::Path with any Windows network mapped drives dereferenced to their UNC. Returns self
if not on Windows, or self
is not on a network mapped drive.
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/windirs.rb', line 123 def win_deref return self unless ENV['OS'] == 'Windows_NT' # FIXME try to get path to net if on Cygwin and can't see net net_use = `net use #{@drive}: 2>&1` return self unless $?.exitstatus == 0 remote_line = net_use.split("\r\n").select{|l| l =~ /^Remote name\s*/} return self unless remote_line.length == 1 drive = remote_line[0].sub(/^Remote name\s*/, '') Path.new "#{drive}#{@dirs}" end |
#windows ⇒ Object
Return a Windows style path String.
> cygpath.windows
=> "C:\\Users\\noah\\Desktop"
> puts cygpath.windows
"C:\Users\noah\Desktop"
=> nil
113 114 115 |
# File 'lib/windirs.rb', line 113 def windows fpath('\\'){ |drive| "#{@drive.upcase}:"} end |