Class: PlatformosCheck::FileSystemStorage

Inherits:
Storage
  • Object
show all
Defined in:
lib/platformos_check/file_system_storage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Storage

#platformos_app, #versioned?

Constructor Details

#initialize(root, ignored_patterns: []) ⇒ FileSystemStorage



10
11
12
13
14
# File 'lib/platformos_check/file_system_storage.rb', line 10

def initialize(root, ignored_patterns: [])
  @root = Pathname.new(root)
  @ignored_patterns = ignored_patterns
  @files = {}
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



8
9
10
# File 'lib/platformos_check/file_system_storage.rb', line 8

def root
  @root
end

Instance Method Details

#filesObject



64
65
66
67
68
# File 'lib/platformos_check/file_system_storage.rb', line 64

def files
  @file_array ||= glob("**/*")
                  .reject { |path| File.directory?(path) }
                  .map { |path| path.relative_path_from(@root).to_s }
end

#mkdir(relative_path) ⇒ Object



45
46
47
48
49
50
# File 'lib/platformos_check/file_system_storage.rb', line 45

def mkdir(relative_path)
  return if file_exists?(relative_path)

  reset_memoizers
  file(relative_path).mkpath
end

#path(relative_path) ⇒ Object



20
21
22
# File 'lib/platformos_check/file_system_storage.rb', line 20

def path(relative_path)
  @root.join(relative_path)
end

#read(relative_path) ⇒ Object



24
25
26
27
28
# File 'lib/platformos_check/file_system_storage.rb', line 24

def read(relative_path)
  file(relative_path).read(mode: 'rb', encoding: 'UTF-8')
rescue Errno::ENOENT
  nil
end

#relative_path(absolute_path) ⇒ Object



16
17
18
# File 'lib/platformos_check/file_system_storage.rb', line 16

def relative_path(absolute_path)
  Pathname.new(absolute_path).relative_path_from(@root).to_s
end

#remove(relative_path) ⇒ Object



38
39
40
41
42
43
# File 'lib/platformos_check/file_system_storage.rb', line 38

def remove(relative_path)
  file(relative_path).delete

  @platformos_app&.update([relative_path], remove: true)
  reset_memoizers
end

#rename(old_path, new_path) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/platformos_check/file_system_storage.rb', line 52

def rename(old_path, new_path)
  return unless file_exists?(old_path)

  @platformos_app&.update([old_path], remove: true)
  @platformos_app&.update([new_path])

  reset_memoizers

  FileUtils.mkdir_p(File.dirname(file(new_path)))
  FileUtils.mv(file(old_path), file(new_path))
end

#write(relative_path, content) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/platformos_check/file_system_storage.rb', line 30

def write(relative_path, content)
  reset_memoizers unless file_exists?(relative_path)

  file(relative_path).dirname.mkpath unless file(relative_path).dirname.directory?
  file(relative_path).write(content, mode: 'w+b', encoding: 'UTF-8')
  @platformos_app&.update([relative_path], remove: !file_exists?(relative_path))
end