Module: Spoom::Context::FileSystem
Overview
File System features for a context
Instance Method Summary collapse
-
#absolute_path_to(relative_path) ⇒ Object
Returns the absolute path to ‘relative_path` in the context’s directory : (String relative_path) -> String.
- #collect_files(allow_extensions: [], allow_mime_types: [], exclude_patterns: []) ⇒ Object
-
#destroy! ⇒ Object
Delete this context and its content.
-
#exist? ⇒ Boolean
Does the context directory at ‘absolute_path` exist and is a directory? : -> bool.
-
#file?(relative_path) ⇒ Boolean
Does ‘relative_path` point to an existing file in this context directory? : (String relative_path) -> bool.
-
#glob(pattern = "**/*") ⇒ Object
List all files in this context matching ‘pattern` : (?String pattern) -> Array.
-
#list ⇒ Object
List all files at the top level of this context directory : -> Array.
-
#mkdir! ⇒ Object
Create the context directory at ‘absolute_path` : -> void.
-
#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.
-
#read(relative_path) ⇒ Object
Return the contents of the file at ‘relative_path` in this context directory.
-
#remove!(relative_path) ⇒ Object
Remove the path at ‘relative_path` (recursive + force) in this context directory : (String relative_path) -> void.
-
#write!(relative_path, contents = "", append: false) ⇒ Object
Write ‘contents` in the file at `relative_path` in this context directory.
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
14 15 16 |
# File 'lib/spoom/context/file_system.rb', line 14 def absolute_path_to(relative_path) File.join(absolute_path, relative_path) end |
#collect_files(allow_extensions: [], allow_mime_types: [], exclude_patterns: []) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/spoom/context/file_system.rb', line 46 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
98 99 100 |
# File 'lib/spoom/context/file_system.rb', line 98 def destroy! FileUtils.rm_rf(absolute_path) end |
#exist? ⇒ Boolean
Does the context directory at ‘absolute_path` exist and is a directory? : -> bool
20 21 22 |
# File 'lib/spoom/context/file_system.rb', line 20 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
58 59 60 |
# File 'lib/spoom/context/file_system.rb', line 58 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
33 34 35 36 37 |
# File 'lib/spoom/context/file_system.rb', line 33 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 |
#list ⇒ Object
List all files at the top level of this context directory : -> Array
41 42 43 |
# File 'lib/spoom/context/file_system.rb', line 41 def list glob("*") end |
#mkdir! ⇒ Object
Create the context directory at ‘absolute_path` : -> void
26 27 28 29 |
# File 'lib/spoom/context/file_system.rb', line 26 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
88 89 90 91 92 |
# File 'lib/spoom/context/file_system.rb', line 88 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
66 67 68 |
# File 'lib/spoom/context/file_system.rb', line 66 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
82 83 84 |
# File 'lib/spoom/context/file_system.rb', line 82 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
74 75 76 77 78 |
# File 'lib/spoom/context/file_system.rb', line 74 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 |