Module: VV::FileMethods::ClassMethods
- Defined in:
- lib/vv/file_methods.rb
Instance Method Summary collapse
- #cache_home(sub_directory = nil) ⇒ Object (also: #xdg_cache_home)
- #cache_home!(sub_directory) ⇒ Object (also: #xdg_cache_home!)
- #config_home(sub_directory = nil) ⇒ Object (also: #xdg_config_home)
- #config_home!(sub_directory) ⇒ Object (also: #xdg_config_home!)
- #copy_into(filepath, directory, allow_hidden: true, allow_absolute: true) ⇒ Object
-
#data_home(sub_directory = nil) ⇒ Object
(also: #output_home, #xdg_data_home)
Where application specific files should be stored.
- #data_home!(sub_directory) ⇒ Object (also: #xdg_data_home!)
- #file ⇒ Object
- #file_directory ⇒ Object
- #make_directory(directory) ⇒ Object (also: #create_directory, #create_dir, #make_dir)
- #make_directory_if_not_exists(directory) ⇒ Object (also: #make_dir_if_not_exists, #create_directory_if_not_exists, #create_dir_if_not_exists)
-
#move_directory(*args, **kwargs) ⇒ Object
TODO: Think about making a ‘directory` method on string so from / to is super clear.
- #move_directory_into(dir, into, allow_hidden: true, allow_absolute: true) ⇒ Object (also: #move_dir_into, #mv_dir_into)
- #pwd ⇒ Object
- #remove(filepath, force: false) ⇒ Object (also: #remove_file, #rm)
- #remove_directory(directory, quiet_if_gone: false) ⇒ Object (also: #rm_directory, #remove_dir, #rm_dir)
- #rename_directory(from, to, allow_hidden: true, allow_absolute: true) ⇒ Object (also: #rename_dir)
- #repo_directory(missing_throws_exception: true) ⇒ Object
- #separator ⇒ Object
- #vv_included? ⇒ Boolean
- #vv_readlines(*args, **kwargs) ⇒ Object
- #vv_split(string) ⇒ Object
Instance Method Details
#cache_home(sub_directory = nil) ⇒ Object Also known as: xdg_cache_home
178 179 180 181 182 183 184 185 |
# File 'lib/vv/file_methods.rb', line 178 def cache_home sub_directory=nil path = ENV['XDG_CACHE_HOME'] path ||= File.join ENV['HOME'], ".cache" return path unless sub_directory File.join path, sub_directory end |
#cache_home!(sub_directory) ⇒ Object Also known as: xdg_cache_home!
188 189 190 191 192 |
# File 'lib/vv/file_methods.rb', line 188 def cache_home! sub_directory path = cache_home sub_directory File.make_dir_if_not_exists path path end |
#config_home(sub_directory = nil) ⇒ Object Also known as: xdg_config_home
156 157 158 159 160 161 162 163 |
# File 'lib/vv/file_methods.rb', line 156 def config_home sub_directory=nil path = ENV['XDG_CACHE_HOME'] path ||= File.join ENV['HOME'], ".config" return path unless sub_directory File.join path, sub_directory end |
#config_home!(sub_directory) ⇒ Object Also known as: xdg_config_home!
195 196 197 198 199 |
# File 'lib/vv/file_methods.rb', line 195 def config_home! sub_directory path = config_home sub_directory File.make_dir_if_not_exists path path end |
#copy_into(filepath, directory, allow_hidden: true, allow_absolute: true) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/vv/file_methods.rb', line 78 def copy_into filepath, directory, allow_hidden: true, allow_absolute: true = "Filepath `#{filepath}` is unsafe." fail unless filepath.safe_path? = "Filepath `#{filepath}` is a directory." fail if filepath.is_directory_path? = "No such `#{directory}` directory." fail unless directory.is_directory_path? FileUtils.cp filepath, directory end |
#data_home(sub_directory = nil) ⇒ Object Also known as: output_home, xdg_data_home
Where application specific files should be stored
167 168 169 170 171 172 173 174 |
# File 'lib/vv/file_methods.rb', line 167 def data_home sub_directory=nil path = ENV['XDG_DATA_HOME'] path ||= File.join ENV['HOME'], ".local", "share" return path unless sub_directory File.join path, sub_directory end |
#data_home!(sub_directory) ⇒ Object Also known as: xdg_data_home!
202 203 204 205 206 |
# File 'lib/vv/file_methods.rb', line 202 def data_home! sub_directory path = data_home sub_directory File.make_dir_if_not_exists path path end |
#file ⇒ Object
66 67 68 |
# File 'lib/vv/file_methods.rb', line 66 def file caller_locations[0].path end |
#file_directory ⇒ Object
62 63 64 |
# File 'lib/vv/file_methods.rb', line 62 def file_directory File.(File.dirname(caller_locations[0].path)) end |
#make_directory(directory) ⇒ Object Also known as: create_directory, create_dir, make_dir
219 220 221 |
# File 'lib/vv/file_methods.rb', line 219 def make_directory directory FileUtils.mkdir(directory).first end |
#make_directory_if_not_exists(directory) ⇒ Object Also known as: make_dir_if_not_exists, create_directory_if_not_exists, create_dir_if_not_exists
209 210 211 |
# File 'lib/vv/file_methods.rb', line 209 def make_directory_if_not_exists directory FileUtils.mkdir_p(directory).first end |
#move_directory(*args, **kwargs) ⇒ Object
TODO: Think about making a ‘directory` method on
string so from / to is super clear.
121 122 123 124 125 126 127 128 129 |
# File 'lib/vv/file_methods.rb', line 121 def move_directory *args, **kwargs = \ %w[ Moving directories is confusing. Call either `rename_directory` or `move_directory_into` depending on your needs. There are many aliases. ] .spaced fail NoMethodError, end |
#move_directory_into(dir, into, allow_hidden: true, allow_absolute: true) ⇒ Object Also known as: move_dir_into, mv_dir_into
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/vv/file_methods.rb', line 131 def move_directory_into dir, into, allow_hidden: true, allow_absolute: true safe = dir.safe_dir_path? allow_hidden: allow_hidden, allow_absolute: allow_absolute safe &&= into.safe_dir_path? allow_hidden: allow_hidden, allow_absolute: allow_absolute = "Refusing to rename unsafe directory" fail unless safe = "Target #{into} is not a directory" fail unless into.is_directory_path? = "Source #{dir} is not a directory" fail unless dir.is_directory_path? FileUtils.mv dir, into end |
#remove(filepath, force: false) ⇒ Object Also known as: remove_file, rm
235 236 237 238 239 240 241 |
# File 'lib/vv/file_methods.rb', line 235 def remove filepath, force: false = \ "Cowardly refusing to remove directory (see `remove_directory`)" fail if filepath.is_directory_path? force ? FileUtils.rm_f(filepath) : FileUtils.rm(filepath) end |
#remove_directory(directory, quiet_if_gone: false) ⇒ Object Also known as: rm_directory, remove_dir, rm_dir
226 227 228 229 230 |
# File 'lib/vv/file_methods.rb', line 226 def remove_directory directory, quiet_if_gone: false no_directory_exists = ! directory.is_directory_path? return if quiet_if_gone && no_directory_exists FileUtils.remove_dir directory end |
#rename_directory(from, to, allow_hidden: true, allow_absolute: true) ⇒ Object Also known as: rename_dir
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/vv/file_methods.rb', line 95 def rename_directory from, to, allow_hidden: true, allow_absolute: true safe = from.safe_dir_path? allow_hidden: allow_hidden, allow_absolute: allow_absolute safe &&= to.safe_dir_path? allow_hidden: allow_hidden, allow_absolute: allow_absolute = "Refusing to rename unsafe directory" fail unless safe = "Source #{from} is not a directory" fail unless from.is_directory_path? = "Target directory name `#{to}` already exists" fail if to.is_directory_path? return FileUtils.mv from, to end |
#repo_directory(missing_throws_exception: true) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/vv/file_methods.rb', line 36 def repo_directory missing_throws_exception: true _path = caller_locations(1)[0].path _file_directory = File.(File.dirname(_path)) directory_fragments = File.vv_split _file_directory while directory_fragments.any? current_directory = File.join directory_fragments supported_source_control_directories = %w(.git .hg) supported_source_control_directories .each do | directory | full_path = File.join current_directory, directory path_exists = File.directory? full_path return current_directory if path_exists end directory_fragments.pop end return unless missing_throws_exception fail "No repository detected" end |
#separator ⇒ Object
74 75 76 |
# File 'lib/vv/file_methods.rb', line 74 def separator File::SEPARATOR end |
#vv_included? ⇒ Boolean
22 23 24 |
# File 'lib/vv/file_methods.rb', line 22 def vv_included? true end |