Class: Folio::FileObject

Inherits:
Object
  • Object
show all
Defined in:
lib/folio/fileobject.rb

Overview

File Object

Base class for all folio objects.

Direct Known Subclasses

Device, Directory, Document, Link, Pipe, Socket

Constant Summary collapse

Separator =
::File::Separator

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



57
58
59
# File 'lib/folio/fileobject.rb', line 57

def path
  @path
end

Class Method Details

.[](*path) ⇒ Object

Factory method.

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/folio/fileobject.rb', line 20

def self.[](*path)
  path = ::File.join(*path)

  raise FileNotFound.new(path) unless ::File.exist?(path)

  case ::File.ftype(path)
  when 'file'
    Document.new(path)
  when 'directory'
    Directory.new(path)
  when 'link'
    Link.new(path)
  when 'characterSpecial'
    CharacterDevice.new(path)
  when 'blockSpecial'
    BlockDevice.new(path)
  when 'socket'
    raise TypeError # Socket.new(path) ?
  when 'fifo'
    raise TypeError # Pipe?
  else # 'unknown'
    raise FileNotFound.new(path)
  end
end

Instance Method Details

#<=>(other) ⇒ Object



245
246
247
# File 'lib/folio/fileobject.rb', line 245

def <=>(other)
  path <=> other.to_s
end

#==(other) ⇒ Object



59
60
61
62
63
# File 'lib/folio/fileobject.rb', line 59

def ==(other)
#p @path, other.path
  return false unless FileObject===other
  @path == other.path
end

#atimeObject



179
# File 'lib/folio/fileobject.rb', line 179

def atime               ; stat.atime             ; end

#basenameObject

– Pathname Methods ++



200
# File 'lib/folio/fileobject.rb', line 200

def basename            ; ::File.basename(path)              ; end

#blockdev?Boolean

Returns:

  • (Boolean)


174
# File 'lib/folio/fileobject.rb', line 174

def blockdev?           ; stat.blockdev?         ; end

#chardev?Boolean

Returns:

  • (Boolean)


175
# File 'lib/folio/fileobject.rb', line 175

def chardev?            ; stat.chardev?          ; end

#chmod(mode) ⇒ Object



123
124
125
# File 'lib/folio/fileobject.rb', line 123

def chmod(mode)
  ::File.chmod(mode, path)
end

#chown(user, group) ⇒ Object



127
128
129
# File 'lib/folio/fileobject.rb', line 127

def chown(user, group)
  ::File.chown(user, group, path)
end

#cp(dest) ⇒ Object

Copy file to destination path.



142
143
144
# File 'lib/folio/fileobject.rb', line 142

def cp(dest)
  util.cp(path, dest)
end

#ctimeObject



180
# File 'lib/folio/fileobject.rb', line 180

def ctime               ; stat.ctime             ; end

#directory?Boolean

Returns:

  • (Boolean)


173
# File 'lib/folio/fileobject.rb', line 173

def directory?          ; stat.directory?        ; end

#dirnameObject



201
# File 'lib/folio/fileobject.rb', line 201

def dirname             ; ::File.dirname(path)               ; end

#document?Boolean

def file? ; stat.file? ; end

Returns:

  • (Boolean)


172
# File 'lib/folio/fileobject.rb', line 172

def document?           ; stat.file?             ; end

#exist?Boolean Also known as: exists?

This will alwasy be true, EXCEPT when #rm, #delete or #unlink have been used.

Returns:

  • (Boolean)


67
68
69
# File 'lib/folio/fileobject.rb', line 67

def exist?
  ::FileTest.exist?(path)
end

#extnameObject



202
# File 'lib/folio/fileobject.rb', line 202

def extname             ; ::File.extname(path)               ; end

#fnmatch(pattern, flags = 0) ⇒ Object Also known as: fnmatch?



229
230
231
# File 'lib/folio/fileobject.rb', line 229

def fnmatch(pattern, flags=0)
  ::File.fnmatch(path, pattern, flags)
end

#grpowned?Boolean

Returns:

  • (Boolean)


181
# File 'lib/folio/fileobject.rb', line 181

def grpowned?           ; stat.grpowned?         ; end

#identical?Boolean

Returns:

  • (Boolean)


182
# File 'lib/folio/fileobject.rb', line 182

def identical?          ; stat.identical?        ; end

#inspectObject

Inspect returns the path string relative to the current working directory.



240
# File 'lib/folio/fileobject.rb', line 240

def inspect; "#{relative}"; end

#install(dest, mode = nil) ⇒ Object

Install file to destination path.



147
148
149
# File 'lib/folio/fileobject.rb', line 147

def install(dest, mode=nil)
  util.install(path, dest, mode)
end

– File Manipulation ++



81
82
83
# File 'lib/folio/fileobject.rb', line 81

def link(new)
  ::File.ln(path, new)
end


