Sha256: a5fb496dfb3489ef70ae35c16002ee7e610347e617b138b5c93e573f79ecbbbe

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

#
# A helper for building path on the file system
# Author: Nayyara Samuel (nayyara.samuel@opower.com)
#
module LoadPath
  class PathHelper
    attr_accessor :root_dir

    # Construct with a root
    def initialize(root_dir, expand_path=false)
      if (expand_path)
        @root_dir = File.expand_path(root_dir)
      else
        @root_dir = root_dir
      end
    end

    # Construct a parent relative to the root by name
    def parent_directory(directory_name, levels_up={up: 1})
      levels_up = levels_up[:up].to_i + 1
      File.join(root_dir, levels_up.times.map { |_| '..' }, directory_name)
    end

    # Construct a child relative to the root by name
    def child_directory(*directory_list)
      File.join(root_dir, directory_list)
    end

    # Construct a sibling relative to the root by name
    def sibling_directory(directory_name)
      parent_directory(directory_name, up: 0)
    end

    def file_path(file_name=nil)
      if (file_name)
        File.join(root_dir, file_name)
      else
        root_dir
      end
    end
  end

# Builder for path helper
  class PathBuilder
    attr_accessor :path_helper

    def initialize(root_dir, expand_path=false)
      @path_helper = PathHelper.new(root_dir, expand_path)
    end

    def parent_directory(*args)
      @path_helper = PathHelper.new(@path_helper.parent_directory(*args))
      self
    end

    def child_directory(*args)
      @path_helper = PathHelper.new(@path_helper.child_directory(*args))
      self
    end

    def sibling_directory(*args)
      @path_helper = PathHelper.new(@path_helper.sibling_directory(*args))
      self
    end

    def file_path(file_name=nil)
      @path_helper.file_path(file_name)
    end

    def to_s
      file_path
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
load_path-0.2.0 lib/path_builder.rb