Class: Kozeki::LocalFilesystem

Inherits:
Object
  • Object
show all
Includes:
Filesystem
Defined in:
lib/kozeki/local_filesystem.rb

Constant Summary

Constants included from Filesystem

Filesystem::Entry, Filesystem::Event

Instance Method Summary collapse

Methods included from Filesystem

#flush, #list, #retain_only

Constructor Details

#initialize(base_directory) ⇒ LocalFilesystem

Returns a new instance of LocalFilesystem.



9
10
11
# File 'lib/kozeki/local_filesystem.rb', line 9

def initialize(base_directory)
  @base = base_directory
end

Instance Method Details

#delete(path) ⇒ Object

Parameters:

  • path (Array)


37
38
39
40
# File 'lib/kozeki/local_filesystem.rb', line 37

def delete(path)
  File.unlink(File.join(@base, *path))
rescue Errno::ENOENT
end

#list_entriesObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kozeki/local_filesystem.rb', line 42

def list_entries
  range = File.join(@base, 'x')[0..-2].size .. -1
  Dir[File.join(@base, '**', '*')].filter_map do |fspath|
    path =  fspath[range].split(File::SEPARATOR)
    next if File.directory?(fspath) rescue nil
    Filesystem::Entry.new(
      path:,
      mtime: File.mtime(fspath),
    )
  end
end

#read(path) ⇒ Object

Parameters:

  • path (Array)


14
15
16
17
18
# File 'lib/kozeki/local_filesystem.rb', line 14

def read(path)
  File.read(File.join(@base, *path))
rescue Errno::ENOENT
  raise Filesystem::NotFound
end

#read_with_mtime(path) ⇒ Object

Parameters:

  • path (Array)


21
22
23
24
25
26
# File 'lib/kozeki/local_filesystem.rb', line 21

def read_with_mtime(path)
  [
    read(path),
    File.mtime(File.join(@base, *path)),
  ]
end

#watch(&block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/kozeki/local_filesystem.rb', line 54

def watch(&block)
  require 'listen'
  base = File.expand_path(@base)
  l = Listen.to(@base) do |modified, added, removed|
    yield [
      *(modified + added).map do |path|
        Filesystem::Event.new(
          op: :update,
          path: convert_absolute_to_path(base, path),
          time: nil,
        )
      end,
      *removed.map do |path|
        Filesystem::Event.new(
          op: :delete,
          path: convert_absolute_to_path(base, path),
          time: nil,
        )
      end,
    ]
  end
  l.start
  -> { l.stop }
end

#write(path, string) ⇒ Object

Parameters:

  • path (Array)


29
30
31
32
33
34
# File 'lib/kozeki/local_filesystem.rb', line 29

def write(path, string)
  path = File.join(@base, *path)
  dirname = File.dirname(path)
  FileUtils.mkdir_p(dirname)
  File.write(path, string)
end