Class: KXI::Application::Workspace

Inherits:
Object
  • Object
show all
Defined in:
lib/kxi/application/workspace.rb

Overview

Represents a workspace (directory)

Instance Method Summary collapse

Constructor Details

#initialize(path, mk = false) ⇒ Workspace

Instantiates the KXI::Application::Workspace class

Parameters:

  • path (String)

    Path to workspace

  • mk (Bool) (defaults to: false)

    True if workspace should be created; otherwise false



10
11
12
13
14
# File 'lib/kxi/application/workspace.rb', line 10

def initialize(path, mk = false)
	@path = File.expand_path(path)
	validate
	make if mk
end

Instance Method Details

#dirs(ptt = nil, rec = true) ⇒ Array<String>

Preforms lookup of directories (glob)

Parameters:

  • ptt (String, nil) (defaults to: nil)

    Pattern for searching, returns all directories if nil

  • rec (Bool) (defaults to: true)

    Determines weather search is recursive

Returns:

  • (Array<String>)

    Found directories



90
91
92
93
94
# 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

#files(ptt = nil, rec = true) ⇒ Array<String>

Preforms lookup of files (glob)

Parameters:

  • ptt (String, nil) (defaults to: nil)

    Pattern for searching, returns all files if nil

  • rec (Bool) (defaults to: true)

    Determines weather search is recursive

Returns:

  • (Array<String>)

    Found files



80
81
82
83
84
# 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

#inner(*path) ⇒ KXI::Application::Workspace

Gets a child workspace

Parameters:

  • path (String)

    Path to child workspace

Returns:



51
52
53
# File 'lib/kxi/application/workspace.rb', line 51

def inner(*path)
	return KXI::Application::Workspace.new(path(*path))
end

#made?Bool

Checks if workspace physically exists in the file system

Returns:

  • (Bool)

    True if workspace exists; false otherwise



34
35
36
37
# File 'lib/kxi/application/workspace.rb', line 34

def made?
	validate
	return Dir.exists?(@path)
end

#makeObject

Creates the workspace, if it doesn’t exist



40
41
42
43
44
45
46
# 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

#open(mode, *path) ⇒ IO #open(mode, *path) {|handle| ... } ⇒ Object

Opens a file withing the workspace

Overloads:

  • #open(mode, *path) ⇒ IO

    Returns File handle.

    Parameters:

    • mode (String)

      IO mode to open the file with

    • path (String)

      Path to desired file

    Returns:

    • (IO)

      File handle

  • #open(mode, *path) {|handle| ... } ⇒ Object

    Parameters:

    • mode (String)

      IO mode to open the file with

    • path (String)

      Path to desired file

    Yields:

    • (handle)

      Block where file is used

    Yield Parameters:

    • handle (IO)

      Handle of block



65
66
67
68
69
70
71
72
73
74
# 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

#parentKXI::Application::Workspace?

Obtains the parent of this workspace, if it exists

Returns:



26
27
28
29
30
# 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

#path(*path) ⇒ String

Gets path relative to this workspace

Parameters:

  • path (String)

    Parts of path

Returns:

  • (String)

    The combined path



19
20
21
22
# 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