Class: FakeFS::FakeDir

Inherits:
Object
  • Object
show all
Defined in:
lib/fakefs/fake/dir.rb

Overview

Fake Dir class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, parent = nil) ⇒ FakeDir

Returns a new instance of FakeDir.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fakefs/fake/dir.rb', line 9

def initialize(name = nil, parent = nil)
  @name    = name
  @parent  = parent
  @ctime   = Time.now
  @mtime   = @ctime
  @atime   = @ctime
  @mode    = 0o100000 + (0o777 - File.umask)
  @uid     = Process.uid
  @gid     = Process.gid
  @inode   = FakeInode.new(self)
  @content = ''
  @entries = {}
end

Instance Attribute Details

#atimeObject

Returns the value of attribute atime.



6
7
8
# File 'lib/fakefs/fake/dir.rb', line 6

def atime
  @atime
end

#contentObject (readonly)

Returns the value of attribute content.



7
8
9
# File 'lib/fakefs/fake/dir.rb', line 7

def content
  @content
end

#ctimeObject (readonly)

Returns the value of attribute ctime.



7
8
9
# File 'lib/fakefs/fake/dir.rb', line 7

def ctime
  @ctime
end

#gidObject

Returns the value of attribute gid.



6
7
8
# File 'lib/fakefs/fake/dir.rb', line 6

def gid
  @gid
end

#inodeObject

Returns the value of attribute inode.



6
7
8
# File 'lib/fakefs/fake/dir.rb', line 6

def inode
  @inode
end

#modeObject

Returns the value of attribute mode.



6
7
8
# File 'lib/fakefs/fake/dir.rb', line 6

def mode
  @mode
end

#mtimeObject

Returns the value of attribute mtime.



6
7
8
# File 'lib/fakefs/fake/dir.rb', line 6

def mtime
  @mtime
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/fakefs/fake/dir.rb', line 6

def name
  @name
end

#parentObject

Returns the value of attribute parent.



6
7
8
# File 'lib/fakefs/fake/dir.rb', line 6

def parent
  @parent
end

#uidObject

Returns the value of attribute uid.



6
7
8
# File 'lib/fakefs/fake/dir.rb', line 6

def uid
  @uid
end

Instance Method Details

#[](name) ⇒ Object



64
65
66
# File 'lib/fakefs/fake/dir.rb', line 64

def [](name)
  @entries[name]
end

#[]=(name, value) ⇒ Object



68
69
70
# File 'lib/fakefs/fake/dir.rb', line 68

def []=(name, value)
  @entries[name] = value
end

#clone(parent = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/fakefs/fake/dir.rb', line 32

def clone(parent = nil)
  clone = Marshal.load(Marshal.dump(self))
  clone.entries.each do |value|
    value.parent = clone
  end
  clone.parent = parent if parent
  clone.inode = @inode.clone
  clone
end

#delete(node = self) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/fakefs/fake/dir.rb', line 72

def delete(node = self)
  if node == self
    parent.delete(self)
    @inode.free_inode_num
  else
    @entries.delete(node.name)
  end
end

#empty?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/fakefs/fake/dir.rb', line 52

def empty?
  @entries.empty?
end

#entriesObject



56
57
58
# File 'lib/fakefs/fake/dir.rb', line 56

def entries
  @entries.values
end

#entryObject



23
24
25
# File 'lib/fakefs/fake/dir.rb', line 23

def entry
  self
end

#inspectObject



27
28
29
30
# File 'lib/fakefs/fake/dir.rb', line 27

def inspect
  "(FakeDir name:#{name.inspect} " \
  "parent:#{parent.to_s.inspect} size:#{@entries.size})"
end

#matches(pattern) ⇒ Object



60
61
62
# File 'lib/fakefs/fake/dir.rb', line 60

def matches(pattern)
  @entries.select { |k, _v| pattern =~ k }.values
end

#to_sObject



42
43
44
45
46
47
48
49
50
# File 'lib/fakefs/fake/dir.rb', line 42

def to_s
  if parent && parent.to_s != '.'
    File.join(parent.to_s, name)
  elsif parent && parent.to_s == '.'
    "#{File::PATH_SEPARATOR}#{name}"
  else
    name
  end
end