Module: DirectoryWatcher::Paths

Extended by:
Paths
Included in:
DirectoryWatcher, Paths
Defined in:
lib/directory_watcher/paths.rb

Overview

Paths contains helpful methods to determine paths of files inside the DirectoryWatcher library

Instance Method Summary (collapse)

Instance Method Details

- (Object) lib_path(*args, &block)

Return a path relative to the 'lib' directory in this project



20
21
22
# File 'lib/directory_watcher/paths.rb', line 20

def lib_path(*args,&block)
  sub_path('lib', *args, &block)
end

- (Object) path(*args, &block)

Return a path relative to the 'root' directory in the project



26
27
28
# File 'lib/directory_watcher/paths.rb', line 26

def path(*args,&block)
  sub_path('', *args, &block)
end

- (Object) root_dir

The root directory of the project is considered the parent directory of the 'lib' directory.

Returns The full expanded path of the parent directory of 'lib' going up the path from the current file. Trailing File::SEPARATOR is guaranteed



12
13
14
15
16
# File 'lib/directory_watcher/paths.rb', line 12

def root_dir
  path_parts = ::File.expand_path(__FILE__).split(::File::SEPARATOR)
  lib_index = path_parts.rindex("lib")
  return path_parts[0...lib_index].join(::File::SEPARATOR) + ::File::SEPARATOR
end

- (Object) sub_path(sub, *args, &block)

Calculate the full expanded path of the item with respect to a sub path of 'root_dir'



33
34
35
36
37
38
39
40
41
42
# File 'lib/directory_watcher/paths.rb', line 33

def sub_path(sub,*args,&block)
  rv = ::File.join(root_dir, sub) + ::File::SEPARATOR
  rv = ::File.join(rv, *args) if args
  if block
    with_load_path( rv ) do
      rv = block.call
    end
  end
  return rv
end

- (Object) with_load_path(path, &block)

Execute a block in the context of a path added to $LOAD_PATH



46
47
48
49
50
51
# File 'lib/directory_watcher/paths.rb', line 46

def with_load_path(path, &block)
  $LOAD_PATH.unshift path
  block.call
ensure
  $LOAD_PATH.shift
end