86
87
88
89
# File 'lib/folio/fileobject.rb', line 86

def link_force(new)
  ::File.remove(new)
  link(new)
end

#mtimeObject



183
# File 'lib/folio/fileobject.rb', line 183

def mtime               ; stat.mtime             ; end

#owned?Boolean

Returns:

  • (Boolean)


184
# File 'lib/folio/fileobject.rb', line 184

def owned?              ; stat.owned?            ; end

#parentObject

Returns the parent directory object.



73
74
75
# File 'lib/folio/fileobject.rb', line 73

def parent
  self.class[File.dirname(path)]
end

#pipe?Boolean

Returns:

  • (Boolean)


177
# File 'lib/folio/fileobject.rb', line 177

def pipe?               ; stat.pipe?             ; end

#readable?Boolean

Returns:

  • (Boolean)


185
# File 'lib/folio/fileobject.rb', line 185

def readable?           ; stat.readable?         ; end

#readable_real?Boolean

Returns:

  • (Boolean)


186
# File 'lib/folio/fileobject.rb', line 186

def readable_real?      ; stat.readable_real     ; end

#relativeObject

Gives path relative to current working directory. If current is below path one step then it uses ‘..’, further below and it returns the full path.



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/folio/fileobject.rb', line 210

def relative
  pwd = Dir.pwd
  pth = path
  if pth.index(pwd) == 0
    r = pth[pwd.size+1..-1]
    r = '.' unless r
    return r
  else
    pwd = File.dirname(pwd)
    if pth.index(pwd) == 0
      r = pth[pwd.size+1..-1]
      return '..' unless r
      return File.join('..', r)
    else
      pth
    end
  end
end

#rename(dest) ⇒ Object Also known as: mv



103
104
105
106
# File 'lib/folio/fileobject.rb', line 103

def rename(dest)
  ::File.rename(path, dest)
  @path = ::File.expand_path(dest)
end

#restatObject Also known as: stat!

Refresh status cache.



166
167
168
# File 'lib/folio/fileobject.rb', line 166

def restat
  @stat = File.stat(path)
end

#setgid?Boolean

Returns:

  • (Boolean)


187
# File 'lib/folio/fileobject.rb', line 187

def setgid?             ; stat.setgid?           ; end

#setuid?Boolean

Returns:

  • (Boolean)


188
# File 'lib/folio/fileobject.rb', line 188

def setuid?             ; stat.setuid?           ; end

#sizeObject



189
# File 'lib/folio/fileobject.rb', line 189

def size                ; stat.size              ; end

#size?Boolean

Returns:

  • (Boolean)


190
# File 'lib/folio/fileobject.rb', line 190

def size?               ; stat.size?             ; end

#socket?Boolean

Returns:

  • (Boolean)


176
# File 'lib/folio/fileobject.rb', line 176

def socket?             ; stat.socket?           ; end

#splitObject

TODO: I don’t like the name of this.



205
# File 'lib/folio/fileobject.rb', line 205

def split               ; ::File.split(path)                 ; end

#statObject

Get stat and cache it.



161
162
163
# File 'lib/folio/fileobject.rb', line 161

def stat
  @stat ||= File.stat(path)
end

#sticky?Boolean

Returns:

  • (Boolean)


191
# File 'lib/folio/fileobject.rb', line 191

def sticky?             ; stat.sticky?           ; end


92
93
94
# File 'lib/folio/fileobject.rb', line 92

def symlink(new)
  ::File.symlink(path, new)
end


97
98
99
100
# File 'lib/folio/fileobject.rb', line 97

def symlink_force(new)
  ::File.remove(new)
  symlink(new)
end

#to_sObject

Returns the path string.



243
# File 'lib/folio/fileobject.rb', line 243

def to_s ; path ; end

#touchObject



152
153
154
# File 'lib/folio/fileobject.rb', line 152

def touch
  util.touch(path)
end

how to handle –b/c it disappears?



110
111
112
# File 'lib/folio/fileobject.rb', line 110

def unlink
  ::File.delete(path)
end


116
117
118
119
# File 'lib/folio/fileobject.rb', line 116

def unlink_force
  ::File.remove(new)
  unlink(path)
end

#utime(atime, mtime) ⇒ Object



131
132
133
# File 'lib/folio/fileobject.rb', line 131

def utime(atime, mtime)
  ::File.utime(atime, mtime, path)
end

#writable?Boolean

Returns:

  • (Boolean)


192
# File 'lib/folio/fileobject.rb', line 192

def writable?           ; stat.writable?         ; end

#writable_real?Boolean

Returns:

  • (Boolean)


193
# File 'lib/folio/fileobject.rb', line 193

def writable_real?      ; stat.writable_real?    ; end

#zero?Boolean

Returns:

  • (Boolean)


194
# File 'lib/folio/fileobject.rb', line 194

def zero?               ; stat.zero?             ; end