Module: Smash::CloudPowers::PathHelp
- Included in:
- Helpers
- Defined in:
- lib/cloud_powers/helpers/path_help.rb
Instance Method Summary collapse
-
#common_delimiter ⇒ Object
Offer a common “path” delimiter.
-
#expand_path(arg) ⇒ Object
Expand a +Pathname variable.
-
#file_exists?(*args) ⇒ Boolean
Check if the URL exists.
-
#file_search(name, scope = '/**') ⇒ Object
Find all directories that match the given string.
-
#filename?(pathname) ⇒ Boolean
Determine if this path or name seems like a file or a directory.
-
#job_exist?(file) ⇒ Boolean
Check if the job file exists in the job directory.
-
#job_path(file = '') ⇒ Object
Gives the path from the project root to lib/jobs.
-
#job_require_path(file_name = '') ⇒ Object
Gives the path from the project root to lib/jobs.
-
#path_search(name, scope = default_scope) ⇒ Object
Find all directories that match the given string.
- #paths_gcd(*paths) ⇒ Object
- #paths_lcd(*paths) ⇒ Object
-
#to_path(*args) ⇒ Object
Convert a list of arguments to a path-like
String
object. -
#to_pathname(*args) ⇒ Object
Convert a list of arguments to a
Pathname
object. -
#to_realpath(*args) ⇒ Object
Create and/or get the full path to the argument(s).
-
#touch(path) ⇒ Object
Create a file or directory.
-
#zlib_path(object = '') ⇒ Object
Offer a common place to operate from, so
CloudPowers
stays tidy.
Instance Method Details
#common_delimiter ⇒ Object
Offer a common “path” delimiter. This is designed to allow Storage keep a common delimiter. It’s not required but it makes things a little more human friendly
Returns String
15 16 17 |
# File 'lib/cloud_powers/helpers/path_help.rb', line 15 def common_delimiter '/'.freeze end |
#expand_path(arg) ⇒ Object
Expand a +Pathname variable
Parameters
-
arg
String
Returns
-
Pathname
- Absolute path
Notes:
-
This method doesn’t guarantee that the path actually exists; It just gives you a correct, absolute path
30 31 32 |
# File 'lib/cloud_powers/helpers/path_help.rb', line 30 def (arg) to_pathname(arg). end |
#file_exists?(*args) ⇒ Boolean
Check if the URL exists
Parameters
-
file
String
Returns Boolean
41 42 43 |
# File 'lib/cloud_powers/helpers/path_help.rb', line 41 def file_exists?(*args) File.exist?(*args.join('/').to_s) end |
#file_search(name, scope = '/**') ⇒ Object
Find all directories that match the given string.
Returns
-
Pathname
- absolute path
Notes
-
This method is su-looooOOOOOOOw but it’s pretty thorough. Fixing that while keeping it thorough is a high priority but it works. Your best bet is to try to set the location for things you’re looking for, before you use them. Another alternative is to use a jailed root to run your project from.
122 123 124 |
# File 'lib/cloud_powers/helpers/path_help.rb', line 122 def file_search(name, scope = '/**') Pathname.glob("#{scope}/#{name}").select(&:exist?).reject(&:directory?).map(&:realpath) end |
#filename?(pathname) ⇒ Boolean
Determine if this path or name seems like a file or a directory
Parameters
-
Pathname
|String
Returns
-
Boolean
52 53 54 |
# File 'lib/cloud_powers/helpers/path_help.rb', line 52 def filename?(pathname) !!(/[A-Za-z]*\.[A-Za-z]+$/ =~ pathname.to_s) end |
#job_exist?(file) ⇒ Boolean
Check if the job file exists in the job directory
Parameters
-
file
String
Returns Boolean
Notes
-
See
#job_home()
-
See
#path_exists?()
67 68 69 |
# File 'lib/cloud_powers/helpers/path_help.rb', line 67 def job_exist?(file) file_exists?(job_home, file) end |
#job_path(file = '') ⇒ Object
Gives the path from the project root to lib/jobs
Parameters
-
file
String
(optional) (default is ”) - name of a file
Returns
-
path/file
String
iffile
parameter is given. return has ‘.rb’ extension included -
file
String
iffile
parameter is not given it will return the#job_require_path()
Notes
-
See
#job_home
84 85 86 87 |
# File 'lib/cloud_powers/helpers/path_help.rb', line 84 def job_path(file = '') return job_home if file.empty? to_pathname(job_home, file) end |
#job_require_path(file_name = '') ⇒ Object
Gives the path from the project root to lib/jobs
Parameters String (optional)
-
file_name name of a file
Returns
-
path/file
String
iffile_name
was given -
path to job_directory if
file_name
was not given
Notes
-
Neither path nor file will have a file extension
-
See
#job_home
101 102 103 104 105 106 107 108 109 |
# File 'lib/cloud_powers/helpers/path_help.rb', line 101 def job_require_path(file_name = '') # TODO: refactor to use Pathname as much as possible begin file_sans_extension = File.basename(file_name, '.*') (Pathname.new(job_home) + file_sans_extension).to_s rescue Errno::ENOENT nil end end |
#path_search(name, scope = default_scope) ⇒ Object
Find all directories that match the given string.
Returns
-
Pathname
- absolute path
Notes
-
This method is su-looooOOOOOOOw but it’s pretty thorough. Fixing that while keeping it thorough is a high priority but it works. Your best bet is to try to set the location for things you’re looking for, before you use them. Another alternative is to use a jailed root to run your project from.
157 158 159 |
# File 'lib/cloud_powers/helpers/path_help.rb', line 157 def path_search(name, scope = default_scope) Pathname.glob("#{scope}/#{name}").select(&:exist?).select(&:directory?).map(&:realpath) end |
#paths_gcd(*paths) ⇒ Object
141 142 143 144 |
# File 'lib/cloud_powers/helpers/path_help.rb', line 141 def paths_gcd(*paths) start_from = paths_lcd(*paths) start_from + paths.sort.last.relative_path_from(start_from) end |
#paths_lcd(*paths) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/cloud_powers/helpers/path_help.rb', line 127 def paths_lcd(*paths) unpacked_paths = paths.map { |path| path.to_s.split('/') }.sort shortest_path_dirs = unpacked_paths.first.count # this is the shortest path common_path = [] (0..shortest_path_dirs).each do |i| if unpacked_paths.first[i] == unpacked_paths.last[i] common_path << unpacked_paths.first[i] end end to_pathname(common_path.join('/')) end |
#to_path(*args) ⇒ Object
Convert a list of arguments to a path-like String
object.
Parameters
Object
[,…]
Returns
-
String
168 169 170 |
# File 'lib/cloud_powers/helpers/path_help.rb', line 168 def to_path(*args) to_pathname(*args).to_s end |
#to_pathname(*args) ⇒ Object
Convert a list of arguments to a Pathname
object.
Parameters
String
[,…]
Returns
-
Pathname
179 180 181 182 183 184 185 186 187 |
# File 'lib/cloud_powers/helpers/path_help.rb', line 179 def to_pathname(*args) args = args.flatten.compact # #join() will create a path that looks like this `//your/path` if the # first argument is the same as the path seperator on your system. first = /^\/+\W*$/ =~ args.first.to_s ? args.shift : '' path_string = args.join('/') path_string = first + path_string Pathname.new(path_string) end |
#to_realpath(*args) ⇒ Object
Create and/or get the full path to the argument(s)
Parameters
-
String
(s) - a path or chunks of a path, in order, as Strings
Returns
-
Pathname
Notes:
-
!This method creates paths, even nested paths, if they don’t exist!
-
!Use
expand_path
if you don’t want to create anything but still need the full path -
See
#to_pathname
-
See
#touch
203 204 205 206 207 208 209 210 211 212 |
# File 'lib/cloud_powers/helpers/path_help.rb', line 203 def to_realpath(*args) begin pathname = to_pathname(*args) pathname.realpath rescue Errno::ENOENT => e logger.debug("#to_realpath(#{pathname}) called from " + "#{caller.first} but the path doesn't exist") to_realpath(touch(pathname)) end end |
#touch(path) ⇒ Object
Create a file or directory. You don’t have to worry if the path exists.
Parameters
-
path
Pathname
|String
- path that you would like created
Returns
-
Pathname
- path to your new object
Notes:
-
This method assumes you are careful enough not to break your machine because you might accidentally over-write an existing, important path with an empty file or directory. That’s no different than any other library, like
FileUtils
orPathname
but it’s worht being said, just for good measure.
228 229 230 231 232 |
# File 'lib/cloud_powers/helpers/path_help.rb', line 228 def touch(path) pathname = to_pathname(path) filename?(pathname) ? ::FileUtils.touch(pathname) : ::FileUtils.mkdir_p(pathname) to_realpath(pathname) end |
#zlib_path(object = '') ⇒ Object
Offer a common place to operate from, so CloudPowers
stays tidy. This method will find out if your project responds to a method called ‘project_root’. If the method exist, it uses it, otherwise, your new files etc. will be places at `pwd`/zlib
.
Returns
-
Pathname
Notes:
-
See
to_realpath
244 245 246 247 |
# File 'lib/cloud_powers/helpers/path_help.rb', line 244 def zlib_path(object = '') return zlib_home if (object.nil? || object.empty?) to_path(zlib_home, object) end |