Module: Spoom::Context::FileSystem

Extended by:
T::Helpers, T::Sig
Included in:
Spoom::Context
Defined in:
lib/spoom/context/file_system.rb

Overview

File System features for a context

Instance Method Summary collapse

Instance Method Details

#absolute_path_to(relative_path) ⇒ Object



15
16
17
# File 'lib/spoom/context/file_system.rb', line 15

def absolute_path_to(relative_path)
  File.join(absolute_path, relative_path)
end

#destroy!Object



88
89
90
# File 'lib/spoom/context/file_system.rb', line 88

def destroy!
  FileUtils.rm_rf(absolute_path)
end

#exist?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/spoom/context/file_system.rb', line 21

def exist?
  File.directory?(absolute_path)
end

#file?(relative_path) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/spoom/context/file_system.rb', line 48

def file?(relative_path)
  File.file?(absolute_path_to(relative_path))
end

#glob(pattern = "**/*") ⇒ Object



34
35
36
37
38
# File 'lib/spoom/context/file_system.rb', line 34

def glob(pattern = "**/*")
  Dir.glob(absolute_path_to(pattern)).map do |path|
    Pathname.new(path).relative_path_from(absolute_path).to_s
  end.sort
end

#listObject



42
43
44
# File 'lib/spoom/context/file_system.rb', line 42

def list
  glob("*")
end

#mkdir!Object



27
28
29
30
# File 'lib/spoom/context/file_system.rb', line 27

def mkdir!
  FileUtils.rm_rf(absolute_path)
  FileUtils.mkdir_p(absolute_path)
end

#move!(from_relative_path, to_relative_path) ⇒ Object



78
79
80
81
82
# File 'lib/spoom/context/file_system.rb', line 78

def move!(from_relative_path, to_relative_path)
  destination_path = absolute_path_to(to_relative_path)
  FileUtils.mkdir_p(File.dirname(destination_path))
  FileUtils.mv(absolute_path_to(from_relative_path), destination_path)
end

#read(relative_path) ⇒ Object



56
57
58
# File 'lib/spoom/context/file_system.rb', line 56

def read(relative_path)
  File.read(absolute_path_to(relative_path))
end

#remove!(relative_path) ⇒ Object



72
73
74
# File 'lib/spoom/context/file_system.rb', line 72

def remove!(relative_path)
  FileUtils.rm_rf(absolute_path_to(relative_path))
end

#write!(relative_path, contents = "", append: false) ⇒ Object



64
65
66
67
68
# File 'lib/spoom/context/file_system.rb', line 64

def write!(relative_path, contents = "", append: false)
  absolute_path = absolute_path_to(relative_path)
  FileUtils.mkdir_p(File.dirname(absolute_path))
  File.write(absolute_path, contents, mode: append ? "a" : "w")
end