Sha256: c330ef48683f9f9e057aecd4ad53cd7af401b518c18de55edd737ae01259441d

Contents?: true

Size: 1.57 KB

Versions: 33

Compression:

Stored size: 1.57 KB

Contents

module FakeFS
  # Fake Dir class
  class FakeDir
    attr_accessor :name, :parent, :mode, :uid, :gid, :mtime, :atime, :inode
    attr_reader :ctime, :content

    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

    def entry
      self
    end

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

    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

    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

    def empty?
      @entries.empty?
    end

    def entries
      @entries.values
    end

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

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

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

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

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
fakefs-2.5.0 lib/fakefs/fake/dir.rb
fakefs-2.4.0 lib/fakefs/fake/dir.rb
fakefs-2.3.0 lib/fakefs/fake/dir.rb
fakefs-2.2.0 lib/fakefs/fake/dir.rb
fakefs-2.1.0 lib/fakefs/fake/dir.rb
fakefs-2.0.0 lib/fakefs/fake/dir.rb
fakefs-1.9.0 lib/fakefs/fake/dir.rb
fakefs-1.8.0 lib/fakefs/fake/dir.rb
fakefs-1.7.0 lib/fakefs/fake/dir.rb
fakefs-1.6.0 lib/fakefs/fake/dir.rb
fakefs-1.5.1 lib/fakefs/fake/dir.rb
fakefs-1.5.0 lib/fakefs/fake/dir.rb
fakefs-1.4.1 lib/fakefs/fake/dir.rb
fakefs-1.4.0 lib/fakefs/fake/dir.rb
fakefs-1.3.2 lib/fakefs/fake/dir.rb
fakefs-1.3.1 lib/fakefs/fake/dir.rb
fakefs-1.3.0 lib/fakefs/fake/dir.rb
fakefs-1.2.3 lib/fakefs/fake/dir.rb
fakefs-1.2.2 lib/fakefs/fake/dir.rb
fakefs-1.2.1 lib/fakefs/fake/dir.rb