Class: ToPass::Directories

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

Overview

Wrapper for the search directories. Primary purpose is to keep the dirty part in one place instead of scattered throughout the project.

Used by ToPass::AlgorithmReader and ToPass::ConverterReader

Class Method Summary collapse

Class Method Details

.[](key) ⇒ Object

get a directory or a list of directories



13
14
15
16
17
18
19
20
21
22
# File 'lib/to_pass/directories.rb', line 13

def [](key)
  case key
  when *all.keys - [:standard, :all]
    all[key]
  when :standard
    [ all[:user], all[:data], all[:source_data] ]
  when :all
    user_paths + [ all[:user], all[:data], all[:source_data] ]
  end
end

.[]=(key, value) ⇒ Object

a new path can be added to have a broader or specialised lookup



25
26
27
28
29
# File 'lib/to_pass/directories.rb', line 25

def []=(key, value)
  add_user_key(key)

  all[key] = value
end

.add_user_key(key) ⇒ Object (private)

adds a new key to the list to distinguish user added keys from built in ones



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

def add_user_key(key)
  (@user_keys ||= []) << key unless all.has_key?(key)
end

.allObject (private)

list of all directories used by this project



51
52
53
54
55
56
57
58
# File 'lib/to_pass/directories.rb', line 51

def all
  @all ||= {
    :user => File.expand_path("~/.#{APP_NAME}"),
    :data => "#{ruby_data_dir}/#{APP_NAME}",
    :base => File.expand_path("#{File.dirname(__FILE__)}/../.."),
    :source_data => File.expand_path("#{File.dirname(__FILE__)}/../../data/#{APP_NAME}"),
  }
end

.include?(search) ⇒ Boolean

checks wether the path is already known by value or key

Returns:

  • (Boolean)


32
33
34
# File 'lib/to_pass/directories.rb', line 32

def include?(search)
  all.has_value?(search) || all.has_key?(search)
end

.ruby_data_dirObject (private)

retrieve the ruby data-dir from different version of ruby.

The latter version is present in 1.8.7 p249/302



63
64
65
# File 'lib/to_pass/directories.rb', line 63

def ruby_data_dir
  RbConfig::CONFIG['data-dir'] || RbConfig::CONFIG['datadir']
end

.user_pathsObject (private)

return the array of user-supplied paths



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

def user_paths
  @user_keys.to_a.map do |key|
    all[key]
  end
end