Module: Spoom::Context::FileSystem

Included in:
Spoom::Context
Defined in:
lib/spoom/context/file_system.rb

Overview

File System features for a context @requires_ancestor: Context

Instance Method Summary collapse

Instance Method Details

#absolute_path_to(relative_path) ⇒ Object

Returns the absolute path to ‘relative_path` in the context’s directory : (String relative_path) -> String



11
12
13
# File 'lib/spoom/context/file_system.rb', line 11

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

#collect_files(allow_extensions: [], allow_mime_types: [], exclude_patterns: []) ⇒ Object

: (?allow_extensions: Array, ?allow_mime_types: Array, ?exclude_patterns: Array) -> Array



43
44
45
46
47
48
49
50
51
# File 'lib/spoom/context/file_system.rb', line 43

def collect_files(allow_extensions: [], allow_mime_types: [], exclude_patterns: [])
  collector = FileCollector.new(
    allow_extensions: allow_extensions,
    allow_mime_types: allow_mime_types,
    exclude_patterns: exclude_patterns,
  )
  collector.visit_path(absolute_path)
  collector.files.map { |file| file.delete_prefix("#{absolute_path}/") }
end

#destroy!Object

Delete this context and its content

Warning: it will ‘rm -rf` the context directory on the file system. : -> void



95
96
97
# File 'lib/spoom/context/file_system.rb', line 95

def destroy!
  FileUtils.rm_rf(absolute_path)
end

#exist?Boolean

Does the context directory at ‘absolute_path` exist and is a directory? : -> bool

Returns:

  • (Boolean)


17
18
19
# File 'lib/spoom/context/file_system.rb', line 17

def exist?
  File.directory?(absolute_path)
end

#file?(relative_path) ⇒ Boolean

Does ‘relative_path` point to an existing file in this context directory? : (String relative_path) -> bool

Returns:

  • (Boolean)


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

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

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

List all files in this context matching ‘pattern` : (?String pattern) -> Array



30
31
32
33
34
# File 'lib/spoom/context/file_system.rb', line 30

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

List all files at the top level of this context directory : -> Array



38
39
40
# File 'lib/spoom/context/file_system.rb', line 38

def list
  glob("*")
end

#mkdir!Object

Create the context directory at ‘absolute_path` : -> void



23
24
25
26
# File 'lib/spoom/context/file_system.rb', line 23

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

#move!(from_relative_path, to_relative_path) ⇒ Object

Move the file or directory from ‘from_relative_path` to `to_relative_path` : (String from_relative_path, String to_relative_path) -> void



85
86
87
88
89
# File 'lib/spoom/context/file_system.rb', line 85

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

Return the contents of the file at ‘relative_path` in this context directory

Will raise if the file doesn’t exist. : (String relative_path) -> String



63
64
65
# File 'lib/spoom/context/file_system.rb', line 63

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

#remove!(relative_path) ⇒ Object

Remove the path at ‘relative_path` (recursive + force) in this context directory : (String relative_path) -> void



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

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

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

Write ‘contents` in the file at `relative_path` in this context directory

Append to the file if ‘append` is true. : (String relative_path, ?String contents, ?append: bool) -> void



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

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