Class: Ahnnotate::VfsDriver::Filesystem

Inherits:
Object
  • Object
show all
Defined in:
lib/ahnnotate/vfs_driver/filesystem.rb

Direct Known Subclasses

ReadOnlyFilesystem

Instance Method Summary collapse

Constructor Details

#initialize(root:) ⇒ Filesystem



6
7
8
# File 'lib/ahnnotate/vfs_driver/filesystem.rb', line 6

def initialize(root:)
  @root = root
end

Instance Method Details

#[](path) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/ahnnotate/vfs_driver/filesystem.rb', line 36

def [](path)
  path = root.join(path)

  if path.exist?
    File.read(path)
  end
end

#[]=(path, content) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ahnnotate/vfs_driver/filesystem.rb', line 44

def []=(path, content)
  path = root.join(path)
  holding_directory = path.dirname

  if holding_directory.file?
    raise "File is not a directory"
  end

  if !holding_directory.exist?
    holding_directory.mkpath
  end

  File.write(path, content)
end

#dir?(path) ⇒ Boolean



59
60
61
# File 'lib/ahnnotate/vfs_driver/filesystem.rb', line 59

def dir?(path)
  root.join(path).directory?
end

#eachObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ahnnotate/vfs_driver/filesystem.rb', line 10

def each
  paths =
    root
      .glob("**/*")
      .select(&:file?)
      .map { |path| path.relative_path_from(root).to_s }

  root.glob("**/*").select(&:file?).each do |abspath|
    relpath = abspath.relative_path_from(root).to_s

    yield relpath, File.read(abspath)
  end
end

#each_in(subset_paths) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ahnnotate/vfs_driver/filesystem.rb', line 24

def each_in(subset_paths)
  subset_paths = subset_paths.map { |path| root.join(path) }

  subset_paths.each do |subset_path|
    subset_path.glob("**/*").select(&:file?).map do |abspath|
      relpath = abspath.relative_path_from(root).to_s

      yield relpath, File.read(abspath)
    end
  end
end