Module: Path
- Defined in:
- lib/rbbt/resource/path.rb,
lib/rbbt/resource/util.rb
Constant Summary collapse
- SEARCH_PATHS =
{ :current => File.join("{PWD}", "{TOPLEVEL}", "{SUBPATH}"), :user => File.join(ENV['HOME'], ".{PKGDIR}", "{TOPLEVEL}", "{SUBPATH}"), :global => File.join('/', "{TOPLEVEL}", "{PKGDIR}", "{SUBPATH}"), :local => File.join('/usr/local', "{TOPLEVEL}", "{PKGDIR}", "{SUBPATH}"), :lib => File.join('{LIBDIR}', "{TOPLEVEL}", "{SUBPATH}"), :default => :user }
- SLASH =
"/"[0]
- DOT =
"."[0]
Instance Attribute Summary collapse
-
#pkgdir ⇒ Object
Returns the value of attribute pkgdir.
-
#resource ⇒ Object
Returns the value of attribute resource.
Class Method Summary collapse
- .caller_lib_dir(file = nil, relative_to = 'lib') ⇒ Object
- .setup(string, pkgdir = nil, resource = nil) ⇒ Object
Instance Method Summary collapse
- #[](name, orig = false) ⇒ Object
- #all_fields ⇒ Object
- #annotate(name) ⇒ Object
- #basename ⇒ Object
- #byte(pos) ⇒ Object
- #dirname ⇒ Object
- #doc_file(relative_to = 'lib') ⇒ Object
- #exists? ⇒ Boolean
- #fields ⇒ Object
- #filename ⇒ Object
- #find(where = nil, caller_lib = nil, search_paths = nil) ⇒ Object
- #find_all(caller_lib = nil, search_paths = nil) ⇒ Object
- #glob(pattern = '*') ⇒ Object
- #identifier_file_path ⇒ Object
- #identifier_files ⇒ Object
-
#in_dir?(dir) ⇒ Boolean
{{{ Methods.
- #index(options = {}) ⇒ Object
- #join(name) ⇒ Object
- #keys(field = 0, sep = "\t") ⇒ Object
- #list ⇒ Object
- #located? ⇒ Boolean
- #method_missing(name, prev = nil, *args, &block) ⇒ Object
- #open(options = {}, &block) ⇒ Object
- #pipe_to(cmd, options = {}) ⇒ Object
- #pos_index(pos, options = {}) ⇒ Object
- #produce(force = false) ⇒ Object
- #range_index(start, eend, options = {}) ⇒ Object
- #read(&block) ⇒ Object
- #set_extension(new_extension = nil) ⇒ Object
- #source_for_doc_file(relative_to = 'lib') ⇒ Object
- #to_s ⇒ Object
- #to_yaml(*args) ⇒ Object
- #tsv(*args) ⇒ Object
- #write(*args, &block) ⇒ Object
- #yaml ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, prev = nil, *args, &block) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rbbt/resource/path.rb', line 43 def method_missing(name, prev = nil, *args, &block) if block_given? super name, prev, *args, &block else # Fix problem with ruby 1.9 calling methods by its own initiative. ARG super(name, prev, *args) if name.to_s =~ /^to_/ if prev.nil? join name else join(prev).join(name) end end end |
Instance Attribute Details
#pkgdir ⇒ Object
Returns the value of attribute pkgdir.
5 6 7 |
# File 'lib/rbbt/resource/path.rb', line 5 def pkgdir @pkgdir end |
#resource ⇒ Object
Returns the value of attribute resource.
5 6 7 |
# File 'lib/rbbt/resource/path.rb', line 5 def resource @resource end |
Class Method Details
.caller_lib_dir(file = nil, relative_to = 'lib') ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/rbbt/resource/util.rb', line 3 def self.caller_lib_dir(file = nil, relative_to = 'lib') file = caller.reject{|l| l =~ /rbbt\/(?:resource\.rb|workflow\.rb)/ or l =~ /rbbt\/resource\/path\.rb/ or l =~ /rbbt\/util\/misc\.rb/ or l =~ /progress-monitor\.rb/ }.first.sub(/\.rb[^\w].*/,'.rb') if file.nil? file = File. file return Path.setup(file) if File.exists? File.join(file, relative_to) while file != '/' dir = File.dirname file return Path.setup(dir) if File.exists? File.join(dir, relative_to) file = File.dirname file end return nil end |
.setup(string, pkgdir = nil, resource = nil) ⇒ Object
7 8 9 10 11 12 |
# File 'lib/rbbt/resource/path.rb', line 7 def self.setup(string, pkgdir = nil, resource = nil) string.extend Path string.pkgdir = pkgdir || 'rbbt' string.resource = resource string end |
Instance Method Details
#[](name, orig = false) ⇒ Object
34 35 36 37 |
# File 'lib/rbbt/resource/path.rb', line 34 def [](name, orig = false) return super(name) if orig join name end |
#all_fields ⇒ Object
212 213 214 |
# File 'lib/rbbt/resource/path.rb', line 212 def all_fields TSV.parse_header(self.open).all_fields end |
#annotate(name) ⇒ Object
14 15 16 |
# File 'lib/rbbt/resource/path.rb', line 14 def annotate(name) Path.setup name.to_s, @pkgdir, @resource end |
#basename ⇒ Object
168 169 170 |
# File 'lib/rbbt/resource/path.rb', line 168 def basename Path.setup(File.basename(self), self.resource, self.pkgdir) end |
#byte(pos) ⇒ Object
39 40 41 |
# File 'lib/rbbt/resource/path.rb', line 39 def byte(pos) send(:[], pos, true) end |
#dirname ⇒ Object
26 27 28 |
# File 'lib/rbbt/resource/path.rb', line 26 def dirname Path.setup File.dirname(self), @pkgdir, @resource end |
#doc_file(relative_to = 'lib') ⇒ Object
237 238 239 240 241 242 243 244 245 |
# File 'lib/rbbt/resource/path.rb', line 237 def doc_file(relative_to = 'lib') if located? lib_dir = Path.caller_lib_dir(self, relative_to) relative_file = File.join( 'doc', self.sub(lib_dir,'')) Path.setup File.join(lib_dir, relative_file) , @pkgdir, @resource else Path.setup File.join('doc', self) , @pkgdir, @resource end end |
#exists? ⇒ Boolean
130 131 132 133 134 135 136 137 |
# File 'lib/rbbt/resource/path.rb', line 130 def exists? begin self.produce File.exists? self.find rescue false end end |
#fields ⇒ Object
208 209 210 |
# File 'lib/rbbt/resource/path.rb', line 208 def fields TSV.parse_header(self.open).fields end |
#filename ⇒ Object
126 127 128 |
# File 'lib/rbbt/resource/path.rb', line 126 def filename self.find end |
#find(where = nil, caller_lib = nil, search_paths = nil) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/rbbt/resource/path.rb', line 73 def find(where = nil, caller_lib = nil, search_paths = nil) where = search_paths[:default] if where == :default search_paths ||= SEARCH_PATHS return self if located? if self.match(/(.*?)\/(.*)/) toplevel, subpath = self.match(/(.*?)\/(.*)/).values_at 1, 2 else toplevel, subpath = self, "" end path = nil if where.nil? %w(current user local global lib).each do |w| w = w.to_sym next unless search_paths.include? w path = find(w, caller_lib, search_paths) return path if File.exists? path end if search_paths.include? :default find((search_paths[:default] || :user), caller_lib, search_paths) else raise "Path '#{ path }' not found, and no default specified in search paths: #{search_paths.inspect}" end else where = where.to_sym raise "Did not recognize the 'where' tag: #{where}. Options: #{search_paths.keys}" unless search_paths.include? where libdir = where == :lib ? Path.caller_lib_dir(caller_lib) : "" libdir ||= "" pwd = FileUtils.pwd Path.setup search_paths[where].sub('{PKGDIR}', pkgdir).sub('{PWD}', pwd).sub('{TOPLEVEL}', toplevel).sub('{SUBPATH}', subpath).sub('{LIBDIR}', libdir), @pkgdir, @resource end end |
#find_all(caller_lib = nil, search_paths = nil) ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/rbbt/resource/path.rb', line 106 def find_all(caller_lib = nil, search_paths = nil) search_paths ||= SEARCH_PATHS search_paths = search_paths.dup search_paths.keys. collect{|where| find(where, Path.caller_lib_dir, search_paths)}. compact.select{|file| file.exists?}.uniq end |
#glob(pattern = '*') ⇒ Object
30 31 32 |
# File 'lib/rbbt/resource/path.rb', line 30 def glob(pattern = '*') Dir.glob(File.join(Regexp.quote(self), pattern)).collect{|f| Path.setup(f, self.resource, self.pkgdir)} end |
#identifier_file_path ⇒ Object
216 217 218 219 220 221 222 |
# File 'lib/rbbt/resource/path.rb', line 216 def identifier_file_path if self.dirname.identifiers.exists? self.dirname.identifiers else nil end end |
#identifier_files ⇒ Object
224 225 226 227 228 229 230 |
# File 'lib/rbbt/resource/path.rb', line 224 def identifier_files if identifier_file_path.nil? [] else [identifier_file_path] end end |
#in_dir?(dir) ⇒ Boolean
{{{ Methods
118 119 120 |
# File 'lib/rbbt/resource/path.rb', line 118 def in_dir?(dir) ! ! File.(self).match(/^#{Regexp.quote dir}/) end |
#index(options = {}) ⇒ Object
192 193 194 |
# File 'lib/rbbt/resource/path.rb', line 192 def index( = {}) TSV.index(self.produce, ) end |
#join(name) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/rbbt/resource/path.rb', line 18 def join(name) if self.empty? self.annotate name.to_s else self.annotate File.join(self, name.to_s) end end |
#keys(field = 0, sep = "\t") ⇒ Object
180 181 182 |
# File 'lib/rbbt/resource/path.rb', line 180 def keys(field = 0, sep = "\t") Open.read(self.produce.find).split("\n").collect{|l| next if l =~ /^#/; l.split(sep, -1)[field]}.compact end |
#list ⇒ Object
176 177 178 |
# File 'lib/rbbt/resource/path.rb', line 176 def list Open.read(self.produce.find).split "\n" end |
#located? ⇒ Boolean
25 26 27 |
# File 'lib/rbbt/resource/util.rb', line 25 def located? self.byte(0) == SLASH or (self.byte(0) == DOT and self.byte(1) == SLASH) end |
#open(options = {}, &block) ⇒ Object
160 161 162 |
# File 'lib/rbbt/resource/path.rb', line 160 def open( = {}, &block) Open.open(self.produce.find, , &block) end |
#pipe_to(cmd, options = {}) ⇒ Object
188 189 190 |
# File 'lib/rbbt/resource/path.rb', line 188 def pipe_to(cmd, = {}) CMD.cmd(cmd, {:in => self.open, :pipe => true}.merge()) end |
#pos_index(pos, options = {}) ⇒ Object
200 201 202 |
# File 'lib/rbbt/resource/path.rb', line 200 def pos_index(pos, = {}) TSV.pos_index(self.produce, pos, ) end |
#produce(force = false) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/rbbt/resource/path.rb', line 139 def produce(force = false) path = self.find return self if Open.exists?(path.to_s) and not force raise "No resource defined to produce file: #{ self }" if resource.nil? resource.produce self, force self end |
#range_index(start, eend, options = {}) ⇒ Object
196 197 198 |
# File 'lib/rbbt/resource/path.rb', line 196 def range_index(start, eend, = {}) TSV.range_index(self.produce, start, eend, ) end |
#read(&block) ⇒ Object
151 152 153 |
# File 'lib/rbbt/resource/path.rb', line 151 def read(&block) Open.read(self.produce.find, &block) end |
#set_extension(new_extension = nil) ⇒ Object
232 233 234 235 |
# File 'lib/rbbt/resource/path.rb', line 232 def set_extension(new_extension = nil) new_path = self.sub(/\.[^\.\/]+$/, "." << new_extension.to_s) Path.setup new_path, @pkgdir, @resource end |
#source_for_doc_file(relative_to = 'lib') ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/rbbt/resource/path.rb', line 247 def source_for_doc_file(relative_to = 'lib') if located? lib_dir = Path.caller_lib_dir(Path.caller_lib_dir(self, 'doc'), relative_to) relative_file = self.sub(/(.*\/)doc\//, '\1').sub(lib_dir + "/",'') file = File.join(lib_dir, relative_file) if not File.exists?(file) file= Dir.glob(file.sub(/\.[^\.\/]+$/, '.*')).first end Path.setup file, @pkgdir, @resource else relative_file = self.sub(/^doc\//, '\1') if not File.exists?(relative_file) relative_file = Dir.glob(relative_file.sub(/\.[^\.\/]+$/, '.*')).first end Path.setup relative_file , @pkgdir, @resource end end |
#to_s ⇒ Object
122 123 124 |
# File 'lib/rbbt/resource/path.rb', line 122 def to_s self.find end |
#to_yaml(*args) ⇒ Object
204 205 206 |
# File 'lib/rbbt/resource/path.rb', line 204 def to_yaml(*args) self.to_s.to_yaml(*args) end |
#tsv(*args) ⇒ Object
172 173 174 |
# File 'lib/rbbt/resource/path.rb', line 172 def tsv(*args) TSV.open(self.produce, *args) end |
#write(*args, &block) ⇒ Object
155 156 157 |
# File 'lib/rbbt/resource/path.rb', line 155 def write(*args, &block) Open.write(self.produce.find, *args, &block) end |
#yaml ⇒ Object
184 185 186 |
# File 'lib/rbbt/resource/path.rb', line 184 def yaml YAML.load self.open end |