Sha256: a9020791e2f56bed855c093887ae313ca51cd90513d0b05873230d47768793ad

Contents?: true

Size: 1.41 KB

Versions: 8

Compression:

Stored size: 1.41 KB

Contents

# helper class to create job directories
class OSC::Machete::JobDir
  def initialize(parent_directory)
    @target = Pathname.new(parent_directory).cleanpath
  end

  # Returns a unique path for a job
  #
  # @return [String] A path of a unique job directory as string.
  def new_jobdir
    @target + unique_dir
  end

  #FIXME: BELOW METHODS SHOULD BE PRIVATE

  # return true if the string is a job dir name
  def jobdir_name?(name)
    name[/^\d+$/]
  end

  # return true if Pathname is a job directory
  # FIXME: this is not used anywhere; remove it?
  def jobdir?(path)
    jobdir_name?(path.basename.to_s) && path.directory?
  end

  # get a list of all job directories
  # FIXME: this is not used anywhere; remove it?
  def jobdirs
    @target.exist? ? @target.children.select { |i| jobdir?(i) } : []
  end


  # get a list of directories in the target directory
  # FIXME: this is not used anywhere; remove it?
  def targetdirs
    @target.exist? ? @target.children.select(&:directory?) : []
  end

  # find the next unique integer name for a job directory
  def unique_dir
    taken_ints = taken_paths.map { |path| path.basename.to_s.to_i }
    (taken_ints.count > 0) ? (taken_ints.max + 1).to_s : 1.to_s
  end

  private

  # paths that are unavailable for creating a new job directory
  def taken_paths
    if @target.exist?
      @target.children.select { |path| jobdir_name?(path.basename.to_s) }
    else
      []
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
osc-machete-2.0.0 lib/osc/machete/job_dir.rb
osc-machete-1.2.2 lib/osc/machete/job_dir.rb
osc-machete-1.2.1 lib/osc/machete/job_dir.rb
osc-machete-1.2.0 lib/osc/machete/job_dir.rb
osc-machete-2.0.0.pre2 lib/osc/machete/job_dir.rb
osc-machete-1.1.4 lib/osc/machete/job_dir.rb
osc-machete-2.0.0.pre1 lib/osc/machete/job_dir.rb
osc-machete-1.1.3 lib/osc/machete/job_dir.rb