Class: ColorLS::FileInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/colorls/fileinfo.rb

Constant Summary collapse

@@users =

rubocop:disable Style/ClassVars

{}
@@groups =

rubocop:disable Style/ClassVars

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, link_info = true) ⇒ FileInfo



9
10
11
12
13
14
15
16
# File 'lib/colorls/fileinfo.rb', line 9

def initialize(path, link_info=true)
  @name = File.basename(path)
  @stats = File.lstat(path)

  return unless link_info && @stats.symlink?
  @dead = !File.exist?(path)
  @target = File.readlink(path)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/colorls/fileinfo.rb', line 7

def name
  @name
end

#statsObject (readonly)

Returns the value of attribute stats.



6
7
8
# File 'lib/colorls/fileinfo.rb', line 6

def stats
  @stats
end

Class Method Details

.info(path) ⇒ Object



18
19
20
# File 'lib/colorls/fileinfo.rb', line 18

def self.info(path)
  FileInfo.new(path)
end

Instance Method Details

#dead?Boolean



22
23
24
# File 'lib/colorls/fileinfo.rb', line 22

def dead?
  @dead
end

#directory?Boolean



52
53
54
# File 'lib/colorls/fileinfo.rb', line 52

def directory?
  @stats.directory?
end

#groupObject



37
38
39
40
41
42
# File 'lib/colorls/fileinfo.rb', line 37

def group
  return @@groups[@stats.gid] if @@groups.key? @stats.gid
  @@groups[@stats.gid] = Etc.getgrgid(@stats.gid).name
rescue ArgumentError
  @stats.gid.to_s
end

target of a symlink (only available for symlinks)



61
62
63
# File 'lib/colorls/fileinfo.rb', line 61

def link_target
  @target
end

#mtimeObject



44
45
46
# File 'lib/colorls/fileinfo.rb', line 44

def mtime
  @stats.mtime
end

#owned?Boolean



33
34
35
# File 'lib/colorls/fileinfo.rb', line 33

def owned?
  @stats.owned?
end

#ownerObject



26
27
28
29
30
31
# File 'lib/colorls/fileinfo.rb', line 26

def owner
  return @@users[@stats.uid] if @@users.key? @stats.uid
  @@users[@stats.uid] = Etc.getpwuid(@stats.uid).name
rescue ArgumentError
  @stats.uid.to_s
end

#sizeObject



48
49
50
# File 'lib/colorls/fileinfo.rb', line 48

def size
  @stats.size
end

#symlink?Boolean



56
57
58
# File 'lib/colorls/fileinfo.rb', line 56

def symlink?
  @stats.symlink?
end

#to_sObject



65
66
67
# File 'lib/colorls/fileinfo.rb', line 65

def to_s
  name
end