class MultiDir::Paths

Can resolve paths using symbols.

Public Class Methods

define(name, options = {}) click to toggle source

Define a new semantic path.

# File lib/multi_dir/paths.rb, line 119
def define(name, options = {})
  instance.define name, options
end
instance() click to toggle source
# File lib/multi_dir/paths.rb, line 105
def instance
  @instance ||= new
end
load_yaml(file) click to toggle source
# File lib/multi_dir/paths.rb, line 113
def load_yaml(file)
  instance.load_yaml! file
end
new(paths = {}) click to toggle source
# File lib/multi_dir/paths.rb, line 9
def initialize(paths = {})
  self.paths.merge! paths.symbolize_keys unless paths.nil? or paths.empty?
end
reset_instance() click to toggle source
# File lib/multi_dir/paths.rb, line 109
def reset_instance
  @instance = nil
end

Public Instance Methods

default_paths() click to toggle source
# 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 = {}) click to toggle source
# 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_paths() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/multi_dir/paths.rb, line 86
def load_yaml!(file)
  paths.merge! load_yaml file
end
paths() click to toggle source
# File lib/multi_dir/paths.rb, line 39
def paths
  @paths ||= load_paths
end
resolve(symbol) click to toggle source

Resolve symbolic path to real path.

# 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_root() click to toggle source

Resolve root path.

# 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