Class: Pathname

Inherits:
Object show all
Defined in:
lib/shorthand.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Pathname

Returns a new instance of Pathname.



105
# File 'lib/shorthand.rb', line 105

def initialize(*args) old_init(*args); @rc={}; @rc2={} end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *a, &b) ⇒ Object



161
# File 'lib/shorthand.rb', line 161

def method_missing(m,*a,&b) to_s.respond_to?(m) ? to_s.send(m,*a,&b) : old_mm(m,*a,&b) end

Class Method Details

.[](p) ⇒ Object



103
# File 'lib/shorthand.rb', line 103

def self.[](p) Path.new(p) end

Instance Method Details

#**(p) ⇒ Object



108
# File 'lib/shorthand.rb', line 108

def **  (p) self+p.to_p      end

#===(p) ⇒ Object



115
# File 'lib/shorthand.rb', line 115

def ===(p)  real == p.real   end

#[](p, dots = true) ⇒ Object



121
# File 'lib/shorthand.rb', line 121

def [](p,dots=true)   Path.glob((dir + p.to_s).to_s, dots ? File::FNM_DOTMATCH : 0)  end

#abs(wd = nil) ⇒ Object

Raises:

  • (ArgumentError)


162
163
164
165
166
167
168
169
170
171
# File 'lib/shorthand.rb', line 162

def abs(wd=nil)
  wd ||= ($pwd || Path.pwd); wd = wd.to_s
  s    = self.to_s
  raise ArgumentError.new('Bad working directory- must be absolute') if wd[0].chr != '/'
  if    s.blank? ;                                   return nil
  elsif s[0].chr=='/' ;                              return s
  elsif s[0].chr=='~' && (s[1].nil?||s[1].chr=='/'); _abs_i(s[1..-1], ENV['HOME'])
  elsif s =~ /~([^\/]+)/;                            _abs_i($', Etc.getpwnam($1).dir)
  else                                               _abs_i(s, wd) end
end

#as_other(new_dir, new_ext = nil) ⇒ Object



124
125
126
127
128
# File 'lib/shorthand.rb', line 124

def as_other(new_dir, new_ext=nil)
  p = new_dir.nil? ? self : (new_dir.to_p + self.basename)
  p = Path[p.to_s.sub(/#{self.extname}$/,'.'+new_ext)] if new_ext
  return p
end

#contentsObject



136
# File 'lib/shorthand.rb', line 136

def contents; IO.read(self) end

#different_contents?(str) ⇒ Boolean

Returns:

  • (Boolean)


137
# File 'lib/shorthand.rb', line 137

def different_contents?(str) IO.read(self).strip != str.strip end

#dirObject



119
# File 'lib/shorthand.rb', line 119

def dir()   (exp.dir? || to_s[-1].chr == '/') ? exp : exp.dirname end

#dir!Object



120
# File 'lib/shorthand.rb', line 120

def dir!()  (exp.mkpath unless exp.dir? rescue return nil); self end

#dir?Boolean

Returns:

  • (Boolean)


114
# File 'lib/shorthand.rb', line 114

def dir?()  directory?       end

#dist_from(p) ⇒ Object



154
155
156
157
158
159
# File 'lib/shorthand.rb', line 154

def dist_from(p)
  return 0 if self === p
  travp = p.dir.rel(self.dir,false).to_s
  return 1 if travp =~ /^\/?\.\/?$/
  return travp.split('/').size + 1
end

#expObject



117
# File 'lib/shorthand.rb', line 117

def exp ()  return @exp ||= self.expand_path    end

#missing?Boolean

Returns:

  • (Boolean)


123
# File 'lib/shorthand.rb', line 123

def missing?() !self.exist? end

#old_initObject



104
# File 'lib/shorthand.rb', line 104

alias old_init initialize

#old_mmObject



160
# File 'lib/shorthand.rb', line 160

alias old_mm method_missing

#older_than?(p) ⇒ Boolean

Returns:

  • (Boolean)


122
# File 'lib/shorthand.rb', line 122

def older_than?(p) self.stat.mtime < p.stat.mtime end

#perm?Boolean

Returns:

  • (Boolean)


116
# File 'lib/shorthand.rb', line 116

def perm?() exp.dir? ? rwx? : rw?               end

#r?Boolean

Returns:

  • (Boolean)


109
# File 'lib/shorthand.rb', line 109

def r?  ()  readable_real?   end

#realObject



118
# File 'lib/shorthand.rb', line 118

def real()  begin exp.realpath rescue exp end   end

#rel(p = nil, home = true) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/shorthand.rb', line 129

def rel(p=nil,home=true)
  p ||= ($pwd || Path.pwd)
  return @rc2[p] if @rc2[p]
  r = abs.rel_path_from(p.abs)
  r = r.sub(ENV['HOME'],'~') if home
  r
end

#rel_path_from(p) ⇒ Object



148
# File 'lib/shorthand.rb', line 148

def rel_path_from(p) @rc ||= {}; @rc[p.to_s] ||= relative_path_from(p) end

#relation_to(p) ⇒ Object



149
150
151
152
153
# File 'lib/shorthand.rb', line 149

def relation_to(p)
  travp = p.rel(self,false).to_s
  if    travp =~ /^(..\/)+..(\/|$)/ then :child
  else  travp =~ /^..\// ? :stranger : :parent end
end

#rw?Boolean

Returns:

  • (Boolean)


112
# File 'lib/shorthand.rb', line 112

def rw? ()  r? && w?         end

#rwx?Boolean

Returns:

  • (Boolean)


113
# File 'lib/shorthand.rb', line 113

def rwx?()  r? && w? && x?   end

#short(p = nil, home = true) ⇒ Object



138
139
140
141
142
143
144
145
146
147
# File 'lib/shorthand.rb', line 138

def short(p=nil,home=true)
  p ||= ($pwd || Path.pwd)
  return @rc2[p.to_s] if @rc2[p.to_s]
  sr  = real; pr  = p.real
  se  = exp;  pe  = p.exp
  candidates  = [sr.rel_path_from(pr), sr.rel_path_from(pe),
    se.rel_path_from(pr), se.rel_path_from(pe)]
  candidates += [sr.sub(ENV['HOME'],'~'), se.sub(ENV['HOME'],'~')] if home
  @rc2[p.to_s] = candidates.sort_by{|v|v.to_s.size}[0]
end

#to_pObject



107
# File 'lib/shorthand.rb', line 107

def to_p()  self             end

#w?Boolean

Returns:

  • (Boolean)


110
# File 'lib/shorthand.rb', line 110

def w?  ()  writable_real?   end

#x?Boolean

Returns:

  • (Boolean)


111
# File 'lib/shorthand.rb', line 111

def x?  ()  executable_real? end