Class: MultiDir::Paths
- Inherits:
-
Object
- Object
- MultiDir::Paths
- Defined in:
- lib/multi_dir/paths.rb
Overview
Can resolve paths using symbols.
Class Method Summary collapse
-
.define(name, options = {}) ⇒ Object
Define a new semantic path.
- .instance ⇒ Object
- .load_yaml(file) ⇒ Object
- .reset_instance ⇒ Object
Instance Method Summary collapse
- #default_paths ⇒ Object
- #define(name, options = {}) ⇒ Object
-
#initialize(paths = {}) ⇒ Paths
constructor
A new instance of Paths.
- #load_paths ⇒ Object
- #load_yaml(file) ⇒ Object
- #load_yaml!(file) ⇒ Object
- #paths ⇒ Object
-
#resolve(symbol) ⇒ Object
Resolve symbolic path to real path.
-
#resolve_root ⇒ Object
Resolve root path.
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, = {}) instance.define name, end |
.instance ⇒ Object
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_instance ⇒ Object
109 110 111 |
# File 'lib/multi_dir/paths.rb', line 109 def reset_instance @instance = nil end |
Instance Method Details
#default_paths ⇒ Object
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, = {}) name = name.to_s if MultiDir.methods.include?(name) raise ArgumentError.new "Path name `#{name}` would override already defined method on MultiDir." end parent = [:id] || [:parent] paths[name] = [ parent, name.to_s ] MultiDir.define_path_method name end |
#load_paths ⇒ Object
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. 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 |
#paths ⇒ Object
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_root ⇒ Object
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 |