class KXI::Application::Workspace
Represents a workspace (directory)
Public Class Methods
Instantiates the {KXI::Application::Workspace} class @param path [String] Path to workspace @param mk [Bool] True if workspace should be created; otherwise false
# File lib/kxi/application/workspace.rb, line 10 def initialize(path, mk = false) @path = File.expand_path(path) validate make if mk end
Public Instance Methods
Preforms lookup of directories (glob) @param ptt [String, nil] Pattern for searching, returns all directories if nil @param rec [Bool] Determines weather search is recursive @return [Array<String>] Found directories
# File lib/kxi/application/workspace.rb, line 90 def dirs(ptt = nil, rec = true) ptt = '*' if ptt == nil ptt = File.join('**', ptt) if rec return Dir.glob(path(ptt), File::FNM_DOTMATCH).select { |i| File.exists?(i) and File.directory?(i) }.collect { |i| File.expand_path(i) } end
Preforms lookup of files (glob) @param ptt [String, nil] Pattern for searching, returns all files if nil @param rec [Bool] Determines weather search is recursive @return [Array<String>] Found files
# File lib/kxi/application/workspace.rb, line 80 def files(ptt = nil, rec = true) ptt = '*' if ptt == nil ptt = File.join('**', ptt) if rec return Dir.glob(path(ptt), File::FNM_DOTMATCH).select { |i| File.exists?(i) and not File.directory?(i) }.collect { |i| File.expand_path(i) } end
Gets a child workspace @param [String] path Path to child workspace @return [KXI::Application::Workspace] Child workspace
# File lib/kxi/application/workspace.rb, line 51 def inner(*path) return KXI::Application::Workspace.new(path(*path)) end
Checks if workspace physically exists in the file system @return [Bool] True if workspace exists; false otherwise
# File lib/kxi/application/workspace.rb, line 34 def made? validate return Dir.exists?(@path) end
Creates the workspace, if it doesn't exist
# File lib/kxi/application/workspace.rb, line 40 def make validate return if made? p = parent p.make unless parent == nil Dir.mkdir(@path) end
Opens a file withing the workspace @overload open(mode, *path)
@param mode [String] IO mode to open the file with @param path [String] Path to desired file @return [IO] File handle
@overload open(mode, *path)
@param mode [String] IO mode to open the file with @param path [String] Path to desired file @yield [handle] Block where file is used @yieldparam handle [IO] Handle of block
# File lib/kxi/application/workspace.rb, line 65 def open(mode, *path) h = File.open(path(*path), mode) return h unless block_given? begin yield(h) ensure h.flush h.close end end
Obtains the parent of this workspace, if it exists @return [KXI::Application::Workspace, nil] Parent workspace if any; nil otherwise
# File lib/kxi/application/workspace.rb, line 26 def parent dir = File.dirname(@path) return nil if KXI::Platform.this.path_case_sensitive? ? dir == @path : dir.downcase == @path.downcase return KXI::Application::Workspace.new(dir) end
Gets path relative to this workspace @param path [String] Parts of path @return [String] The combined path
# File lib/kxi/application/workspace.rb, line 19 def path(*path) return @path if path.length == 0 return File.expand_path(File.join(@path, *path)) end
Private Instance Methods
Validates path of workspace @raise [KXI::Exceptions::WorkspaceCollisionException] If workspace path is used by file
# File lib/kxi/application/workspace.rb, line 98 def validate raise(KXI::Exceptions::WorkspaceCollisionException.new(@path)) if File.exists?(@path) and not File.directory?(@path) end