Module: Smash::CloudPowers::PathHelp

Included in:
Helpers
Defined in:
lib/cloud_powers/helpers/path_help.rb

Instance Method Summary collapse

Instance Method Details

#job_exist?(file) ⇒ Boolean

Check if the job file exists in the job directory

Parameters

  • file String

Returns Boolean

Notes

  • See #job_home()

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
# File 'lib/cloud_powers/helpers/path_help.rb', line 59

def job_exist?(file)
  begin
    File.new("#{job_home}/#{file}")
    true
  rescue Errno::ENOENT
    false
  end
end

#job_homeObject

Gives a common home for jobs to live so they can be easily grouped and found. This method will create nested directories, based on the #project_root() method and an additional ‘lib/jobs’ directory. If no project root has been set by the time this method is called, a new directory will be created relative to the gem’s project root.

Returns String

Notes

  • # If no project root has been set by the time this method is called, a new

directory will be created relative to the gem’s project root. This might have deeper implications than you want to deal with so it’s always a good idea to set your project root as soon as you can.

  • TODO: find a way to have this method figure out the actual project’s root, as opposed to just making common “good” assumptions.



25
26
27
28
# File 'lib/cloud_powers/helpers/path_help.rb', line 25

def job_home
  string_th = FileUtils.mkdir_p("#{project_root}/lib/jobs/").first
  @job_home ||= Pathname.new(string_th).realpath.to_s
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 if file parameter is given. return has ‘.rb’ extension included

  • file String if file parameter is not given it will return the #job_require_path()

Notes

  • See #job_home



43
44
45
46
# File 'lib/cloud_powers/helpers/path_help.rb', line 43

def job_path(file = '')
 return job_home if file.empty?
  Pathname.new("#{job_home}/#{file}").to_s
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 if file_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



80
81
82
83
84
85
86
87
# File 'lib/cloud_powers/helpers/path_help.rb', line 80

def job_require_path(file_name = '')
  begin
    file_sans_extension = File.basename(file_name, '.*')
    (Pathname.new(job_home) + file_sans_extension).to_s
  rescue Errno::ENOENT
    nil
  end
end