Class: LoadPath::LoadPathSetup

Inherits:
Object
  • Object
show all
Defined in:
lib/load_path.rb

Overview

Helper class to setup the load path

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_dir) ⇒ LoadPathSetup

Construct with a root



39
40
41
# File 'lib/load_path.rb', line 39

def initialize(root_dir)
  @root_dir = root_dir
end

Instance Attribute Details

#root_dirObject

Returns the value of attribute root_dir.



36
37
38
# File 'lib/load_path.rb', line 36

def root_dir
  @root_dir
end

Instance Method Details

#add(directory) ⇒ Object

Add the given directory to the load path



51
52
53
# File 'lib/load_path.rb', line 51

def add(directory)
  $: << directory
end

#child_directory(*directory_list) ⇒ Object

Construct a child relative to the root by name



62
63
64
# File 'lib/load_path.rb', line 62

def child_directory(*directory_list)
  File.join(root_dir, directory_list)
end

#parent_directory(directory_name, levels_up = {up: 1}) ⇒ Object

Construct a parent relative to the root by name



56
57
58
59
# File 'lib/load_path.rb', line 56

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

#require_files(directory, file_pattern = '*.rb') ⇒ Object

Require all files matching the pattern in the directory



44
45
46
47
48
# File 'lib/load_path.rb', line 44

def require_files(directory, file_pattern='*.rb')
  Dir.glob(File.join(directory, file_pattern)) do |file|
    require File.basename(file)
  end
end

#sibling_directory(directory_name) ⇒ Object

Construct a sibling relative to the root by name



67
68
69
# File 'lib/load_path.rb', line 67

def sibling_directory(directory_name)
  parent_directory(directory_name, up: 0)
end