Class: MultiDir::Paths

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

Overview

Can resolve paths using symbols.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paths = {}) ⇒ Paths

Returns a new instance of Paths.



9
10
11
# File 'lib/multi_dir/paths.rb', line 9

def initialize(paths = {})
  self.paths.merge! paths.symbolize_keys unless paths.nil? or paths.empty?
end

Class Method Details

.define(name, options = {}) ⇒ Object

Define a new semantic path.



119
120
121
# File 'lib/multi_dir/paths.rb', line 119

def define(name, options = {})
  instance.define name, options
end

.instanceObject



105
106
107
# File 'lib/multi_dir/paths.rb', line 105

def instance
  @instance ||= new
end

.load_yaml(file) ⇒ Object



113
114
115
# File 'lib/multi_dir/paths.rb', line 113

def load_yaml(file)
  instance.load_yaml! file
end

.reset_instanceObject



109
110
111
# File 'lib/multi_dir/paths.rb', line 109

def reset_instance
  @instance = nil
end

Instance Method Details

#default_pathsObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/multi_dir/paths.rb', line 54

def default_paths
  {
      :bin    => [:root, 'bin'],
      :lib    => [:root, 'lib'],
      :tmp    => [:root, 'tmp'],
      :cache  => [:tmp, 'cache'],
      :config => [:root, 'config'],
      :files  => [:root, 'files']
  }
end

#define(name, options = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/multi_dir/paths.rb', line 90

def define(name, options = {})
  name = name.to_s

  if MultiDir.methods.include?(name)
    raise ArgumentError.new "Path name `#{name}` would override already defined method on MultiDir."
  end

  parent = options[:id] || options[:parent]
  paths[name] = [ parent, name.to_s ]

  MultiDir.define_path_method name
end

#load_pathsObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/multi_dir/paths.rb', line 43

def load_paths
  paths = default_paths

  [ 'multi_dir.yml', ENV['MULTI_DIR_CONFIG'] ].reject(&:nil?).each do |file|
    next unless File.exists? file
    paths.merge! load_yaml file
  end

  paths
end

#load_yaml(file) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/multi_dir/paths.rb', line 65

def load_yaml(file)
  raise ArgumentError.new "File `#{file}` does not exists." unless File.exists? file
  raise ArgumentError.new "File `#{file}` is not readable." unless File.readable? file
  data = YAML.load_file(file).symbolize_keys

  unless data.is_a? Hash and data.has_key? :paths
    raise ArgumentError.new "File `#{file}` does not contain a valid MultiDir YAML definition."
  end

  data[:paths].inject({}) do |memo, row|
    key, path = row[0].to_sym, row[1].to_s
    memo[key] = if %w(/ .).include? path[0].chr
      File.expand_path path.to_s
    else
      [ :root, path.to_s ]
    end

    memo
  end
end

#load_yaml!(file) ⇒ Object



86
87
88
# File 'lib/multi_dir/paths.rb', line 86

def load_yaml!(file)
  paths.merge! load_yaml file
end

#pathsObject



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

def paths
  @paths ||= load_paths
end

#resolve(symbol) ⇒ Object

Resolve symbolic path to real path.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/multi_dir/paths.rb', line 15

def resolve(symbol)
  case symbol
    when :root
      resolve_root
    else
      raise ArgumentError.new "Path symbol `#{symbol.inspect}` does not exist." unless paths.has_key? symbol

      path = paths[symbol]
      if path.is_a? Array
        File.join resolve(path[0]), path[1].to_s
      else
        path.to_s
      end
  end
end

#resolve_rootObject

Resolve root path.



33
34
35
36
37
# File 'lib/multi_dir/paths.rb', line 33

def resolve_root
  return paths[:root] if paths.has_key? :root
  return ::Rails.root.to_s if Object.const_defined?(:Rails) && ::Rails.respond_to?(:root)
  Pathname.pwd.to_s
end