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.



55
56
57
# File 'lib/folio/fileobject.rb', line 55

def path
  @path
end

#relpathObject (readonly)

Returns the value of attribute relpath.



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

def relpath
  @relpath
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 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 TypeError
  end
end

Instance Method Details

#<=>(other) ⇒ Object



233
234
235
# File 'lib/folio/fileobject.rb', line 233

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

#==(other) ⇒ Object



237
238
239
# File 'lib/folio/fileobject.rb', line 237

def ==(other)
  path == other.path
end

#atimeObject



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

def atime               ; stat.atime             ; end

#basenameObject

– Pathname Methods ++



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

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

#blockdev?Boolean

Returns:

  • (Boolean)


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

def blockdev?           ; stat.blockdev?         ; end

#chardev?Boolean

Returns:

  • (Boolean)


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

def chardev?            ; stat.chardev?          ; end

#chmod(mode) ⇒ Object



112
113
114
# File 'lib/folio/fileobject.rb', line 112

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

#chown(user, group) ⇒ Object



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

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

#cp(dest) ⇒ Object

Copy file to destination path.



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

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

#ctimeObject



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

def ctime               ; stat.ctime             ; end

#directory?Boolean

Returns:

  • (Boolean)


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

def directory?          ; stat.directory?        ; end

#dirnameObject



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

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

#document?Boolean

def file? ; stat.file? ; end

Returns:

  • (Boolean)


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

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)


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

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

#extnameObject



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

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

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



217
218
219
# File 'lib/folio/fileobject.rb', line 217

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

#grpowned?Boolean

Returns:

  • (Boolean)


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

def grpowned?           ; stat.grpowned?         ; end

#identical?Boolean

Returns:

  • (Boolean)


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

def identical?          ; stat.identical?        ; end

#inspectObject

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



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

def inspect; "#{relative}"; end

#install(dest, mode = nil) ⇒ Object

Install file to destination path.



136
137
138
# File 'lib/folio/fileobject.rb', line 136

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

– File Manipulation ++



69
70
71
# File 'lib/folio/fileobject.rb', line 69

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


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

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

#mtimeObject



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

def mtime               ; stat.mtime             ; end

#owned?Boolean

Returns:

  • (Boolean)


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

def owned?              ; stat.owned?            ; end

#pipe?Boolean

Returns:

  • (Boolean)


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

def pipe?               ; stat.pipe?             ; end

#readable?Boolean

Returns:

  • (Boolean)


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

def readable?           ; stat.readable?         ; end

#readable_real?Boolean

Returns:

  • (Boolean)


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

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.



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/folio/fileobject.rb', line 198

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



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

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

#restatObject Also known as: stat!

Refresh status cache.



154
155
156
# File 'lib/folio/fileobject.rb', line 154

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

#setgid?Boolean

Returns:

  • (Boolean)


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

def setgid?             ; stat.setgid?           ; end

#setuid?Boolean

Returns:

  • (Boolean)


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

def setuid?             ; stat.setuid?           ; end

#sizeObject



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

def size                ; stat.size              ; end

#size?Boolean

Returns:

  • (Boolean)


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

def size?               ; stat.size?             ; end

#socket?Boolean

Returns:

  • (Boolean)


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

def socket?             ; stat.socket?           ; end

#splitObject

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



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

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

#statObject

Get stat and cache it.



149
150
151
# File 'lib/folio/fileobject.rb', line 149

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

#sticky?Boolean

Returns:

  • (Boolean)


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

def sticky?             ; stat.sticky?           ; end


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

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


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

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

#to_sObject

Returns the path string.



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

def to_s ; path ; end

#touchObject



140
141
142
# File 'lib/folio/fileobject.rb', line 140

def touch
  util.touch(path)
end

how to handle –b/c it disappears?



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

def unlink
  ::File.delete(path)
end


105
106
107
108
# File 'lib/folio/fileobject.rb', line 105

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

#utime(atime, mtime) ⇒ Object



120
121
122
# File 'lib/folio/fileobject.rb', line 120

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

#writable?Boolean

Returns:

  • (Boolean)


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

def writable?           ; stat.writable?         ; end

#writable_real?Boolean

Returns:

  • (Boolean)


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

def writable_real?      ; stat.writable_real?    ; end

#zero?Boolean

Returns:

  • (Boolean)


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

def zero?               ; stat.zero?             ; end